2017-03-05 2 views
6

내가 그리기 전에 회전하는 이미지가 있습니다. 이미지는 육각형의 각도만큼 회전됩니다. 즉, 이미지는 기본적으로 육각형의 개별 가장자리를 "강조 표시"합니다. 마우스를이 회전 된 이미지 내부에서 클릭했는지 감지해야합니다. 회전되지 않은 이미지 내부에서 마우스 클릭을 감지하는 것은 간단하지만 회전 된 포인트 내에서 클릭을 감지하는 방법에 대해서는 알지 못합니다. 회전 후에 이미지 모서리의 점을 얻는 방법이 있나요? 그래서 이미지 위에 보이지 않는 다각형을 놓고 Polygon.contains()를 사용할 수 있습니까?Slick2D에서 회전 된 이미지의 클릭을 어떻게 감지합니까?

    Image highlightEdge = new Image("assets/img/highlightEdge.png"); 
        if(angle == 90){ 
         highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(testPoint.x - 56, testPoint.y); 
        } else if(angle == 210) { 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x - 72, lastSettlement.y - 32); 
        } else if(angle == 330){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x - 8, lastSettlement.y - 32); 
        } else if(angle == 30){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-8, lastSettlement.y); 
        } else if(angle == 150){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-72, lastSettlement.y); 
        } else { 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-40, lastSettlement.y - 48); 
        } 

답변

0

당신은 마우스 내부를 클릭 할 경우 감지하는 방법 contains를 사용하여 다음 정확히 Image의 모양과 일치하는 Shape 만들고 있었다.

Image의 회전을 고려하여 Shape에 해당 회전 Transform을 적용 할 수 있습니다.

이 작업을 수행하는 shapeFromImage 메서드를 만들었습니다. 그것은 Image 및 위치를 수신하고이 Shape 해당 반환

float positionX; 
float positionY; 

if (angle == 90) { 
    highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0); 
    highlightEdge.rotate(new Float(angle)); 

    positionX = testPoint.x - 56; 
    positionY = testPoint.y; 

    highlightEdge.draw(positionX, positionY); 
} 

... 

// you can now use this Shape to use its method "contains" 
imageShape = shapeFromImage(highlightEdge, positionX, positionY); 
:

이 예에서
/** 
* Returns the Shape of an Image considering its rotation 
* @param image 
* @param x the x position of the Image 
* @param y the y position of the Image 
*/ 
public static Shape shapeFromImage(Image image, float x, float y) { 

    // create a rectangle with same position and size of the image 
    Shape imageShape = new Rectangle(x, y, image.getWidth(), image.getHeight()); 

    // get the rotation angle of the image 
    float angle = image.getRotation(); 

    // if the image is rotated, we also need to rotate our shape 
    if (angle != 0.f) { 

     // convert the rotation angle in radians to use in Transform 
     float angleInRadians = (float) Math.toRadians(angle); 

     // get the point of rotation to use in Transform. 
     // image.getCenterOfRotation returns a point relative to the image. 
     // for Transform we need an absolute point, so we add the image position to it 
     float rotationX = image.getCenterOfRotationX() + x; 
     float rotationY = image.getCenterOfRotationY() + y; 

     // create the rotation Transform to match the image rotation 
     Transform rotationTransform = Transform.createRotateTransform(angleInRadians, rotationX, rotationY); 

     // apply the rotation Transform to our shape 
     imageShape = imageShape.transform(rotationTransform); 

    } 

    return imageShape; 
} 

이처럼 사용할 수

관련 문제