Transform Rotation Translation : 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 




Transform Rotation Translation
Transform Rotation Translation
   

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class TransformersRotationTranslation extends JComponent {
  Shape axes, shape;

  int length = 54, arrowLength = 4, tickSize = 4;

  public TransformersRotationTranslation() {
    axes = createAxes();
    shape = createShape();
  }

  protected Shape createAxes() {
    GeneralPath path = new GeneralPath();

    // Axes.
    path.moveTo(-length, 0);
    path.lineTo(length, 0);
    path.moveTo(0, -length);
    path.lineTo(0, length);
    // Arrows.
    path.moveTo(length - arrowLength, -arrowLength);
    path.lineTo(length, 0);
    path.lineTo(length - arrowLength, arrowLength);
    path.moveTo(-arrowLength, length - arrowLength);
    path.lineTo(0, length);
    path.lineTo(arrowLength, length - arrowLength);
    // Half-centimeter tick marks
    float cm = 72 2.54f;
    float lengthCentimeter = length / cm;
    for (float i = 0.5f; i < lengthCentimeter; i += 1.0f) {
      float tick = i * cm;
      path.moveTo(tick, -tickSize / 2);
      path.lineTo(tick, tickSize / 2);
      path.moveTo(-tick, -tickSize / 2);
      path.lineTo(-tick, tickSize / 2);
      path.moveTo(-tickSize / 2, tick);
      path.lineTo(tickSize / 2, tick);
      path.moveTo(-tickSize / 2, -tick);
      path.lineTo(tickSize / 2, -tick);
    }
    // Full-centimeter tick marks
    for (float i = 1.0f; i < lengthCentimeter; i += 1.0f) {
      float tick = i * cm;
      path.moveTo(tick, -tickSize);
      path.lineTo(tick, tickSize);
      path.moveTo(-tick, -tickSize);
      path.lineTo(-tick, tickSize);
      path.moveTo(-tickSize, tick);
      path.lineTo(tickSize, tick);
      path.moveTo(-tickSize, -tick);
      path.lineTo(tickSize, -tick);
    }
    return path;
  }

  protected Shape createShape() {
    float cm = 72 2.54f;
    return new Rectangle2D.Float(cm, cm, * cm, cm);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2Dg;

    // Use antialiasing.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    // Move the origin to 75, 75.
    AffineTransform at = AffineTransform.getTranslateInstance(7575);
    g2.transform(at);

    // Draw the shapes in their original locations.
    g2.setPaint(Color.black);
    g2.draw(axes);
    g2.draw(shape);

    // Transform the Graphics2D.
      AffineTransform rat = new AffineTransform();
      rat.setToRotation(Math.PI / 6);
      rat.translate(100100);
    
    g2.transform(rat);

    // Draw the "new" shapes in dashed.
    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
        BasicStroke.JOIN_BEVEL, 0new float[] { 3}0);
    g2.setStroke(stroke);
    g2.draw(axes);
    g2.draw(shape);
  }

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.getContentPane().add(new TransformersRotationTranslation());
    f.setSize(350450);
    f.show();
  }
}

           
         
    
    
  














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.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.
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.