Resize a shape : Shape « 2D Graphics « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » 2D Graphics » Shape 
16.17.10.Resize a shapePrevious/Next
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

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

public class ResizeRectangle extends JPanel {
  private int SIZE = 8;
  private Rectangle2D[] points = new Rectangle2D.Double(5050,SIZE, SIZE)new Rectangle2D.Double(150100,SIZE, SIZE) };
  Rectangle2D s = new Rectangle2D.Double();

  ShapeResizeHandler ada = new ShapeResizeHandler();

  public ResizeRectangle() {
    addMouseListener(ada);
    addMouseMotionListener(ada);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2Dg;

    for (int i = 0; i < points.length; i++) {
      g2.fill(points[i]);
    }
    s.setRect(points[0].getCenterX(), points[0].getCenterY(),
        Math.abs(points[1].getCenterX()-points[0].getCenterX()),
        Math.abs(points[1].getCenterY()- points[0].getCenterY()));

    g2.draw(s);
  }

  class ShapeResizeHandler extends MouseAdapter {
    Rectangle2D r = new Rectangle2D.Double(0,0,SIZE,SIZE);
    private int pos = -1;
    public void mousePressed(MouseEvent event) {
      Point p = event.getPoint();

      for (int i = 0; i < points.length; i++) {
        if (points[i].contains(p)) {
          pos = i;
          return;
        }
      }
    }

    public void mouseReleased(MouseEvent event) {
      pos = -1;
    }

    public void mouseDragged(MouseEvent event) {
      if (pos == -1)
        return;

      points[pos].setRect(event.getPoint().x,event.getPoint().y,points[pos].getWidth(),
          points[pos].getHeight());
      repaint();
    }
  }

  public static void main(String[] args) {

    JFrame frame = new JFrame("Resize Rectangle");

    frame.add(new ResizeRectangle());
    frame.setSize(300300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}
16.17.Shape
16.17.1.Creating Basic Shapes
16.17.2.Creating a Shape Using Lines and Curves
16.17.3.Combining Shapes
16.17.4.Scaling a Shape
16.17.5.Shearing a Shape
16.17.6.Translating a Shape
16.17.7.Rotating a Shape
16.17.8.Add Round Rectangle, Ellipse Arc to a shapeAdd Round Rectangle, Ellipse Arc to a shape
16.17.9.GlyphVector.getNumGlyphs()
16.17.10.Resize a shape
16.17.11.Creates a diagonal cross shape.
16.17.12.Creates a diamond shape.
16.17.13.Creates a triangle shape that points downwards.
16.17.14.Creates a triangle shape that points upwards.
16.17.15.Draws a shape with the specified rotation about (x, y).
16.17.16.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.