2017-03-05 5 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

आप एक Shape बिल्कुल Image के आकार से मेल करने के बना सकते हैं, और फिर पता लगाने के लिए अगर माउस के अंदर क्लिक किया गया था इसकी विधि contains का उपयोग करें।

Image के घूर्णन को ध्यान में रखते हुए आप Transform पर Shape पर एक संबंधित रोटेशन लागू कर सकते हैं।

मैंने विधि 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; 
} 

अपने उदाहरण में आप इसे इस तरह इस्तेमाल कर सकते हैं

संबंधित मुद्दे