Mam taką łamigłówkę. Rysuje sobie kulki które poruszają się w jakimś oknie. Chciałbym teraz aby kliknięcie na kulkę spowodoało jakąś akcję ale jakoś mi nie wychodzi.
class Ball extends JComponent implements MouseListener {
Random ran = new Random();
private static final int XSIZE =35;
private static final int YSIZE =35;
private double x =ran.nextInt(450);
private double y =ran.nextInt(350);
private double dx =1;
private double dy =1;
Ball(){
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { log(e); }
public void mouseReleased(MouseEvent e){ }
//====================================================================== log
// Utility method to handle common logging action by all listeners.
private void log(MouseEvent e) {
System.out.println("---------");
}
public void mouseMoved(MouseEvent e) {
//log(e);
e.getX();
e.getY();
System.out.println("+");
//this.repaint(); // Show the change. Indirectly calls paintComponent.
}
public void mouseDragged(MouseEvent e) {
e.getPoint(); // Save the point for drawing.
//this.repaint(); // Show the change. Indirectly calls paintComponent.
}
public void move(Rectangle2D bounds ){
x +=dx;
y +=dy;
if(x<bounds.getMinX()){
x=bounds.getMinX();
dx = -dx;
}
if(x + XSIZE >= bounds.getMaxX()){
x=bounds.getMaxX() - XSIZE;
dx = -dx;
}
if(y < bounds.getMinY()){
y = bounds.getMinY();
dy = -dy;
}
if(y +YSIZE >= bounds.getMaxY()){
y = bounds.getMaxY()-YSIZE;
dy= -dy;
}
}
public Ellipse2D getShape(){
return new Ellipse2D.Double(x,y,XSIZE,YSIZE);
}
}
Wydaje mi się, że "Mysz nie widzi obszaru kulki". Może jakoś te kulki zastąpić obiektami typu button ??