Mouseclick on unit rectangle wrong

helo,

i am trying to put in a targeting system to target an enemy from my hero and i have a rectangle behind the enemy so i call mouseclicked


	// user release mouse buton - mouse released method
	@Override
	public void mouseReleased(MouseEvent e) {
		// caled when mouse buton released
		if (e.getButton() == 3) {
			// check where click was example: left top from the center of wooyl along with sub quadrent and move unit
				wooyl = detectquadrent(e, wooyl);
				Rectangle click = new Rectangle(e.getX(), e.getY(), 1, 1);
				if (wolftargetcheck(click)) {
					resetwolftarget();
				}
			}

and the method is


	/** scan wolf list to see if any wolf are intersecting wooyl click location */
private boolean wolftargetcheck(Rectangle xy) {
	boolean noclick = true;
	Iterator<unit> wolfiteratorfive = wolf.values().iterator();
	while(wolfiteratorfive.hasNext()) {
		unit wolfobjectfive = wolfiteratorfive.next();
		if (xy.intersects(wolfobjectfive.unitrectangle)) {
			//System.out.println("working");
			wolfobjectfive.targeted = true;
			noclick = false;
			break;
		}
	}
	return noclick;
}

and i have set the enemy background rectangle to the displayed image location, and width excetera

				wolfdeathsouthernwestern = new ImageIcon("libbisouthernwesterndeath2.gif");
				curentimage = wolfeastern;
				curentanimated = wolfmovingwestern;
				unitrectangle = new Rectangle(x.intValue(), y.intValue(), curentimage.getWidth(), curentimage.getHeight());

and that is updated all the time in paintcomponent when it changes


			if (wolfobject.facing.equals("southernwestern")) {
				g.drawImage(wolfobject.wolfsouthernwestern, wolfobject.x.intValue(), wolfobject.y.intValue(), this);
				wolfobject.curentimage = wolfobject.wolfsouthernwestern;
			}
			wolfobject.unitrectangle = new Rectangle(wolfobject.x.intValue(), wolfobject.y.intValue(), wolfobject.curentimage.getWidth(),
					wolfobject.curentimage.getHeight());

and i am displaying the rectangle and it look good, the unit rectangle so i do not understand when i click on the wolf toward the botom it does not target then when i click above the wolf it targets

the workspace project is at:

and the main clas is roleplayinggamehorn.java

it is for 1366 / 768 resolution, window

i could not even get this code to work

package test;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

public class Click extends JPanel implements MouseListener{
	Rectangle rectangle;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Click();
	}
	public Click() {
		rectangle = new Rectangle(50, 50, 100, 100);
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 400);
		frame.setVisible(true);
		frame.add(this);
		frame.addMouseListener(this);
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
		repaint();
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		Rectangle point = new Rectangle(e.getLocationOnScreen().x, e.getLocationOnScreen().y, 1, 1);
		if (point.intersects(rectangle)) {
			System.out.println("click intersecting rectangle");
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

Try to add the mouse listener to JPanel, not JFrame.

i did it and it is stil giving me trouble

This should work. I set the size of panel instead of the frame and called pack method of frame to fit the panel.

public Click() {
    rectangle = new Rectangle(50, 50, 100, 100);
    
    setSize(600, 400);
    addMouseListener(this);
    
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(this);
    frame.pack();
    frame.setVisible(true);
}

hmmm i tried it and it stil was doing the same thing. my computer is kinda cheap maybe it is not made for development it is weird like it does not record the mouse during screnshot or video

i got it working in my game: Nathan tower defense beta year ago with the tower purchase

it look like i used rectangle two dimensional but that is not working either

I’ve been hesitating to try and help out because I use JavaFX for GUI work and haven’t done much with Swing for many years. Once JavaFX is set up, I think it is easier to work with, but the initial setup is a pain. Anyway, here are some observations on the issue you are having with the mouse click’s location.

The method getLocationOnScreen() is returning [x,y] based on your monitor’s screen, not on the containing Frame!

Use getX() and getY() is a step in the right direction, but the location returned is still off in the Y axis due to the area consumed by the [title bar area] (my old brain is forgetting the term for this header/title area). This top area consumes Y pixel location 0 to 37. I don’t know how to delete this part of a JPanel – maybe someone else does?

To deal with it, I think I used to add or subtract 38 to correct Y-axis location calculations. This is annoying, for sure. There should be a way to avoid this, but my recollection is that when I was doing things with Swing, this was a headache.

If you put this in your constructor you will see that the actual bounds of the JPanel is smaller than it’s defined size:

System.out.println("JPanel bounds: " + getBounds());

Check this out for your mouseRelease:

@Override
public void mouseReleased(MouseEvent e) {
	Point2D point = new Point2D.Double(e.getX(), e.getY());
	System.out.println("Click point: [" + e.getX() + ", " + e.getY() + "]");
	System.out.println("Click point: [" + e.getLocationOnScreen().x 
			+ ", " + e.getLocationOnScreen().y + "]");
	
	point = new Point2D.Double(point.getX(), point.getY() - 38);
	
	if (rectangle.contains(point)) {
		 System.out.println("click intersecting rectangle");
	}
}

Good job adding the “Click” example to your question. It makes it much easier for people to help if they have a really simple case that isolates the issue.

ah yes the frame fix i just measured it i gues i mismeasured. taking 31 pixel off the y value is working for me.

thank you so much!