java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4096
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Transform Scale E-mail
User Rating: / 7
PoorBest 

Image

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 TransformScale extends JComponent {
  Shape axes, shape;

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

  public TransformScale() {
    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.
      g2.transform(AffineTransform.getScaleInstance(33));

    // Draw the "new" shapes in dashed.
      g2.transform(AffineTransform.getTranslateInstance(7575));
    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 TransformScale());
    f.setSize(650550);
    f.show();
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.