Reputation: 9
I want to add a mousse listener to this polygon. How can I add mouse listener to graphics Polygon
?
public class Domx extends JPanel{
Domx(){
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
this.setBackground(Color.white);
Polygon p4=new Polygon();
for (int i = -1; i < 2; i++) {
p4.addPoint((int) (X + i * P4[9] / 2), (int) (Y + P1[10] + P2[10] + P3[10] + P4[10]));
}
for (int i = 1; i > -2; i--) {
p4.addPoint((int) (X + i * P4[8] / 2), (int) (Y + P1[10] + P2[10] + P3[10]));
}
g2.fillPolygon(p4);
}
}
Upvotes: 0
Views: 121
Reputation: 168825
A MouseListener
is added to components, which a Polygon
is .. not.
Instead add the mouse listener to the component which displays it, then when a mouse action happens, check if polygon.contains(..)
before proceeding.
Upvotes: 1