[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About
MyMovingShapes.java
/*
 * Author: Havard Rast Blok
 * E-mail: 
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;

/*
 * Draws a circle, polygon and rectangle using Graphics2D
 * and lets the user move these objects using the mouse.
 */
public class MyMovingShapes extends JFrame implements MouseInputListener
{
  Ellipse2D.Double circle;
  Polygon polygon;
  Rectangle rectangle;
  int xpoints[] {100150170160130};
  int ypoints[] {100,  80120150160};

  Point startDrag;
  boolean dragCircle, dragPolygon, dragRectangle;

  public MyMovingShapes()
  {
    super("MyMovingShapes");
    setDefaultCloseOperationJFrame.EXIT_ON_CLOSE );
    setSize(300300);
    addMouseListenerthis );
    addMouseMotionListenerthis );

    //init shapes
    circle = new   Ellipse2D.Double50504040 );
    polygon = new Polygonxpoints, ypoints, xpoints.length );
    rectangle = new Rectangle2002005050 );

    show();
  }

  public void paintGraphics g )
  {
    //use Graphics2D
    Graphics2D g2d = (Graphics2D)g;

    //clear screen
    g2d.setColorg2d.getBackground() );
    g2d.fillRect00, getWidth(), getHeight() );

    //draw the objects based on the contained values
    g2d.setColorColor.black );
    g2d.fillcircle );
    g2d.fillpolygon );
    g2d.fillrectangle );

  }

 //methods from MouseInputListener

  public void mouseClicked(MouseEvent e)
  {
    Point p;

    /* Note that if you move the mouse while clicking, 
     * it is not registered as a click but a drag.
     * To some users, this might seem a bit too sensitive.
     * So to solve this, move this code to the mouseReleased method.
     */

    //get the point that was clicked
    p = e.getPoint();

    //check which object was clicked
    //if-else is deliberately avoided, so overlapping figures may be detected.
    ifcircle.contains) )
    {
      System.out.println("The circle was hit");
    }

    ifpolygon.contains) )
    {
      System.out.println("The polygon was hit");
    }

    ifrectangle.contains) )
    {
      System.out.println("The rectangle was hit");
    }
    
  }


  public void mouseEntered(MouseEvent e)
  {
  }


  public void mouseExited(MouseEvent e)
  {
  }


  public void mousePressed(MouseEvent e)
  {
    Point p;

    //register start of drag
    System.out.println("Started drag");

    //get the point that was pressed
    p =  e.getPoint();
    startDrag = p;

    //check which object was clicked
    //if-else is deliberately avoided, so overlapping figures may be detected.
    ifcircle.contains) )
    {
      dragCircle = true;
    }

    ifpolygon.contains) )
    {
      dragPolygon = true;
    }

    ifrectangle.contains) )
    {
      dragRectangle = true;
    }
       

  }


  public void mouseReleased(MouseEvent e)
  {
    //end drag
    startDrag = null;
    dragCircle = dragPolygon = dragRectangle = false;
    System.out.println("End dragging");
  }


  public void mouseDragged(MouseEvent e)
  {
    Point p;
    int dX, dY;

    //get the point that was clicked
    p = e.getPoint();

    //ensure that we have a start dragging point
    //startDrag = p;

    //calculate difference between start and current drag poing
    dX = p.x - startDrag.x;
    dY = p.y - startDrag.y;
    
    //check which object(s) we are dragging and move them
    ifdragCircle )
    {
      circle.x += dX;
      circle.y += dY;
    }

    ifdragPolygon )
    {
      polygon.translatedX, dY );
    }

    ifdragRectangle )
    {
      rectangle.translatedX, dY );
    }

    //reset startdrag point
    startDrag = p;

    //redraw the screen
    repaint();
        
  }

  public void mouseMoved(MouseEvent e)
  {
  }

  public static void main(String args[])
  {
    new MyMovingShapes();
  }
}


site: Håvard Rast Blok
mail:
updated: 16 July 2010