Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:59 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: flip image in java
PostPosted: Tue Nov 11, 2008 8:08 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

flip image in java
Code:
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(350, 250);
    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(450, 400);
    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, 0, 0, this);
    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, 0, 0, this);
  }
}


Attachments:
File comment: before flip
flip1.gif
flip1.gif [ 13.25 KiB | Viewed 4312 times ]
File comment: after flip
flip2.gif
flip2.gif [ 13.09 KiB | Viewed 4308 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 Image Insert in Excel File Using POI
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 image processing
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team