Java - 旋转图像不断失败

时间:2015-03-14 15:39:38

标签: java image rotation jpanel repaint

我几个小时以来一直坚持这个问题。我有一个JPanel,我画了几张照片。

这是一款2D游戏,我只需使用repaint();就可以将所有内容打印到屏幕上。

在某些时候,我想画一个男人开枪。到目前为止一切正常,但只有当男人看向北方时,因为我的动画图片都被吸引到了北方。手动旋转它们并将它们粘贴到我的项目之后会使它太远,所以我决定旋转图像使其在任何方向都可用,但我无法弄清楚如何做到这一点。这是一些代码:

此方法中的简单开关检查当前的面向方向。请注意,第一种情况(NORTH)工作正常,因为默认情况下将图片绘制到北方。

switch(CharDirection){
        case NORTH:
            if(PistolAnim == 0){
            try {
                Man = ImageIO.read(ManNorthURL);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }else{
                try{
                    if(PistolAnim == 1){
                    Man = ImageIO.read(Shoot1URL);
                    PistolAnim = 2;
                    return;
                    }
                    if(PistolAnim == 2){
                    Man = ImageIO.read(Shoot2URL);
                    PistolAnim = 3;
                    return;
                    }
                    if(PistolAnim ==3){
                    Man = ImageIO.read(Shoot3URL);
                    PistolAnim = 0;
                    return;
                    }
                } catch (IOException e){

                }
            }
            break;
        case EAST:
            if(PistolAnim == 0){
                try {
                    Man = ImageIO.read(ManEastURL);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }else{
                    try{
                        if(PistolAnim == 1){    
                        Man = ImageIO.read(Shoot1URL);
                        PistolAnim = 2;
                        return;
                        }
                        if(PistolAnim == 2){
                        Man = ImageIO.read(Shoot2URL);
                        PistolAnim = 3;
                        return;
                        }
                        if(PistolAnim ==3){
                        Man = ImageIO.read(Shoot3URL);
                        PistolAnim = 0;
                        return;
                        }
                    } catch (IOException e){

                    }
                }
            break;

整数变量“PistolAnim”用于显示动画的状态。 0 - 没有动画正在进行中 1 - 图片1

2 - 图片2

3 - 图3

绘画功能(以及重要部分)如下所示:

g.drawImage(Man, CharX, CharY, Man.getWidth(), Man.getHeight(), null);

我尝试使用以下旋转方法:

public void rotate(double Degrees, BufferedImage img1){
        ImageIcon icon = new ImageIcon(img1);
        BufferedImage BlankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D)BlankCanvas.getGraphics();
        g2d.rotate(Math.toRadians(Degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);



        g2d.drawImage(img1, 100, 100, null);
        img1 = BlankCanvas;
    }

我在互联网上找到了它并且它在我的一些测试中起作用,但现在它没有做它应该做的事情。我插了一行

rotation(90, Man);

几乎我的代码的每一点,但没有任何作用。我也无法在paint中旋转整个Graphics2D对象(也称为g),因为它也会绘制其他图片。

有人有个主意吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

 Graphics2D g2d = (Graphics2D)BlankCanvas.getGraphics();

这段代码错了。您不应该使用getGraphics()方法。您应该在JPanel的paintComponent()方法中进行自定义绘制,并使用传递给此方法的Graphics对象。然后,当您调用" rotate(...)`方法时,将此Graphics对象传递给方法并使用它来绘制旋转的图像。

您可以使用Rotated Icon,而不必担心轮换/翻译代码。然后,您可以使用paintIcon(...)方法绘制图标:

RotatedIcon rotated = new RotatedIcon(icon, degrees);
rotated.paintIcon(...) 

答案 1 :(得分:0)

以下方法会将BufferedImage旋转到您指定的特定角度。您可以使用此方法旋转图像。

//method for rotating the image to a specific angle 
    public BufferedImage rotateImage(BufferedImage image, double angle) {
        //calculate the new size of the rotated image
        double rad = Math.toRadians(angle);
        double sin = Math.abs(Math.sin(rad));
        double cos = Math.abs(Math.cos(rad));
        int newWidth = (int) Math.floor(image.getWidth() * cos + image.getHeight() * sin);
        int newHeight = (int) Math.floor(image.getHeight() * cos + image.getWidth() * sin);
        //createding a new bufferedimage with size that will have rotated image
        BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_INDEXED);
        //we set the information of how much will the image be 
       //displaced in x an y axis during the transform operation
        AffineTransform at = new AffineTransform();
        at.translate((newWidth - image.getWidth()) / 2, (newHeight - image.getHeight()) / 2);
        //calculate the center of transformation
        int coordX = image.getWidth() / 2;
        int coordY = image.getHeight() / 2;
        //setting the center and angle information for rotate operation 
        at.rotate(rad, coordX, coordY);
        //executing the rotate operation
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        rotated = scaleOp.filter(image, rotated);
        return rotated;
    }

将图像旋转到一定角度后,您可以绘制图像。

BufferedImage rotatedImage = rotateImage(image,90);
//draw image