Java 用定时器使圆圈改变颜色
所以我试着让这个圆圈里的灯泡每半秒钟就换一次颜色,每当我按下闪烁按钮的时候。我试过几件事,但都卡住了。。我对这方面还不太熟悉。这是我目前的代码:Java 用定时器使圆圈改变颜色,java,swing,animation,timer,Java,Swing,Animation,Timer,所以我试着让这个圆圈里的灯泡每半秒钟就换一次颜色,每当我按下闪烁按钮的时候。我试过几件事,但都卡住了。。我对这方面还不太熟悉。这是我目前的代码: /** * Created by Metallion on 30/03/2015. */ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; public class BelishaBeacon {
/**
* Created by Metallion on 30/03/2015.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BelishaBeacon {
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
private boolean changeColors = false;
private boolean flashColors = true;
private Timer timer;
public void paintComponent(Graphics g) {
super.paintComponent(g);
final Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
if (changeColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
changeColors = false;
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
g2.setColor(Color.GRAY);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
repaint();
}
}
}
public Drawing() {
this.add(TimerListener());
timer = new Timer(30, new TimerListener());
timer.start();
}
public void changeColors() {
changeColors = true;
repaint();
}
}
public BelishaBeacon() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
JButton jbtFlash = new JButton("Flash");
jbtFlash.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapes.flashColors();
}
}
);
JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapes.changeColors();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
}
}
问题是什么?你的代码做什么,应该做什么?我的代码基本上创建了一个带有指示灯和两个按钮的GUI。按钮稳定且闪烁。每当我单击“稳定”时,信标必须改变颜色,每当我单击“闪烁”时,信标必须开始在两种颜色之间闪烁,例如黄色和绿色。我应该编辑并发布完整代码吗?是的,请发布完整代码。好的,我发布了完整代码。TimerListener和method FlashColor未知。