我真的很新的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();
}
}
一句忠告,最好采取由摇摆提供双缓冲的优势。因此,不要直接在JFrame上绘画,而是通过重写相应的paintComponent(...)方法来绘制JPanel/JComponent。 –
@ Anony-Mousse:该标签[已过时](http://stackoverflow.com/tags/homework/info)。 – trashgod