单击鼠标移动一个圆圈

时间:2012-09-23 09:24:39

标签: java swing user-interface paint mouse-listeners

我对Java很陌生,我需要一个圆圈来点击JFrame,但圆圈必须得到随机坐标。到目前为止,这个代码每次点击都会生成一个新的圆圈,但所有其他圆圈也会保留在那里。我只需要一个圆圈就可以在框架周围移动。所以也许有人可以帮我一点:)

这是我的代码:

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();
}

}

2 个答案:

答案 0 :(得分:5)

看看这是否有帮助,这里我在绘制圆圈之前用背景颜色填充整个矩形。虽然效率不高但是有目的

替换paint方法如下

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);
    }

答案 1 :(得分:5)

我认为你在这里遇到的一个主要问题是你没有制作全局a和b变量。每次调用paint()mouseClicked()方法时,都会创建2个新变量。还有其他两个问题/警告。

  1. 如果您使用`paint()
  2. ,您的paintComponents(Graphics g)方法确实应该被称为JFrame
  3. 您需要在paintComponents()定义下添加行super.paint(g);
  4. 我真的很惊讶任何东西都被吸引了。此外,Anony-Mousse在讲述约定时也是对的。类名应始终以大写字母开头。

    您的代码应如下所示:

    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();
    }
    
    }