Transform Demo : Transform « 2D Graphics GUI « Java






Transform Demo

Transform Demo
   
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransformDemo extends JPanel {
  public void init() {
    setBackground(Color.white);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect = new Rectangle(5,5,200,200);
    
    int w = getSize().width;
    int h = getSize().height;
    
    AffineTransform saveXform = g2.getTransform();
    AffineTransform toCenterAt = new AffineTransform();
    toCenterAt.translate(w / 2 - (rect.width / 2), h / 2 - (rect.height / 2));
    g2.transform(toCenterAt);
  
    g2.fill(rect);
    
    g2.transform(saveXform);
  }

  public static void main(String s[]) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    TransformDemo p = new TransformDemo();
    f.getContentPane().add("Center", p);
    p.init();
    f.pack();
    f.setSize(new Dimension(300, 300));
    f.show();
  }

}
           
         
    
    
  








Related examples in the same category

1.Coordinate DemoCoordinate Demo
2.Rotation and coordinate translation
3.Scaling an object
4.Transform ShearTransform Shear
5.Transform ScaleTransform Scale
6.Transform Rotation Translation Transform Rotation Translation
7.Transforme Rotation demoTransforme Rotation demo
8.Transform Translation and RotationTransform Translation and Rotation
9.Transform Translated RotationTransform Translated Rotation
10.Transform TranslationTransform Translation
11.Line transformation, rotation, shear,scale Line transformation, rotation, shear,scale
12.AffineTransform demoAffineTransform demo
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.