Total members 11889 |It is currently Thu Mar 28, 2024 9:00 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 Reading a Specific Character in php     -  
 how to get Color name from a specific pixel ??     -  
 ban visitor from a specific IP-address     -  
 ordered list from specific number     -  
 Check the websites hosted at a specific IP     -  
 Controlling the DC motor , Motor rotate clockwise Assembly     -  



Topic Tags

Java Graphics, Java Image






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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