2011-09-01 45 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