2012-09-23 141 views
3

我真的很新的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(); 
} 

} 
+1

一句忠告,最好采取由摇摆提供双缓冲的优势。因此,不要直接在JFrame上绘画,而是通过重写相应的paintComponent(...)方法来绘制JPanel/JComponent。 –

+0

@ Anony-Mousse:该标签[已过时](http://stackoverflow.com/tags/homework/info)。 – trashgod

回答

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

谢谢了很多,它的工作:) – Myt

5

我想你有这里的主要问题之一是,你不要让全球a和b的变量。每次调用paint()mouseClicked()方法时,都会创建2个新变量。还有两个其他问题/警告。

  1. `paint()方法真的应该,如果你使用的是JFrame
  2. 您需要添加在你paintComponents()定义行super.paint(g);被称为paintComponents(Graphics g)

其实我很惊讶,任何事情都是在所有绘制。而且,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(); 
} 

} 
+4

+1,但我建议你(和OP)使用一个JPanel或者JComponent的,而不是为一体,它提供了双缓冲,恕我直言 – MadProgrammer

+0

您的权利,当然,但我专注于实际的图形而不是屏幕。 – imulsion

+0

对不起,只是一个观察,而不是一个批评;) – MadProgrammer