Determining if my mouse is on a shape or not.

Greetings All,

I am writing a program where I am rendering varying geometric shapes on a jpanel. I want to be able to mouse over each shape, and keep track of whether I am on any particular shape so I can do something with the shape.

I have started going down the path of having a class that stores the actual pixels the shape encompasses. I suppose that will work, and am not ready to throw that approach away (although it seems a bit primitive), but before I reinvented the wheel, I was wondering if there might already be some code snippets demonstrating a strategy, not necessarily mine, that works (I would assume so). I have been unable to find anything on the web yet.

Can anyone help?

Thanks,

Richard

[747 byte] By [richardcromera] at [2007-11-26 23:46:56]
# 1
Try to use (new Area(yourShape)).contains(x,y) for that purpose.
AlexeyUshakova at 2007-7-11 15:20:47 > top of Java-index,Security,Cryptography...
# 2

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.util.ArrayList;

public class RolloverTest{

public static void main(String[] args){

new RolloverTest();

}

public RolloverTest(){

shapes = new ArrayList(5);

Rectangle rect1 = new Rectangle(10, 10, 40, 40);

shapes.add(new HotShape(rect1, mouseOff));

Rectangle rect2 = new Rectangle(40, 70, 40, 40);

shapes.add(new HotShape(rect2, mouseOff));

Line2D line1 = new Line2D.Double(100, 100, 200, 200);

shapes.add(new HotShape(line1, mouseOff));

Line2D line2 = new Line2D.Double(10, 300, 300, 10);

shapes.add(new HotShape(line2, mouseOff));

Ellipse2D oval1 = new Ellipse2D.Double(300, 100, 60, 60);

shapes.add(new HotShape(oval1, mouseOff));

hotPiece = (HotShape)shapes.get(0);

drawingBoard = new DrawingBoard(this);

frame = new JFrame("Rollover Test");

frame.setContentPane(drawingBoard);

frame.setSize(500, 500);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public class DrawingBoard extends JPanel implements MouseMotionListener{

public DrawingBoard(RolloverTest rt){

this.rt = rt;

this.setBackground(Color.WHITE);

this.addMouseMotionListener(this);

}

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D gfx = (Graphics2D)g;

gfx.setStroke(new BasicStroke(5));

for(int i = 0; i < rt.shapes.size(); i++){

HotShape shape = (HotShape)rt.shapes.get(i);

gfx.setColor(shape.color);

gfx.draw(shape.shape);

}

}

public void mouseDragged(MouseEvent evt){}

public void mouseMoved(MouseEvent evt){

Rectangle rect = new Rectangle(evt.getX(), evt.getY(), 1, 1);

for(int i = 0; i < rt.shapes.size(); i++){

HotShape shape = (HotShape)rt.shapes.get(i);

if(//shape.shape.contains((double)evt.getX(), (double)evt.getY())){

shape.shape.intersects(rect)){

rt.hotPiece.color = rt.mouseOff;

rt.hotPiece = shape;

shape.color = rt.mouseOn;

this.repaint();

}

}

}

RolloverTest rt;

}

public class HotShape{

public HotShape(Shape shape, Color color){

this.shape = shape;

this.color = color;

}

Shape shape;

Color color;

}

JFrame frame;

DrawingBoard drawingBoard;

ArrayList shapes;

HotShape hotPiece;

Color mouseOff = Color.BLUE;

Color mouseOn = Color.RED;

}

TuringPesta at 2007-7-11 15:20:47 > top of Java-index,Security,Cryptography...