2012-09-23 142 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