1

キャンバスを左クリックしたときにその下に画像アイコンがあるかどうかを知ることができるかどうかを知りたかったのですが、これはそのケースを処理できる必要があるコードです:

 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") などのようなグラフィックス画像の上にマウスが押されていることを確認したいと思います。

4

1 に答える 1

1

たとえば、画像の位置を確認してみてください。画像の位置が (X=100, Y=100) で、幅と高さが 100 の場合、カーソルの現在の位置で確認できます。オブジェクトから X 位置、Y 位置、幅、高さを取得しImageIconます。お気に入り -

// imgX has the position of Image in X direction
// imgY has the position of Image in Y direction
// imgW has the width of image
// imgH has the height of image

これで確認できます-

if((startX <= imgX+imgW && startX >= imgX) && (startY <= imgY+imgH && startY >= imgH))
{
   //On the image
}
else
{
  //Out side of the image
}
于 2013-10-15T05:37:10.630 に答える