AffineTransform demo : Transform « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » TransformScreenshots 
AffineTransform demo
AffineTransform demo
   

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AffineTransformApp extends JFrame {
  DisplayPanel displayPanel;

  JComboBox scaleXval, scaleYval, shearXval, shearYval;

  String[] scaleValues = "0.10""0.25""0.50""0.75""1.00""1.25",
      "1.50""1.75""2.00" };

  String[] shearValues = "0.00""0.25""0.50""0.75""1.00" };

  public AffineTransformApp() {
    super();
    Container container = getContentPane();

    displayPanel = new DisplayPanel();
    container.add(displayPanel);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2455));
    scaleXval = new JComboBox(scaleValues);
    scaleXval.setSelectedItem("1.00");
    scaleXval.addActionListener(new ComboBoxListener());
    scaleYval = new JComboBox(scaleValues);
    scaleYval.setSelectedItem("1.00");
    scaleYval.addActionListener(new ComboBoxListener());

    shearXval = new JComboBox(shearValues);
    shearXval.setSelectedItem("0.00");
    shearXval.addActionListener(new ComboBoxListener());
    shearYval = new JComboBox(shearValues);
    shearYval.setSelectedItem("0.00");
    shearYval.addActionListener(new ComboBoxListener());

    panel.add(new JLabel("Scale X value:"));
    panel.add(scaleXval);
    panel.add(new JLabel("Scale Y value:"));
    panel.add(scaleYval);
    panel.add(new JLabel("Shear X value:"));
    panel.add(shearXval);
    panel.add(new JLabel("Shear Y value:"));
    panel.add(shearYval);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(displayPanel.getWidth(), displayPanel.getHeight() 10);
    setVisible(true);
  }

  public static void main(String arg[]) {
    new AffineTransformApp();
  }

  class ComboBoxListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JComboBox temp = (JComboBoxe.getSource();

      if (temp == scaleXval) {
        displayPanel.scalex = Double.parseDouble((Stringtemp
            .getSelectedItem());
        displayPanel.applyValue(true, false);
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (temp == scaleYval) {
        displayPanel.scaley = Double.parseDouble((Stringtemp
            .getSelectedItem());
        displayPanel.applyValue(true, false);
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (temp == shearXval) {
        displayPanel.shearx = Double.parseDouble((Stringtemp
            .getSelectedItem());
        displayPanel.applyValue(false, true);
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (temp == shearYval) {
        displayPanel.sheary = Double.parseDouble((Stringtemp
            .getSelectedItem());
        displayPanel.applyValue(false, true);
        displayPanel.applyFilter();
        displayPanel.repaint();
      }
    }
  }
}

class DisplayPanel extends JLabel {
  Image displayImage;

  BufferedImage biSrc, biDest;

  BufferedImage bi;

  Graphics2D big;

  AffineTransform transform;

  double scalex = 1.0;

  double scaley = 1.0;

  double shearx = 1.0;

  double sheary = 1.0;

  DisplayPanel() {
    setBackground(Color.black);
    loadImage();
    setSize(displayImage.getWidth(this), displayImage.getWidth(this))// panel
    createBufferedImages();

    transform = new AffineTransform();
  }

  public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage(
        "largeJava2sLogo.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);
    try {
      mt.waitForAll();
    catch (Exception e) {
      System.out.println("Exception while loading.");
    }

    if (displayImage.getWidth(this== -1) {
      System.out.println(" Missing .jpg file");
      System.exit(0);
    }
  }

  public void createBufferedImages() {
    biSrc = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);

    big = biSrc.createGraphics();
    big.drawImage(displayImage, 00this);

    bi = biSrc;

    biDest = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
  }

  public void applyValue(boolean scale, boolean shear) {
    if (scale) {
      transform.setToScale(scalex, scaley);
      scale = false;
    else if (shear) {
      transform.setToShear(shearx, sheary);
      shear = false;
    }
  }

  public void applyFilter() {
    AffineTransformOp op = new AffineTransformOp(transform, null);
    Graphics2D biDestG2D = biDest.createGraphics();
    biDestG2D
        .clearRect(00, biDest.getWidth(this), biDest.getHeight(this));
    op.filter(biSrc, biDest);
    bi = biDest;
  }

  public void reset() {
    big.setColor(Color.black);
    big.clearRect(00, bi.getWidth(this), bi.getHeight(this));
    big.drawImage(displayImage, 00this);
  }

  public void update(Graphics g) {
    g.clearRect(00, getWidth(), getHeight());
    paintComponent(g);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2Dg;
    g2D.drawImage(bi, 00this);
  }
}

           
         
    
    
  
Related examples in the same category
1.Coordinate DemoCoordinate Demo
2.Rotation and coordinate translation
3.Scaling an object
4.Transform DemoTransform Demo
5.Transform ShearTransform Shear
6.Transform ScaleTransform Scale
7.Transform Rotation Translation Transform Rotation Translation
8.Transforme Rotation demoTransforme Rotation demo
9.Transform Translation and RotationTransform Translation and Rotation
10.Transform Translated RotationTransform Translated Rotation
11.Transform TranslationTransform Translation
12.Line transformation, rotation, shear,scale Line transformation, rotation, shear,scale
13.Scaling a Drawn Image
14.Shearing a Drawn Image
15.Translating a Drawn Image
16.Rotating a Drawn Image
17.Create an complex shape by rotating an ellipse.
18.Scaling a Shape with AffineTransform
19.Shearing a Shape with AffineTransform
20.Perform shearing: use share() method.
21.Translating a Shape with AffineTransform
22.Rotating a Shape with AffineTransform
23.Rotating image using Java 2D AffineTransform class
24.Rotates a shape about the specified coordinates.
25.Resizes or translates a Shape
26.Creates and returns a translated shape.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.