954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Differentiate between mouseEvents of instances

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();
    
}
   }
Judas3213
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

is "Rectangle" a JPanel? I don't think in this section of the code is the error.

teo236
Junior Poster in Training
73 posts since Jul 2011
Reputation Points: 20
Solved Threads: 10
 

umm rectangle is just a class from the wheels package.
http://wps.aw.com/aw_sanders_oopjava_1/36/9390/2403932.cw/index.html

Judas3213
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

so does anyone know what im missing

Judas3213
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This is a hard one to work on because of the third party code.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: