Java-旋转图像不断失败

问题描述 投票:0回答:2

我已经被这个问题困扰了几个小时了。我有一个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);

几乎在我的代码的每一个地方,但是没有任何效果。我也无法旋转整个Paint2D对象(称为g),因为它也会绘制其他图片。

有人知道吗?

谢谢!

java image rotation jpanel repaint
2个回答
1
投票
 Graphics2D g2d = (Graphics2D)BlankCanvas.getGraphics();

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

不必担心旋转/平移代码,只需使用Rotated Icon。然后,您可以使用paintIcon(...)方法绘制图标:

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

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);
        //rotating the image
        BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_INDEXED);
        AffineTransform at = new AffineTransform();
        at.translate((newWidth - image.getWidth()) / 2, (newHeight - image.getHeight()) / 2);
        int coordX = image.getWidth() / 2;
        int coordY = image.getHeight() / 2;
        at.rotate(rad, coordX, coordY);
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        rotated = scaleOp.filter(image, rotated);
        return rotated;
    }
© www.soinside.com 2019 - 2024. All rights reserved.