Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners rotate crop & flip image
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


piyush
rotate crop & flip image
2 Answer(s)      6 years and 10 months ago
Posted in : Java Beginners


Hello frends, I m not getting how to flip image ,crop image & rotate image by 90 degree. can you plz give some ideas...


Advertisement
View Answers

March 1, 2008 at 10:56 AM


Java Example Code - Rotate image by 90 degree

Java Source Code
ImageRotator.html

<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET ALIGN="CENTER" CODE="ImageRotator.class" WIDTH="800" HEIGHT="500"></APPLET>
</BODY>
</HTML>


ImageRotator.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageRotator extends JApplet implements ActionListener{
public static final int DEFAULT_SPEED = 50;

private String images[] = {"catfeline.jpg","rose.jpg"};
private JButton button;
private JList list;
private JTextField rotateSpeed;
private JCheckBox checkbox;

public void init(){
getContentPane().setLayout(new BorderLayout());
list = new JList(images);
list.setSelectedIndex(0);
button = new JButton("click me");
button.addActionListener(this);

Panel pane = new Panel(new FlowLayout());
pane.add(button);
Panel panel = new Panel(new FlowLayout());
panel.add(new Label("Enter image rotation speed and direction :"));

rotateSpeed = new JTextField(5);
rotateSpeed.setText(String.valueOf(DEFAULT_SPEED));
checkbox = new JCheckBox("Rotate Clockwise", true);
panel.add(rotateSpeed);
panel.add(checkbox);
getContentPane().add(BorderLayout.NORTH, panel);
getContentPane().add(BorderLayout.CENTER, list);
getContentPane().add(BorderLayout.SOUTH, pane);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(button)){
String imageName = ((String)(list.getSelectedValue())).trim();
Image image = getImage(getCodeBase(), imageName);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 1);
try{
tracker.waitForAll();
}
catch(InterruptedException e){
System.out.println(e);
}

int speed = DEFAULT_SPEED;
try{
speed = Integer.valueOf(rotateSpeed.getText().trim()).intValue();
}
catch(NumberFormatException ex){
System.out.println(ex);
}
ImageRotatorFrame imageRotator = new ImageRotatorFrame(image, speed, imageName, checkbox.isSelected());
}
}
}


March 1, 2008 at 10:59 AM


now write ImageRotatorFrame.java and run both codes in your computer to rotate image using Java


ImageRotatorFrame.java

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageRotatorFrame extends JFrame implements ActionListener{
private ImagePanel imagePanel;
private int rotated;
private Timer rotateTimer;
private JPanel m_imageContainer;

public ImageRotatorFrame(Image image, int speed, String imageName, boolean clockwise){
super("Image Rotator ["+ imageName +"]");
rotated = speed;
if(image == null){
add(new JLabel("Image " + imageName + " not Found"));
}
else{
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
String mess = "Rotation at:" + rotated + " milli-seconds";
if(clockwise){
mess += "in Clockwise direction";
}
else{
mess += " in Anti-Clockwise direction";
}
topPanel.add(new JLabel(mess));
imagePanel = new ImagePanel(image, clockwise);
getContentPane().add(BorderLayout.NORTH, topPanel);
getContentPane().add(BorderLayout.CENTER, imagePanel);
imagePanel.repaint();
}
setSize(1000, 700);
setVisible(true);
rotateTimer = new Timer(rotated, this);
rotateTimer.start();
}
public void actionPerformed(ActionEvent event){
if(event.getSource().equals(rotateTimer)){
//give call to rotate the image
imagePanel.rotate();
}
}

public class ImagePanel extends JPanel{
public static final int ROTATE_ANGLE = 1;
private Image m_image;
private int RotateAngle;
private int imageWidth, imageHeight;
private AffineTransform transform;
private boolean clockway;

public ImagePanel(Image image, boolean clockwise){
clockway = clockwise;
m_image = image;
imageWidth = m_image.getWidth(this);
imageHeight = m_image.getHeight(this);
if(!clockwise){
RotateAngle = 90 + ROTATE_ANGLE;
}
transform = new AffineTransform();
}
public void paintComponent(Graphics grp){
Rectangle rect = this.getBounds();
Graphics2D graphic = (Graphics2D)grp;
graphic.setColor(Color.pink);
graphic.fillRect(0, 0, getWidth(), getHeight());
transform.setToTranslation((rect.width-imageWidth)/2,(rect.height-imageHeight)/2);
transform.rotate(Math.toRadians(RotateAngle), imageWidth/2, imageHeight/2);
graphic.drawImage(m_image, transform, this);
}

public void rotate(){
if(clockway){
RotateAngle += ROTATE_ANGLE;
RotateAngle %= 90;
}
else{
RotateAngle -= ROTATE_ANGLE;
if(RotateAngle <= 0){
RotateAngle = 90;
}
}
repaint();
}
}
}


Amar ;)


Related Tutorials/Questions & Answers:
rotate crop & flip image - Java Beginners
rotate crop & flip image  Hello frends, I m not getting how to flip image ,crop image & rotate image by 90 degree. can you plz give some ideas...  Java Example Code - Rotate image by 90 degreeJava Source
rotate image with caption
rotate image with caption  How to rotate image with caption
Advertisements
rotate image in php - PHP
rotate image in php   Function to rotate image in PHP
Crop Image Code in Applet
Crop Image Code in Applet  Sir, Can somebody please provide me with code to crop and save an image in applet
Crop Image Code in Applet
Crop Image Code in Applet  Sir, Can somebody please provide me with code to crop and save an image in applet
java rotate image
java rotate image   How to rotate image to 90 and 180. can you please post an example to rotate image
Jmagick crop image
Jmagick crop image  hi, How can I crop the images using Jmagick Thanks.   hi, To crop an image you can use the cropImage() method of MagickImage. Following code explains how an image may cropped ImageInfo ii = new
PHP GD rotate image
<?php $image = 'images.jpg'; $degrees = 180; header('Content-type: image/jpeg') ; $source = imagecreatefromjpeg($image) ; $rotate = imagerotate($source, $degrees, 0) ; imagejpeg($rotate) ; ?> After running
Crop Image
Crop Image       This section shows you how the image get crops. To crop an image... image and a filter object by using the class FilteredImageSource. To crop an image
PHP GD flip image
<?php $image = imagecreatefromjpeg("images.jpg"); $image = flip($image,1,1); header("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image); function flip($i,$h=1,$v=0) { $width = imagesx
PHP GD crop image
<?php $src = imagecreatefromjpeg('image1.jpg'); $dest = imagecreatetruecolor(80, 40); imagecopy($dest, $src, 0, 0, 20, 13, 80, 40); header('Content-Type: image/jpeg'); imagegif($dest); imagedestroy($dest
iphone Flip Image
iphone Flip Image In this tutorial will learn how to flip the image, so to do this will use two Image View and a button i.e. bar button for the flip action, whenever we press flip button image should change. For that had to write
Crop image Using JQuery, servlet or JSP.
Crop image Using JQuery, servlet or JSP.  Hi sir I need the code of Cropping image using JQuery with the help of servlet or JSP. I need both the code Client side as well as of server side. Please Help me
Crop Image using plug-in ,JSP and 'Servlet'
Crop Image using plug-in ,JSP and 'Servlet' In this tutorial, we will crop image using jQuery plug-in & servlet. In this example, a image & a button is given.When you press button 'crop image' , it will crop
How to flip image
How to flip image       In this section , you will see how to flip image. To reverse the image, an image is put into the folder where class is specified. The getDefaultToolkit
iphone programming combining move and flip - Java Beginners
iphone programming combining move and flip  Hi, I would like to combine the move image and flip image codes so that i can move and flip different images to show another image
Rotating image in the pdf file
insert a image in a pdf file and rotate it irrespective of the fact whether... Rotating image in the pdf file   ... the image. After that add the content to the document. Add the paragraph
Spark Rotate Effect in Flex4
Spark Rotate Effect in Flex4: The Rotate effect rotates a component. You can set the starting and ending angle of rotation. The syntax of Rotate effect is following: <s:Rotate id="" angleFrom="" angleTo
MX Rotate Effect in Flex4
MX Rotate Effect in Flex4: The Rotate effect rotates a component. You can set...; 3. originX="0" 4. originY="0" The tag of Rotate effect is <mx:Rotate>. In this example you can see how we can use a Rotate
ROTATE THE VOWELS IN A STRING!!! - Java Beginners
ROTATE THE VOWELS IN A STRING!!!  Hi Sir, My Question is to rotate... or lower case, and if it's a vowel or not. Use another function to rotate the vowels. CODING: import jpb.*; public class Rotate_Vowels{ public static
image
image  how to add the image in servlet code
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
flex didn't know how to drag and drop video and audio in flip book - Framework
flex didn't know how to drag and drop video and audio in flip book  hello sir in my project in flex flip book it is book which can be flip.our target to add the video audio and photo in this book i am able to add the photo
image effects - Java Beginners
that will show you image crop effect: import java.sql.*; import java.awt.... JButton("Crop Image"); this.add(crop); crop.addActionListener(new...image effects  hey can u help me in loadin an image file
Image Steganography
Image Steganography  SOURCE CODE FOR IMAGE STEGANOGRAPHY
image scrolling
image scrolling  A brief description of scrolling image
image format
image format  what are the different format available in image
image format
image format  what are the different format available in image
To scan a image encode that image in j2me
To scan a image encode that image in j2me  Hai team i am new man of j2me i have doubt to scan a image and encode the image in j2me... Please clarify that doubt... Thanks in Advance... Kumar
 

 

 

DMCA.com