Switch to full style
Graphics and animation Java code examples
Post a reply

Rotate Image with specific angle

Thu Feb 07, 2013 8:35 pm

When the user enters an angle, the image is rotated to the specified angle.

java code
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);
}
}




Post a reply
  Related Posts  to : Rotate Image with specific angle
 Specific Colors for Specific Forum     -  
 Transform Rotate Filter     -  
 rotate sphere in a circle with light     -  
 Bit operations-set-get-xor-rotate on bits arrays     -  
 how to get Color name from a specific pixel ??     -  
 ban visitor from a specific IP-address     -  
 Reading a Specific Character in php     -  
 Check the websites hosted at a specific IP     -  
 ordered list from specific number     -  
 Controlling the DC motor , Motor rotate clockwise Assembly     -  

Topic Tags

Java Image, Java Graphics