2011-09-01 21 views
1

我想在每2秒出現一次並消失的面板上創建一個圓圈。 下面是我有:帶圖形組件的擺動計時器

public class Board extends JPanel implements ActionListener { 

    private final int DELAY = 2000; 

    private Timer timer; 

    /* 
    * constructor 
    */ 
    public Board() { 

     setFocusable(true); 
     initGame(); 
    } 

    /* 
    * initialize board 
    */ 
    public void initGame() { 

     timer = new Timer(DELAY, this); 
     timer.start(); 

    } 

    public void paint(Graphics g) { 

     super.paint(g); 
     g.setColor(Color.gray); 
     // draw an oval starting at 20,20 with a width and height of 100 and 
     // fill it 
     g.drawOval(20, 20, 100, 100); 
     g.fillOval(20, 20, 100, 100); 

     g.dispose(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     repaint(); 
    } 
} 

感謝你們。現在我想控制我的圈子。但是再次出現了一些問題,並且它並沒有像我想要的那樣移動。

這裏有新的方法:

private boolean left = false; 
private boolean right = true; 
private boolean up = false; 
private boolean down = false; 

private Timer timer; 

public int x = 20; 
public int y = 20; 
public int x2 = 100; 
public int y2 = 100; 

...

public void paint(Graphics g) { 

    super.paint(g); 
    if (drawCircle) { 
     g.setColor(Color.gray); 
     // draw an oval starting at 20,20 with a width and height of 100 and 
     // fill it 
     g.drawOval(x, y, x2, y2); 
     g.fillOval(x, y, x2, y2); 
    } 
    // removes native non-Java recourses 
    g.dispose(); 
} 

public void move() { 

    if (left) { 
     x -= 5; 
    } 
    if (right) { 
     x += 5; 
    } 
    if (up) { 
     y -= 5; 
    } 
    if (down) { 
     y += 5; 
    } 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

    move(); 
    repaint(); 
} 

private class TAdapter extends KeyAdapter { 

    public void keyPressed(KeyEvent e) { 

     int key = e.getKeyCode(); 

     if ((key == KeyEvent.VK_LEFT) && (!right)) { 
      left = true; 
      up = false; 
      down = false; 
     } 

     if ((key == KeyEvent.VK_RIGHT) && (!left)) { 
      right = true; 
      up = false; 
      down = false; 
     } 

     if ((key == KeyEvent.VK_UP) && (!down)) { 
      up = true; 
      right = false; 
      left = false; 
     } 

     if ((key == KeyEvent.VK_DOWN) && (!up)) { 
      down = true; 
      right = false; 
      left = false; 
     } 
    } 
} 

/* ** *解決/ ,我只是說

addKeyListener(new TAdapter()); 

給我的主板構造函數!

+1

問題是什麼? –

+2

在Swing中,不重寫paint,而是覆蓋paintComponent – kleopatra

回答

3

你只需要你的Timer修改某些狀態。試試這樣:

private boolean drawCircle = false; 

public void actionPerformed(ActionEvent e) { 
    drawCircle = !drawCircle; 
    repaint(); 
} 

public void paintComponent(Graphics g) { 
    //... 
    if (drawCircle) { 
     g.setColor(Color.gray); 
     //... 
    } 
} 
+2

與OP相同的評論:-) – kleopatra

2

難道你不應該提出這個問題......作爲一個問題嗎?

無論如何,爲你的類添加一個布爾值,在每個動作事件上切換它,並且只有在它爲真時才繪製橢圓。

編輯/註釋: - 覆寫paintComponent而不是繪圖 - 不要處置尚未創建的圖形。要麼刪除該行,要麼使用g.create()進行復制。詳情請參閱http://java.sun.com/products/jfc/tsc/articles/swing2d/

+2

由於無法預測何時會調用該方法,因此不應在'paint'上發生切換。它應該被任何驅動遊戲狀態的開關切換;在這種情況下是'Timer'。 –

+0

確實,謝謝指出我的答案不夠精確... :-)(通過「在每個油漆上」,我想表示「在每個油漆觸發器上」來自定時器動作) – PhiLho

+0

我相應地更改了我的配方(並增加了更多的建議)。 – PhiLho