我想知道是否有可能知道当我左键单击画布时是否有一个图像图标,这是必须能够处理这种情况的代码:
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") 或类似的东西。