如何在任何时候停止图像混合并将图像保存到文件

时间:2015-04-14 11:32:32

标签: java eclipse graphics

我创建了一个将两个图像从一个图像混合到另一个图像的应用程序。参见下面: -

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.JPanel;
import javax.swing.Timer;

class Frame extends JPanel implements ActionListener {

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

public Frame() {
    loadImages();
    initTimer();
}

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

private void initTimer() {
    timeBlend = new Timer(1000, this);
    timeBlend.start();
    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, -2, 0, null);
    gBuffI.setComposite(aComp);
    gBuffI.drawImage(MeSmaller2, 0, -7, 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;
        timeBlend.stop();
        System.out.println("Morph Finished please restart.");
    }
    repaint();
}

如何添加一个按钮,单击此按钮将在单击的任何点停止图像混合(再次单击时混合将恢复),然后如何添加一个按钮,在单击时将混合图像保存到文件中(在任何一点想要保存)我希望这不会立刻问太多。

0 个答案:

没有答案