Thu Feb 07, 2013 8:35 pm
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
/*
* displays an image.
*/
public class RotateImage extends Applet {
TextField degreeField;
RotatorCanvas rotator;
double radiansPerDegree = Math.PI / 180;
public void init() {
// Read Image
Image image = getImage(getCodeBase(), "../xyz.gif");
//Set Layout GridBag
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label l = new Label("Degrees");
gridBag.setConstraints(l, c);
add(l);
degreeField = new TextField(5);
gridBag.setConstraints(degreeField, c);
add(degreeField);
Button b = new Button("Refresh");
c.gridwidth = GridBagConstraints.REMAINDER;
gridBag.setConstraints(b, c);
add(b);
rotator = new RotatorCanvas(image);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(rotator, c);
add(rotator);
validate();
}
public boolean action(Event evt, Object arg) {
int degrees;
try {
degrees = Integer.parseInt(degreeField.getText());
} catch (NumberFormatException e) {
degrees = 0;
}
//Convert to radians.
rotator.rotateImage((double)degrees * radiansPerDegree);
return true;
}
}
class RotatorCanvas extends Canvas {
Image sourceImage;
Image resultImage;
public RotatorCanvas(Image image) {
sourceImage = image;
resultImage = sourceImage;
}
public void rotateImage(double angle) {
ImageFilter filter = new RotateFilter(angle);
ImageProducer producer = new FilteredImageSource(
sourceImage.getSource(),
filter);
resultImage = createImage(producer);
repaint();
}
public void paint(Graphics g) {
Dimension d = size();
int x = (d.width - resultImage.getWidth(this)) / 2;
int y = (d.height - resultImage.getHeight(this)) / 2;
g.drawImage(resultImage, x, y, this);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.