Line transformation, rotation, shear,scale : Transform « 2D Graphics GUI « Java






Line transformation, rotation, shear,scale

Line transformation, rotation, shear,scale
   

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class House extends JPanel {
  MyCanvas canvas;

  JSlider sliderTransX, sliderTransY, sliderRotateTheta, sliderRotateX,
      sliderRotateY, sliderScaleX, sliderScaleY, sliderWidth;

  double transX = 0.0;

  double transY = 0.0;

  double rotateTheta = 0.0;

  double rotateX = 150.0;

  double rotateY = 150.0;

  double scaleX = 1.0;

  double scaleY = 1.0;

  float width = 1.0f;

  public House() {
    super(new BorderLayout());
    JPanel controlPanel = new JPanel(new GridLayout(3, 3));
    add(controlPanel, BorderLayout.NORTH);

    controlPanel.add(new JLabel("Translate(dx,dy): "));

    sliderTransX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
        100, 50);
    sliderTransY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
        100, 50);

    // To control rotation
    controlPanel.add(new JLabel("Rotate(Theta,ox,oy): "));
    sliderRotateTheta = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 360,
        0, 90, 45);

    JPanel subPanel = new JPanel();
    subPanel.setLayout(new GridLayout(1, 2));

    sliderRotateX = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
        150, 50);

    sliderRotateY = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
        150, 50);
    controlPanel.add(subPanel);

    // To control scaling
    controlPanel.add(new JLabel("Scale(sx,sy)x10E-2:"));

    sliderScaleX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
        100, 10);

    sliderScaleY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
        100, 10);

    // To control width of line segments
    JLabel label4 = new JLabel("Width Control:", JLabel.RIGHT);
    sliderWidth = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
    sliderWidth.setPaintTicks(true);
    sliderWidth.setMajorTickSpacing(5);
    sliderWidth.setMinorTickSpacing(1);
    sliderWidth.setPaintLabels(true);
    sliderWidth.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        width = sliderWidth.getValue();
        canvas.repaint();
      }
    });
    JPanel widthPanel = new JPanel();
    widthPanel.setLayout(new GridLayout(1, 2));
    widthPanel.add(label4);
    widthPanel.add(sliderWidth);
    add(widthPanel, BorderLayout.SOUTH);

    canvas = new MyCanvas();
    add(canvas, "Center");
  }

  public JSlider setSlider(JPanel panel, int orientation, int minimumValue,
      int maximumValue, int initValue, int majorTickSpacing,
      int minorTickSpacing) {
    JSlider slider = new JSlider(orientation, minimumValue, maximumValue,
        initValue);
    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(majorTickSpacing);
    slider.setMinorTickSpacing(minorTickSpacing);
    slider.setPaintLabels(true);
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider tempSlider = (JSlider) e.getSource();

        if (tempSlider.equals(sliderTransX)) {
          transX = sliderTransX.getValue() - 150.0;
          canvas.repaint();
        } else if (tempSlider.equals(sliderTransY)) {
          transY = sliderTransY.getValue() - 150.0;
          canvas.repaint();
        } else if (tempSlider.equals(sliderRotateTheta)) {
          rotateTheta = sliderRotateTheta.getValue() * Math.PI / 180;
          canvas.repaint();
        } else if (tempSlider.equals(sliderRotateX)) {
          rotateX = sliderRotateX.getValue();
          canvas.repaint();
        } else if (tempSlider.equals(sliderRotateY)) {
          rotateY = sliderRotateY.getValue();
          canvas.repaint();
        } else if (tempSlider.equals(sliderScaleX)) {
          if (sliderScaleX.getValue() != 0.0) {
            scaleX = sliderScaleX.getValue() / 100.0;
            canvas.repaint();
          }
        } else if (tempSlider.equals(sliderScaleY)) {
          if (sliderScaleY.getValue() != 0.0) {
            scaleY = sliderScaleY.getValue() / 100.0;
            canvas.repaint();
          }
        }
      }
    });
    panel.add(slider);

    return slider;
  }

  class MyCanvas extends Canvas {
    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;

      g2D.translate(transX, transY);
      g2D.rotate(rotateTheta, rotateX, rotateY);
      g2D.scale(scaleX, scaleY);

      BasicStroke stroke = new BasicStroke(width);
      g2D.setStroke(stroke);

      drawHome(g2D);
    }

    public void drawHome(Graphics2D g2D) {
      Line2D line1 = new Line2D.Float(100f, 200f, 200f, 200f);
      Line2D line2 = new Line2D.Float(100f, 200f, 100f, 100f);
      Line2D line3 = new Line2D.Float(100f, 100f, 150f, 50f);
      Line2D line4 = new Line2D.Float(150f, 50f, 200f, 100f);
      Line2D line5 = new Line2D.Float(200f, 100f, 200f, 200f);
      Line2D line6 = new Line2D.Float(140f, 200f, 140f, 150f);
      Line2D line7 = new Line2D.Float(140f, 150f, 160f, 150f);
      Line2D line8 = new Line2D.Float(160f, 150f, 160f, 200f);

      g2D.draw(line1);
      g2D.draw(line2);
      g2D.draw(line3);
      g2D.draw(line4);
      g2D.draw(line5);
      g2D.draw(line6);
      g2D.draw(line7);
      g2D.draw(line8);
    }
  }

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.getContentPane().add(new House());
    f.setDefaultCloseOperation(1);
    f.setSize(700, 550);
    f.setVisible(true);
  }
}

           
         
    
    
  








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