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();
    
}
   }

Recommended Answers

All 6 Replies

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

so does anyone know what im missing

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

Member Avatar for hfx642

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.