跳出循环,但需要保持运行?
Getting out of a loop, but it needs to remain running?
我已经绕了一个圈(在一个圆圈里),但现在我的应用程序不会继续执行其他任务,因为我陷入了一个循环。
如何摆脱这种情况,同时让我的圆圈在后台移动?
- 我知道这是因为我一直在调用方法 draw();,但是我应该如何保持绘制这个圆?
public class MyFrame extends JPanel {
int xc = 150, yc = 150, r = 100, diam = 50;
double inc = Math.PI/360, theta = 0;
public void draw(Graphics g) {
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
theta = theta+inc;
repaint();
System.out.println(theta);
}
});
timer.setDelay(20);
timer.start();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); //smooth the border around the circle
g2d.rotate(theta, xc, yc);
g2d.setColor(Color.blue);
g2d.drawOval(xc + r - diam / 2, yc + r - diam / 2, diam, diam);
draw(g);
}
}
将循环、计时器与绘画分开。让定时器改变关键变量的状态,然后调用重绘。因此,您可以在绘画方法之外创建并启动 Timer ,例如在构造函数中,并让 paintComponent 专注于一件事和一件事,绘画,而不是改变状态:
public class MyPanel extends JPanel {
int xc = 150, yc = 150, r = 100, diam = 50;
double inc = Math.PI/360, theta = 0;
public MyPanel () {
Timer timer = new Timer(20, e -> {
theta += inc;
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2d.rotate(theta, xc, yc);
g2d.setColor(Color.blue);
g2d.drawOval(xc + r - diam / 2, yc + r - diam / 2, diam, diam);
}
}
我已经绕了一个圈(在一个圆圈里),但现在我的应用程序不会继续执行其他任务,因为我陷入了一个循环。
如何摆脱这种情况,同时让我的圆圈在后台移动?
- 我知道这是因为我一直在调用方法 draw();,但是我应该如何保持绘制这个圆?
public class MyFrame extends JPanel {
int xc = 150, yc = 150, r = 100, diam = 50;
double inc = Math.PI/360, theta = 0;
public void draw(Graphics g) {
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
theta = theta+inc;
repaint();
System.out.println(theta);
}
});
timer.setDelay(20);
timer.start();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); //smooth the border around the circle
g2d.rotate(theta, xc, yc);
g2d.setColor(Color.blue);
g2d.drawOval(xc + r - diam / 2, yc + r - diam / 2, diam, diam);
draw(g);
}
}
将循环、计时器与绘画分开。让定时器改变关键变量的状态,然后调用重绘。因此,您可以在绘画方法之外创建并启动 Timer ,例如在构造函数中,并让 paintComponent 专注于一件事和一件事,绘画,而不是改变状态:
public class MyPanel extends JPanel {
int xc = 150, yc = 150, r = 100, diam = 50;
double inc = Math.PI/360, theta = 0;
public MyPanel () {
Timer timer = new Timer(20, e -> {
theta += inc;
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2d.rotate(theta, xc, yc);
g2d.setColor(Color.blue);
g2d.drawOval(xc + r - diam / 2, yc + r - diam / 2, diam, diam);
}
}