import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class RotationTest implements ActionListener
{
BufferedImage bg;
BufferedImage fg;
JLabel label;
double theta;
double thetaInc;
public RotationTest(BufferedImage bg, BufferedImage fg)
{
this.bg = bg;
this.fg = fg;
theta = 0.0;
thetaInc = Math.PI/6;
}
public void actionPerformed(ActionEvent e)
{
theta += thetaInc;
label.setIcon(new ImageIcon(getImage()));
}
private BufferedImage getImage()
{
int w = bg.getWidth();
int h = bg.getHeight();
BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = out.createGraphics();
g2.drawRenderedImage(bg, null);
double width = fg.getWidth();
double height = fg.getHeight();
double x = (w - width)/2;
double y = (h - height)/2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(theta, width/2, height/2);
g2.drawRenderedImage(fg, at);
g2.dispose();
return out;
}
private JLabel getLabel()
{
ImageIcon icon = new ImageIcon(getImage());
label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}
private JPanel getUIPanel()
{
JButton rotate = new JButton("rotate");
rotate.addActionListener(this);
JPanel panel = new JPanel();
panel.add(rotate);
return panel;
}
public static void main(String[] args) throws IOException
{
ClassLoader cl = RotationTest.class.getClassLoader();
String path = "images/cougar.jpg";
BufferedImage one = ImageIO.read(cl.getResourceAsStream(path));
path = "images/Bird.gif";
BufferedImage two = ImageIO.read(cl.getResourceAsStream(path));
RotationTest test = new RotationTest(one, two);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getLabel());
f.getContentPane().add(test.getUIPanel(), "South");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
Hi,
It works perfect.. i am doing the same thing using rotate operator in JAI... why that is not working... I am pasting both Rotate programs here.. One is in Java2D and 2nd in JAI.. Java2D is working but JAI rotate is leaving black area after rotation.
Please tell where i am wrong in JAI or is it bug in JAI?
Program in JAI
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.util.*;
import javax.media.jai.*;
import javax.media.jai.operator.*;
import javax.media.jai.widget.*;
import com.sun.media.jai.codec.*;
public class RotateTestJAI extends JPanel
{
PlanarImage src1 = null;
PlanarImage src2 = null;
ImageDisplay ic1 = null;
public static void main(String any[]) throws Exception{
JFrame frame = new JFrame();
frame.add(new RotateTestJAI(any[0],any[1]));
frame.setSize(800,600);
frame.setVisible(true);
}
public RotateTestJAI(String im1, String im2) throws Exception{
super(true);
setLayout(new BorderLayout());
try
{
//scale first image
FileSeekableStream stream = new FileSeekableStream(im1);
RenderedOp imscaled = JAI.create("stream",stream);
Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
System.out.println("height : "+imscaled.getHeight()+" Width : "+imscaled.getWidth());
ParameterBlock params = new ParameterBlock();
params.addSource(imscaled);
params.add((float)1388/imscaled.getWidth());
params.add((float)1641/imscaled.getHeight());
params.add(0.00F);
params.add(0.00F);
params.add(interp);
src1 = JAI.create("scale", params );
//scale second image
stream = new FileSeekableStream(im2);
imscaled = JAI.create("stream",stream);
interp = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
params = new ParameterBlock();
params.addSource(imscaled);
params.add((float)400/imscaled.getWidth());
params.add((float)300/imscaled.getHeight());
params.add(0.00F);
params.add(0.00F);
params.add(interp);
src2 = JAI.create("scale", params );
float x = (float)(src1.getHeight()/2)-src2.getHeight();
float y = (float)(src1.getWidth()/2)-src2.getWidth();
int value = 10;
float angle = (float)(value * (Math.PI/180.0F));
//Rotate second image
params = new ParameterBlock();
params.addSource(src2); // The source image
params.add(x); // The x origin
params.add(y); // The y origin
params.add(angle); // The rotation angle
params.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR)); // The interpolation
// Create the rotate operation
src2 = JAI.create("Rotate", params, null);
//translate 2nd image to bring it in center
params = new ParameterBlock();
params.addSource(src2); // The source image
params.add((float)Math.max(x, 0)); // The x translation
params.add((float)Math.max(y, 0)); // The y translation
params.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR)); // The interpolation
// Create the translate operation
src2 = JAI.create("translate", params, null);
//overlay second image on first image
params = new ParameterBlock();
params.addSource(src1);
params.addSource(src2);
RenderedOp tmp = JAI.create("overlay", params);
//put the image on display
ic1=new ImageDisplay(tmp);
this.add(ic1,BorderLayout.CENTER);
}
catch ( MalformedURLException e)
{
return;
}
}
}
Program in Java2D:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class RotationTest implements ActionListener
{
BufferedImage bg;
BufferedImage fg;
JLabel label;
double theta;
double thetaInc;
public RotationTest(BufferedImage bg, BufferedImage fg)
{
this.bg = bg;
this.fg = fg;
theta = 0.0;
thetaInc = Math.PI/6;
}
public void actionPerformed(ActionEvent e)
{
theta += thetaInc;
label.setIcon(new ImageIcon(getImage()));
}
private BufferedImage getImage()
{
int w = bg.getWidth();
int h = bg.getHeight();
BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = out.createGraphics();
g2.drawRenderedImage(bg, null);
double width = fg.getWidth();
double height = fg.getHeight();
double x = (w - width)/2;
double y = (h - height)/2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(theta, width/2, height/2);
g2.drawRenderedImage(fg, at);
g2.dispose();
return out;
}
private JLabel getLabel()
{
ImageIcon icon = new ImageIcon(getImage());
label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}
private JPanel getUIPanel()
{
JButton rotate = new JButton("rotate");
rotate.addActionListener(this);
JPanel panel = new JPanel();
panel.add(rotate);
return panel;
}
public static void main(String[] args) throws IOException
{
ClassLoader cl = RotationTest.class.getClassLoader();
String path = "images/cougar.jpg";
BufferedImage one = ImageIO.read(cl.getResourceAsStream(path));
path = "images/Bird.gif";
BufferedImage two = ImageIO.read(cl.getResourceAsStream(path));
RotationTest test = new RotationTest(one, two);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getLabel());
f.getContentPane().add(test.getUIPanel(), "South");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}