旋转图像时出现的问题

时间:2019-05-13 17:54:51

标签: java swing user-interface image-rotation

我通常在西班牙语Stackoverflow中问这些问题,但是我的问题没有答案。因此,我将第一次在此网站上尝试。在Java上旋转图像时遇到一个问题(该图像从不绘制,也没有第一个paintComponent(...)调用)。

img2已绘制,但我想旋转的图像img未绘制。这是我第一次与BufferedImage合作。我看了一些示例,但这也许就是问题所在。

我真的不知道哪个是错误。我的代码如下:

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g;
    Graphics2D g2d1 = (Graphics2D) g;
    Image img2 = new ImageIcon(getClass().getResource("/imagenes/fondoMapaPirata(Prueba).jpg")).getImage();
    g2d.drawImage(img2, 30, 30, this); 
    BufferedImage img;
    try {
        img = ImageIO.read(new URL("/imagenes/canon(Prueba).jpeg"));
        g2d1.rotate(alfa);
        double locationX = img.getWidth(this) / 2;
        double locationY = img.getHeight(this) / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(alfa, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        g2d1.drawImage(op.filter(img, null), 335, 730, this); 
    } catch (MalformedURLException ex) {
        Logger.getLogger(PanelCanon.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PanelCanon.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我在SO中也看到了其他与我有关的问题,但似乎我正在以适当的方式来做。我不确定在创建graphics2D的两个实例时会出现问题。 ¿有人可以帮助我吗?

0 个答案:

没有答案