/**
*
*
Undo Redo for image manipulations
* Copyright: Copyright (c) 2007 Raditha Dissanayake.
*
* @version 1.0
*/
package com.raditha.articles;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.renderable.ParameterBlock;
import java.util.Vector;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.swing.*;
/**
*
* @author raditha
*/
public class RenderedOpHistory
{
JFrame jfr = new JFrame();
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.drawRenderedImage(image, new AffineTransform());
}
};
RenderedOp image;
JButton btn_undo = new JButton("Undo");
JButton btn_redo = new JButton("Redo");
Vector redoActions = new Vector();
/** Creates a new instance of UndoRedo */
public RenderedOpHistory()
{
btn_undo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
Vector v = image.getSources();
if(v != null && v.size() != 0)
{
redoActions.add(image);
image = v.firstElement();
panel.repaint();
}
}
});
btn_redo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
if(redoActions != null && redoActions.size() != 0)
{
image = redoActions.lastElement();
redoActions.remove(image);
panel.repaint();
}
}
});
image = JAI.create("fileload","/home/raditha/radinka.png");
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
double consts[] = {100,20,30};
pb.add(consts);
image = JAI.create("addconst", pb, null);
pb = new ParameterBlock();
pb.addSource(image);
consts[1]=200;
pb.add(consts);
image = JAI.create("addconst", pb, null);
pb = new ParameterBlock();
pb.addSource(image);
pb.add(0.5f);
pb.add(0.5f);
pb.add(0.0F);
pb.add(0.0F);
image = JAI.create("scale",pb,null);
jfr.getContentPane().setLayout(new BorderLayout());
JPanel jp_sub = new JPanel();
jp_sub.setLayout(new FlowLayout());
jfr.getContentPane().add(panel, BorderLayout.CENTER);
jfr.getContentPane().add(jp_sub,BorderLayout.SOUTH);
jp_sub.add(btn_undo);
jp_sub.add(btn_redo);
Dimension dim = new Dimension(image.getWidth(), image.getHeight());
System.out.println(dim);
panel.setSize(dim);
jfr.setSize(400,400);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(jfr.EXIT_ON_CLOSE);
panel.repaint();
}
public static void main(String[] args)
{
System.setProperty("com.sun.media.jai.disableMediaLib","true");
RenderedOpHistory undoRedo = new RenderedOpHistory();
}
}