I have three classes. I have a main app, a Draggable class, and a creator class.
I want it so that when i click on one rectangle it produces the same exact dimensions as the shape i pressed. However, the problem is that when I use mousePressed, it seems that I can't differentiate between the two rectangles. Click on one or the other produces the same result, but I want them to be different. I'm not sure on how to go about doing this.

MyApp.java

import wheels.users.*;
import java.awt.Color;


public class MyApp extends Frame {


private Creator _brick1, _brick2;


public MyApp() {

	
	_brick1 = new Creator(410,0);
	_brick1.setSize(30,60);
	_brick1.setColor(Color.BLACK);
	
	_brick2 = new Creator(448,0);
	_brick2.setSize(90,80);
	_brick2.setColor(Color.PINK);
	}
	
	

	public static void main(String [] args) {
	    MyApp app = new MyApp();
	}
}

Creator.java

import wheels.users.*;
import java.awt.Color;
import java.awt.event.*;


public class Creator extends Draggable {
private Creator _brick1, _brick2;



    public Creator (int x, int y) {
    	
      super(x, y);
      
    }



  public void mousePressed(MouseEvent e){
  	
  	   super.mousePressed(e);
  	   this.setColor(Color.blue);
  	 _brick1 = new Creator(0,0);
   
          
      }

  public void mouseReleased(MouseEvent e){
  	
      this.setColor(Color.black);
      
      }

}


Draggable.java

import wheels.users.*;
import java.awt.event.*;


public class Draggable extends Rectangle {
private java.awt.Point _lastMousePosition;
  

public void mouseDragged(MouseEvent e) {
	
java.awt.Point currentPoint = e.getPoint();

int dx = currentPoint.x-_lastMousePosition.x;
int dy = currentPoint.y-_lastMousePosition.y;
this.setLocation(this.getXLocation()+dx,this.getYLocation()+dy);
_lastMousePosition = currentPoint;

}
 

public Draggable(int x, int y) {
	
    super(x, y);
     
    }


public void mousePressed(java.awt.event.MouseEvent e){ 
	
    _lastMousePosition =  e.getPoint();

    }

public void mouseReleased(java.awt.event.MouseEvent e){ 
	
    _lastMousePosition =  e.getPoint();
    
}
   }

Edited 5 Years Ago by Judas3213: n/a

A rectangle is a graphic, and not an object.
You can't "click" on it.
You can store your coordinates of the rectangle, as a polygon, and test to see if you "click" inside of the polygon.

You don' know what package these classes come from. Look at the import statements.

Whoever wrote the wheels package didn't do anyone a favor by using class names that are the same as the Java SE class names. Download the zip file and see all the classes with names the same as the java SE classes.

This article has been dead for over six months. Start a new discussion instead.