AffineTransform demo : Transform « 2D Graphics GUI « Java

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




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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.