キャンバスを左クリックしたときにその下に画像アイコンがあるかどうかを知ることができるかどうかを知りたかったのですが、これはそのケースを処理できる必要があるコードです:
public void mousePressed(MouseEvent evt) {
if (!labelSelected){
// This is called by the system when the user presses the mouse button.
// Record the location at which the mouse was pressed. This location
// is one endpoint of the line that will be drawn when the mouse is
// released. This method is part of the MouseLister interface.
startX = evt.getX();
startY = evt.getY();
prevX = startX;
prevY = startY;
dragging = true;
Random ran = new Random();
int x = ran.nextInt(10);
currentColorIndex = x;
gc = getGraphics(); // Get a graphics context for use while drawing.
gc.setColor(colorList[currentColorIndex]);
gc.setXORMode(getBackground());
gc.drawLine(startX, startY, prevX, prevY);
}
}
しかし、線を描く前に、if (evt.getsource() == "Graphics ICON") などのようなグラフィックス画像の上にマウスが押されていることを確認したいと思います。