简体   繁体   中英

Java repaint method do not working ,why?

Well my English is not so good but i will try to explain.

I was make two class,First Class and Second Class(Second was named "Grafika").

I want that my rectangle move to position where i was clicked , but obviously he dont move and i cant understand why, please help .

import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    public class Grafika extends JPanel implements MouseListener{
        static int x=0,y=0;
        @Override
    public void paintComponent(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 20, 30);
    }

    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub``
        x=arg0.getX();
        y=arg0.getY();
        this.repaint(x, y, 20, 30);
    }

i will show you a full code, it was small .This was second class.And my(i think) only problem is repaint() method . Why i dont know :D .

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Grafika extends JPanel implements MouseListener{
    static int x=0,y=0;
    @Override
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, 20, 30);
}

public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    x=arg0.getX();
    y=arg0.getY();
    this.repaint(x, y, 20, 30);
}

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

Now i will show you first class from where i was calling second class.

import java.awt.*;
import javax.swing.*;
public class Glavna extends Grafika {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
     Grafika g=new Grafika();
     JFrame wi=new JFrame("Grafika");
     wi.setBounds(50, 50, 500, 600);
     wi.add(g);
     wi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     wi.setVisible(true);


    }

}

You've several problems in that code:

  • You're sort of creating a MouseListener (you're still missing several of the interface's methods), but I don't see anywhere that you've added it to your GUI. You must somewhere have addMouseListener(this); in your code for the listener to work.
  • Repaint by itself doesn't move the rectangle. Changing the X and Y does, but again, not without adding the MouseListener first.
  • You probably want to call repaint(); without parameters to paint the whole component. Otherwise the old rectangle might not get erased.
  • Your paintComponent method should call the super.paintComponent(g); as the first method call of your override method. Without this you won't erase the old rectangle, and you break the painting chain which could have painting side effects in child components.

For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class Grafika extends JPanel {
    private static final int RECT_W = 20;
    private static final int RECT_H = 30;
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final Color MY_COLOR = Color.RED;    
    private int myX = 0;
    private int myY = 0;

    public Grafika() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);  // add MouseListener
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);  // **MUST** call this 
        g.setColor(MY_COLOR);
        g.fillRect(myX, myY, RECT_W, RECT_H);
    }

    private class MyMouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            myX = e.getX();
            myY = e.getY();
            repaint(); // repaint the whole JPanel
        }
    }

    @Override  // to make the GUI larger
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        Grafika mainPanel = new Grafika();

        JFrame frame = new JFrame("Grafika");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

}

If you want to get fancier and start dragging the square around:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class Grafika extends JPanel {
    private static final int RECT_W = 20;
    private static final int RECT_H = RECT_W;
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final Color MY_COLOR = Color.RED;
    private int myX = 0;
    private int myY = 0;

    public Grafika() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(MY_COLOR);
        g.fillRect(myX, myY, RECT_W, RECT_H);
    }

    private class MyMouse extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
            moveRect(e);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            moveRect(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            moveRect(e);
        }

        private void moveRect(MouseEvent e) {
            myX = e.getX() - RECT_W / 2;
            myY = e.getY() - RECT_H / 2;
            repaint();
        }

    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        Grafika mainPanel = new Grafika();

        JFrame frame = new JFrame("Grafika");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

}

you have to add mouse listener to component porobably

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Grafika extends JPanel implements MouseListener{
    static int x=0,y=0;

public Grafika(){
    super();
    addMouseListener(this); // add to constructor
}

@Override
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, 20, 30);
}

public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub``
    x=arg0.getX();
    y=arg0.getY();
    this.repaint(x, y, 20, 30);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM