[英]Swing Timer with Graphics component
I want to creat a circle in on a panel which appears and dissapears every 2 seconds. 我想在每2秒出现和消失的面板上创建一个圆圈。 Here is that i have:
这是我有的:
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();
}
}
thank you guys. 感谢大伙们。 Now I wanna control my circle.
现在我想控制自己的圈子。 But again something is wrong and it is not moving as I want.
但是再次出了点问题,它没有按照我的意愿移动。
here are new methods: 这是新方法:
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;
}
}
}
/ * ** * / Solved , i just added / * ** * /解决了,我刚刚添加了
addKeyListener(new TAdapter());
to my Board constructor! 给我的董事会建设者!
You just need to have your Timer
modify some state. 您只需要让您的
Timer
修改某些状态即可。 Try something like this: 尝试这样的事情:
private boolean drawCircle = false;
public void actionPerformed(ActionEvent e) {
drawCircle = !drawCircle;
repaint();
}
public void paintComponent(Graphics g) {
//...
if ( drawCircle ) {
g.setColor(Color.gray);
//...
}
}
Aren't you supposed to formulate the question... as a question? 您不是应该提出问题...作为问题吗?
Anyway, add a boolean to your class, toggle it on each action event, and paint the oval only if it is true. 无论如何,向您的班级添加一个布尔值,在每个动作事件上切换它,并且仅在椭圆为真时才对其进行绘制。
Edit/Notes: - Override paintComponent instead of paint - Don't dispose of a Graphics you haven't created. 编辑/注释:-覆盖paintComponent而不是paint-不要处理尚未创建的图形。 Either drop the line, or use g.create() to make a copy.
删除该行,或使用g.create()进行复制。 See http://java.sun.com/products/jfc/tsc/articles/swing2d/ for details.
有关详细信息,请参见http://java.sun.com/products/jfc/tsc/articles/swing2d/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.