使用动作侦听器按钮

时间:2015-03-28 13:13:12

标签: java graphics actionlistener

代码:Java Sphere类

 import javax.swing.*;
 import java.awt.*;
 import java.awt.geom.Ellipse2D;

public class Sphere extends JPanel {
private boolean flashinglights = false;
private int x = 168;
private int y = 75;


public void paintComponent(Graphics g) { 
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    if (flashinglights) { //This is the flash option. Here it should change between grey and orange
        g2.setColor(Color.ORANGE);
        Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
        g2.draw(ball);
        g2.fill(ball);
    } else {
        g2.setColor(Color.gray); //This should stay grey as it does now.
        Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
        g2.draw(ball);
        g2.fill(ball);
    }
}

public void chooseflashinglights(){ //Ignore these methods
    flashinglights = false;
}

public void choosesteady(){
    flashinglights = true;
}

public void flickerorange(int d) { y = y + d; }


public void flickergrey(int d) { y = y + d; }


public static void main(String[] args) {
    JFrame scFrame = new AnimationViewer();
    scFrame.setTitle("Circle");
    scFrame.setSize(400, 400);
    scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
    scFrame.setVisible(true);
}

}

动画查看器类:

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;


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() {
    this.add(bPanel, BorderLayout.SOUTH);
    bPanel.add(jbtFlash);
    bPanel.setLayout(new GridLayout(1,2));
    bPanel.add(jbtSteady);

    this.add(sphPanel, BorderLayout.CENTER);


    jbtSteady.addActionListener(new SteadyLights());
    jbtFlash.addActionListener(new FlashingLights());

    timer = new Timer(100, new TimerListener());
    timer.start();
}



class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        sphPanel.flickerorange(0);
        sphPanel.flickergrey(0);
        repaint();
    }
}
class FlashingLights implements ActionListener{
    public void actionPerformed(ActionEvent e){
        sphPanel.chooseflashinglights();
    }
}
class SteadyLights implements ActionListener{
    public void actionPerformed(ActionEvent e){
        sphPanel.choosesteady();
    }
}

}

所以现在屏幕上会出现一个球体。下面显示了两个按钮。闪光和稳定。在稳定按钮上,它必须保持一种颜色(橙色),而不是。

现在在Flash上​​,它必须每100毫秒从橙色变为灰色。

我知道它必须与Action侦听器有关,但我究竟该如何实现呢?

2 个答案:

答案 0 :(得分:2)

你有很多额外的代码。我会这样做的。在paintComponent方法中写入闪烁逻辑。然后只创建一个计时器。在tick上每隔100ms调用paintComponent方法。在闪光灯按钮上单击启动计时器,在稳定按钮上单击停止计时器并调用paintComponent一次。

球类:

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.GRAY };
private int colorIndex = 0;

public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2 = (Graphics2D) g;
   if (!flashinglights) {
       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;

       g2.setColor(colors[colorIndex++]);
       Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
       g2.draw(ball);
       g2.fill(ball);
   }
}

public void chooseflashinglights(){ //Ignore these methods
   flashinglights = true;
}

public void choosesteady(){
   flashinglights = false;
}

public static void main(String[] args) {
   JFrame scFrame = new AnimationViewer();
   scFrame.setTitle("Circle");
   scFrame.setSize(400, 400);
   scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
   scFrame.setVisible(true);
}

}

AnimationViewer类:

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() {
   this.add(bPanel, BorderLayout.SOUTH);
   bPanel.add(jbtFlash);
   bPanel.setLayout(new GridLayout(1,2));
   bPanel.add(jbtSteady);

   this.add(sphPanel, BorderLayout.CENTER);

   timer = new Timer(100, new TimerListener());
   jbtSteady.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            sphPanel.choosesteady();
            timer.stop();
            sphPanel.repaint();
        }
    });
   jbtFlash.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            sphPanel.chooseflashinglights();
            timer.start();
        }
    });

}

class TimerListener implements ActionListener {
   public void actionPerformed(ActionEvent e) {
       sphPanel.repaint();
   }
}

}

答案 1 :(得分:0)

我会做一些不同的事情。

  • 我的绘图JPanel类中有一个字段,用于指示应显示的颜色。
  • 例如,如果只交换了两种颜色,我会使用一个布尔字段,并在我的计时器中交换它的值,然后将颜色绘制基于其值。
  • paintComponent方法将使用该字段(布尔值)来决定用于g.setColor(...)的颜色。
  • 如果绘制了许多颜色,则该字段可以是Color数组和数组中的int索引的组合。然后我根据需要增加索引,并使用paintComponent中的索引来决定要绘制的颜色。
  • 我会在我的Timer的ActionListener中更改该字段 - 只需一次调用即可更改它,而不是您对闪烁的橙色和闪烁灰色的奇怪调用。
  • 我用一个按钮启动计时器
  • 我用另一个按钮停止它。这就是按钮的动作监听器所能做的,只需启动或停止计时器。