Scaling an object : Transform « 2D Graphics GUI « Java






Scaling an object

   

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

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

public class Scale extends JPanel {

  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(new Color(150, 150, 150));
    g2d.fillRect(0, 0, 80, 50);

    AffineTransform tx1 = new AffineTransform();
    tx1.translate(110, 20);
    tx1.scale(0.5, 0.5);

    g2d.setTransform(tx1);
    g2d.fillRect(0, 0, 80, 50);

    AffineTransform tx2 = new AffineTransform();
    tx2.translate(200, 20);
    tx2.scale(1.5, 1.5);

    g2d.setTransform(tx2);
    g2d.fillRect(0, 0, 80, 50);

  }

  public static void main(String[] args) {

    JFrame frame = new JFrame("Scaling");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Scale());
    frame.setSize(330, 160);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

   
    
    
  








Related examples in the same category

1.Coordinate DemoCoordinate Demo
2.Rotation and coordinate translation
3.Transform DemoTransform Demo
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.