Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How to flip image 
 

To reverse the image, an image is put into the folder where class is specified. The getDefaultToolkit() method of ToolKit class provides the default toolkit.

 

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() method of ToolKit class provides the default toolkit. The method getImage() of class ToolKit returns an image.

The MediaTracker class provides a media objects including images. Create an instance of MediaTracker and call addImage() method which adds an image. The method waitForAll() of this class loads the image which is added.

To wrap one or more data arrays to store image data, we have used the class DataBuffer. The method getDataBuffer() retrieves the data buffer. The method getSize() of DataBuffer class returns the size of all data arrays. To return the requested element from the array, the method getElem(i) is used. The method setElem(j, buffer1.getElem(i)) sets the requested element in the array.

 We have create two objects of  class DataBuffer, i.e., buffer1 which retrieves the the data buffer of bufferedImage1 and buffer2 retrieves the data buffer of bufferedImage2. The two objects bufferedImage1 and bufferedImage2 of class BufferedImage class acts as a source image and the destination image respectively. The destination image is the flipped image.

Here is the code of FlipImageExample.java

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

public class FlipImageExample extends JFrame {
  ShowPanel panel;
  JButton button;

  public FlipImageExample() {
    super("Flip the image");
    Container container = getContentPane();
    panel = new ShowPanel();
    container.add(panel);

    Box box = Box.createHorizontalBox();
    button = new JButton("Flip");
    button.addActionListener(new ButtonListener());
    box.add(Box.createHorizontalGlue());
    box.add(button);
    box.add(Box.createHorizontalGlue());
    container.add(box, BorderLayout.NORTH);

    addWindowListener(new WindowEventHandler());
    setSize(350250);
    setVisible(true); 
  }
    class WindowEventHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
  }
    public static void main(String arg[]) {
    new FlipImageExample();
  }
    class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if (!panel.flip) {
        panel.flipImage();
        panel.bufferedImage = panel.bufferedImage2;
        panel.flip= true;
      }
    else {
        panel.bufferedImage = panel.bufferedImage1;
        panel.flip = false;
      }
      panel.repaint();
    }
  }
}
class ShowPanel extends JPanel {
  BufferedImage bufferedImage, bufferedImage1, bufferedImage2;
  boolean flip = false;

    ShowPanel() {
    setBackground(Color.white);
    setSize(450400);
    Image image = getToolkit().getImage("image4.jpg");
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 1);
    try {
      mediaTracker.waitForAll();
    catch (Exception e) {}
    bufferedImage1 = new BufferedImage(image.getWidth(this), 
      image.getHeight(
this),BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufferedImage1.createGraphics();
    g2d.drawImage(image, 00this);
    bufferedImage = bufferedImage1;
  }
    public void flipImage() {
    bufferedImage2 = new BufferedImage(bufferedImage1.getWidth(), 
    bufferedImage1.getHeight(), bufferedImage1.getType());
    DataBuffer buffer1 = bufferedImage1.getRaster().getDataBuffer();
    DataBuffer buffer2 = bufferedImage2.getRaster().getDataBuffer();
      for (int i = buffer1.getSize() - 1, j = 0; i >= 0; --i, j++) {
      buffer2.setElem(j, buffer1.getElem(i));
    }
  }
    public void paintComponent(Graphics g) {
     Graphics2D g2D = (Graphics2D) g;
     g2D.drawImage(bufferedImage, 00this);
  }
}

Output will be displayed as:

On clicking the button, the image will get reverse.

]

Download Source Code

                         

» View all related tutorials
Related Tags: c class orm form io method sed format 2d get ip translation return instance transform show for transformation to draw

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.