所以我设定的任务是制作一个灯的动画。添加的按钮可以执行不同的操作,例如更改球体的颜色等。
代码:Sphere Class
public class Sphere extends JPanel {
private boolean flashinglights = false;
private int x = 168;
private int y = 75;
private Color[] colors = new Color[] {Color.ORANGE, Color.LIGHT_GRAY };
private int colorIndex = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Graphics2D g3 = (Graphics2D) g;
if (!flashinglights) {
Rectangle box0 = new Rectangle(x+16, y+50,14, 50);
g3.setColor(Color.black);
g3.draw(box0);
g3.fill(box0);
Rectangle box1 = new Rectangle(x+16, y+90,14, 50);
g3.setColor(Color.white);
g3.draw(box1);
g3.fill(box1);
Rectangle box2 = new Rectangle(x+16, y+130,14, 50);
g3.setColor(Color.black);
g3.draw(box2);
g3.fill(box2);
Rectangle box3 = new Rectangle(x+16, y+170,14, 50);
g3.setColor(Color.white);
g3.draw(box3);
g3.fill(box3);
Rectangle box4 = new Rectangle(x+16, y+210,14, 50);
g3.setColor(Color.black);
g3.draw(box4);
g3.fill(box4);
Rectangle box5 = new Rectangle(x+16, y+250,14, 50);
g3.setColor(Color.white);
g3.draw(box5);
g3.fill(box5);
Rectangle box6 = new Rectangle(x+16, y+290,14, 50);
g3.setColor(Color.black);
g3.draw(box6);
g3.fill(box6);
Rectangle box7 = new Rectangle(x+16, y+330,14, 50);
g3.setColor(Color.white);
g3.draw(box7);
g3.fill(box7);
Rectangle box8 = new Rectangle(x+16, y+370,14, 50);
g3.setColor(Color.black);
g3.draw(box8);
g3.fill(box8);
Rectangle box9 = new Rectangle(x+16, y+410,14, 50);
g3.setColor(Color.white);
g3.draw(box9);
g3.fill(box9);
Rectangle box10 = new Rectangle(x+16, y+450,14, 50);
g3.setColor(Color.black);
g3.draw(box10);
g3.fill(box10);
g2.setColor(Color.ORANGE);
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
g2.draw(ball);
g2.fill(ball);
} else {
if(colorIndex > colors.length - 1)
colorIndex = 0;
Rectangle box0 = new Rectangle(x+16, y+50,14, 50);
g3.setColor(Color.black);
g3.draw(box0);
g3.fill(box0);
Rectangle box1 = new Rectangle(x+16, y+90,14, 50);
g3.setColor(Color.white);
g3.draw(box1);
g3.fill(box1);
Rectangle box2 = new Rectangle(x+16, y+130,14, 50);
g3.setColor(Color.black);
g3.draw(box2);
g3.fill(box2);
Rectangle box3 = new Rectangle(x+16, y+170,14, 50);
g3.setColor(Color.white);
g3.draw(box3);
g3.fill(box3);
Rectangle box4 = new Rectangle(x+16, y+210,14, 50);
g3.setColor(Color.black);
g3.draw(box4);
g3.fill(box4);
Rectangle box5 = new Rectangle(x+16, y+250,14, 50);
g3.setColor(Color.white);
g3.draw(box5);
g3.fill(box5);
Rectangle box6 = new Rectangle(x+16, y+290,14, 50);
g3.setColor(Color.black);
g3.draw(box6);
g3.fill(box6);
Rectangle box7 = new Rectangle(x+16, y+330,14, 50);
g3.setColor(Color.white);
g3.draw(box7);
g3.fill(box7);
Rectangle box8 = new Rectangle(x+16, y+370,14, 50);
g3.setColor(Color.black);
g3.draw(box8);
g3.fill(box8);
Rectangle box9 = new Rectangle(x+16, y+410,14, 50);
g3.setColor(Color.white);
g3.draw(box9);
g3.fill(box9);
Rectangle box10 = new Rectangle(x+16, y+450,14, 50);
g3.setColor(Color.black);
g3.draw(box10);
g3.fill(box10);
g2.setColor(colors[colorIndex++]);
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
g2.draw(ball);
g2.fill(ball);
}
}
public void chooseflashinglights(){
flashinglights = true;
}
public void choosesteady(){
flashinglights = false;
}
public static void main(String[] args) {
JFrame scFrame = new AnimationViewer();
scFrame.setTitle("Belisha Beacon");
scFrame.setSize(400, 500);
scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
scFrame.setVisible(true);
}
}
代码:动画查看器CLass
public class AnimationViewer extends JFrame {
JButton jbtFlash = new JButton("Flash");
JButton jbtSteady = new JButton("Steady");
JPanel bPanel = new JPanel();
Sphere sphPanel = new Sphere();
Timer timer;
public AnimationViewer() {
timer = new Timer(500, new TimerListener());
this.add(bPanel, BorderLayout.SOUTH);
bPanel.add(jbtFlash);
bPanel.setLayout(new GridLayout(1, 1));
bPanel.add(jbtSteady);
this.add(sphPanel, BorderLayout.CENTER);
jbtFlash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sphPanel.chooseflashinglights();
timer.start();
}
});
jbtSteady.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sphPanel.choosesteady();
timer.stop();
sphPanel.repaint();
}
});
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
sphPanel.repaint();
}
}
}
主要的两个按钮做两件事。一个使球体保持纯橙色(稳定),另一个使球体从橙色变为灰色。 (闪烁)
现在出现问题: 当您启动程序时,球体以稳定模式启动,颜色只是纯橙色。
我希望程序以闪烁模式启动。因此,当您单击运行时,球体应该在闪烁阶段笔直,从橙色到灰色直接交替。
那我该如何制作呢?所以我可以先启动一段代码,直接进入闪烁灯模式?
答案 0 :(得分:1)
在方法中提取用于将模式切换为闪烁的代码,以避免代码重复:
private void flash() {
sphPanel.chooseflashinglights();
timer.start();
}
从动作侦听器调用此方法:
jbtFlash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flash();
}
});
并在构造函数中调用它:
public AnimationViewer() {
// existing code omitted
flash();
}