我无法使用我的按钮来控制Java中的计时器

时间:2015-04-16 10:02:29

标签: java eclipse swing button actionlistener

我有这段代码:

public void initTimer() {

        timer = new Timer(1000, this);
        timer.start();
        alpha = 1f;
    }

启动两个图像从一个到另一个的混合。我添加了3个按钮。启动混合,停止混合和保存图像。此代码位于一个名为Frame的类中。

public Frame() {

        loadImages();
        initTimer();

        pnlButton.add(btnStartBlend);
        this.add(pnlButton);
        pnlButton.add(btnStopBlend);
        this.add(pnlButton);
        pnlButton.add(saveImage);
        this.add(pnlButton);
    }

        public static void placeComponents (JFrame frame)  {
        frame.setLayout(null);  

        ActionListener btnStartBlendListener = (ActionListener) new BtnStartBlendListener();
        btnStartBlend.addActionListener(btnStartBlendListener);

        ActionListener btnStopBlendListener = (ActionListener) new BtnStopBlendListener();
        btnStopBlend.addActionListener(btnStopBlendListener);

        ActionListener saveImageListener = (ActionListener) new SaveImageListener();
        saveImage.addActionListener(saveImageListener);
    }

    private void loadImages() {
        MeSmaller1 = new ImageIcon("MeSmaller1.jpg").getImage();
        MeSmaller2 = new ImageIcon("MeSmaller2.jpg").getImage();
    }

    public void initTimer() {

        timer = new Timer(1000, this);
        timer.start();
        alpha = 1f;
    }

这个代码在另一个名为pictureMorph的类中: -

 public void initTimer() {
    int delay = 500;
    ActionListener btnStartBlend = new BtnStartBlendListener() {
          public void actionPerformed(BtnStartBlendListener e) {
              //...Perform a task...
          }
      };

我还为按钮创建了3个其他类,所有这些类只是与按钮相关的不同标题: -

public class BtnStartBlendListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    JButton btnStartBlend = (JButton) e.getSource();
}
}

所以还有BtnStopListener ....和SaveImageListener。我怎样才能让按钮做我想做的事情,我对此很困惑。

此致

卡罗琳

1 个答案:

答案 0 :(得分:0)

如果你需要的只是ActionListener按钮来启动计时器,那么这样就可以了:

JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        initTimer();
    }
});

如果这不能满足您的要求,请告诉我。

(为什么你在ActionListener方法中创建了pictureMorph.initTimer()?)


编辑 - 回答

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

class Frame extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private Image MeSmaller1;
    private Image MeSmaller2;
    protected Timer timer;
    private float alpha;

    JPanel pnlButton = new JPanel();
    static JButton btnStartBlend = new JButton("Start Blend");
    static JButton btnStopBlend = new JButton("Stop Blend");
    static JButton saveImage = new JButton("Save Image To File");

    /*BtnStartBlendListener BtnStart = new BtnStartBlendListener();
     BtnStopBlendListener BtnStop = new BtnStopBlendListener();
     SaveImageListener SaveImg = new SaveImageListener();*/
    public Frame() {

        loadImages();
        initTimer();
        timer.start();
        //      timer.stop();

        pnlButton.add(btnStartBlend);
        this.add(pnlButton);
        pnlButton.add(btnStopBlend);
        this.add(pnlButton);
        pnlButton.add(saveImage);
        this.add(pnlButton);

        addListeners();
    }

    public void addListeners() {
        //start button actionlistener
        btnStartBlend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initTimer();
                timer.start();

                System.out.println("Timer started");
            }
        });

        //stop button actionlistener
        btnStopBlend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // initTimer();
                timer.stop();

                System.out.println("Timer stopped");
            }
        });

        //Save button actionlistener
        saveImage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Call to method to save image

                System.out.println("Save image clicked");
            }
        });
    }

    private void loadImages() {
        MeSmaller1 = new ImageIcon("MeSmaller1.jpg").getImage();
        MeSmaller2 = new ImageIcon("MeSmaller2.jpg").getImage();
    }

    public final void initTimer() {

        timer = new Timer(1000, this);
        timer.start();
        timer.stop();
        alpha = 1f;
    }

    private void doDrawing(Graphics g) {
        Graphics2D g2Dim = (Graphics2D) g;
        // below sets the size of picture
        BufferedImage buffImage = new BufferedImage(400, 600,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D gBuffI = buffImage.createGraphics();
        AlphaComposite aComp = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha);
        // decides where images are drawn in JFrame
        gBuffI.drawImage(MeSmaller1, 28, 55, null);
        gBuffI.setComposite(aComp);
        gBuffI.drawImage(MeSmaller2, 30, 48, null);
        g2Dim.drawImage(buffImage, 10, 10, null);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        alpha -= 0.1;
        if (alpha <= 0) {
            alpha = 0;
            timer.stop();
            System.out.println("Morph Finished please restart.");
        }
        repaint();
    }
}

//---------------------------------------------------------------------------------------------------------

public class PictureMorph extends JFrame {

    private static final long serialVersionUID = 1L;

    public PictureMorph() {
        initUI();
    }

    private void initUI() {
        JFrame frame = new JFrame("Image Morph");
        setTitle("Facial Image Manipulation");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new Frame());
        // below set Frame Size around image
        setSize(380, 470);
        setLocationRelativeTo(null);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                PictureMorph picMorph = new PictureMorph();
                picMorph.setVisible(true);
            }
        });
    }
}