简体   繁体   中英

Move a circle on mouse click

I am really new to Java and I need a circle to move around a JFrame when it's clicked, but the circle has to get random coordinates. So far this code generates a new circle every time it's clicked, but all the other circles stay there as well. I only need one circle to move around the frame. So maybe someone can help me a little :)

Here is my code:

public class test2 extends JFrame implements MouseListener {
int height, width;
public test2() {
    this.setTitle("Click");
    this.setSize(400,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    addMouseListener(this);
    width = getSize().width;
    height = getSize().height;
}

public void paint (Graphics g) {
    setBackground (Color.red);
    g.setColor(Color.yellow);
    int a, b;
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    g.fillOval(a, b, 130, 110);
}

    public void mouseClicked(MouseEvent e) {
    int a, b;
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    repaint();
}

public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}

public static void main(String arg[]){

    new test2();
}

}

See if this helps, here i have filled the entire rect with background color before drawing the circle. Though not efficient but serves the purpose

replace the paint method as follows

public void paint (Graphics g) {
        setBackground (Color.red);
        g.setColor(Color.red);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.yellow);
        int a, b;
        a = -50 + (int)(Math.random()*(width+40));
        b = (int)(Math.random()*(height+20));
        g.fillOval(a, b, 130, 110);
    }

i think one of the major problems you are having here is that you don't make global a and b variables. You create 2 new variables every time you call the paint() and mouseClicked() methods. There are two other problems/warnings.

  1. Your `paint() method really should be called paintComponents(Graphics g) if you are using a JFrame
  2. You need to add the line super.paint(g); under your paintComponents() definition.

I am actually quite surprised that anything is drawn at all. Also, Anony-Mousse is right when he says about conventions. Class names should always begin with a capital.

Your code should look like this:

public class Test2 extends JFrame implements MouseListener {
int height, width;
int a,b;
public test2() {
    this.setTitle("Click");
    this.setSize(400,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    addMouseListener(this);
    width = getSize().width;
    height = getSize().height;
}

public void paintComponents(Graphics g) {
    super.paint(g);
    setBackground(Color.red);
    g.setColor(Color.yellow);
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    g.fillOval(a, b, 130, 110);
}

    public void mouseClicked(MouseEvent e) {
    int a, b;
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    repaint();
}

public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}

public static void main(String arg[]){

    new test2();
}

}

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