【问题标题】:Rotating BufferedImage instances旋转 BufferedImage 实例
【发布时间】:2011-02-07 06:04:41
【问题描述】:

我无法显示旋转的BufferedImage。我认为旋转工作得很好,但我实际上无法将它绘制到屏幕上。我的代码:

Class extends JPanel {
    BufferedImage img;
    int rotation = 0;

    public void paintComponent(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        img2d = img.createGraphics();
        img2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);
        g.drawImage(img, imgx, imgy, null);
        this.repaint();
    }
}

这对我不起作用。我找不到任何方法将旋转后的 img2d 绘制到 g 上。

编辑:我有多个对象被绘制到g,所以我不能旋转它。我需要能够单独旋转东西。

【问题讨论】:

    标签: java swing rotation bufferedimage graphics2d


    【解决方案1】:

    也许你应该尝试像这样使用AffineTransform

    AffineTransform transform = new AffineTransform();
    transform.rotate(radians, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    bufferedImage = op.filter(bufferedImage, null);
    

    希望这会有所帮助。

    【讨论】:

    • 这会扭曲文件,当它不是正方形时添加填充。
    【解决方案2】:

    我会使用Graphics2D.drawImage(image, affinetranform, imageobserver)

    下面的代码示例将图像旋转并平移到组件的中心。这是结果的截图:

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Test");
    
        frame.add(new JComponent() {
            BufferedImage image = ImageIO.read(
                    new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                // create the transform, note that the transformations happen
                // in reversed order (so check them backwards)
                AffineTransform at = new AffineTransform();
    
                // 4. translate it to the center of the component
                at.translate(getWidth() / 2, getHeight() / 2);
    
                // 3. do the actual rotation
                at.rotate(Math.PI / 4);
    
                // 2. just a scale because this image is big
                at.scale(0.5, 0.5);
    
                // 1. translate the object so that you rotate it around the 
                //    center (easier :))
                at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
    
                // draw the image
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image, at, null);
    
                // continue drawing other stuff (non-transformed)
                //...
            }
        });
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
    

    【讨论】:

      【解决方案3】:

      您正在旋转用于绘图的图形,而不是图像。这就是为什么你看不到任何效果。将旋转应用于您正在绘制的图形,它将绘制旋转的图像:

      public void paintComponent(Graphics g) {
          g.clearRect(0, 0, getWidth(), getHeight());
          g.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);
          g.drawImage(img, imgx, imgy, null);
          this.repaint();
      }
      

      这可能不会完全按照您的预期绘制,旋转将围绕坐标原点旋转。要使图像围绕其中心旋转,您需要在旋转之前应用坐标平移,例如:

      g.translate(imgx >> 1, imgy >> 1);
      

      Graphics2D Tutorial 有更多示例。

      【讨论】:

      • 我想单独旋转BufferedImage,然后用Graphics绘制。我在 Graphics 对象上还有其他不应该旋转的东西。
      • 绘制完图像后可以撤消Graphics的变换。
      • @Durandal 你确定Graphics 有旋转方法吗?还是你的意思是g2d.rotate()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多