1

I usually asked these questions in Spanish Stackoverflow, but my question got no answer. So I'm gonna try on this site for the first time. I've got an issue when rotating an image on Java (The image is never painted, neither the first paintComponent(...) call).

img2 is painted, but img, which is the image I want to rotate doesn't. It's the first time that I work with BufferedImage. I've looked some examples, but maybe that's the issue.

I really don't know which is the error. My code is the following one:

@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);
    }
}

I've seen other questions in SO related to mine, but it seems that I'm doing it in a properly way. Maybe there's a problem when creating two instances of a graphics2D, I'm not sure. ¿Anyone can help me?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
deadlock
  • 164
  • 1
  • 14
  • What is the exception are you getting ? – Sambit May 13 '19 at 17:56
  • nothing is painted. – deadlock May 13 '19 at 17:56
  • 1
    Was it been painted before rotating it? For better help sooner post a proper [mcve]. We don't have access to your image, please take one of this [Q&A](https://stackoverflow.com/questions/19209650/example-images-for-code-and-mark-up-qas) and hotlink it in your app, take [this answer](https://stackoverflow.com/questions/10861852/add-a-complex-image-in-the-panel-with-buttons-around-it-in-one-customized-user/10862262#10862262) as an example. – Frakcool May 13 '19 at 18:04
  • 1
    Why do you have 2 Graphics2D references to the same object? – TvanB May 13 '19 at 18:09
  • I've include more explanation in the question. Answering to @TvanB, I only want to rotate img. If I included both images in the same g2d object, I believe that both images would be rotated. – deadlock May 13 '19 at 18:13
  • 1
    1) Why don't you try to draw the image **before** rotating it and see if that works, then you'll know if it's that you're not finding the image or what the error is. 2) Post a proper [mcve] and 3) Try [this answer](https://stackoverflow.com/a/30422313/2180785) and 4) Don't use the `back-button` of your browser to edit your question as it takes away edits made by others. – Frakcool May 13 '19 at 18:17
  • @Frakcoll I've tried (3) and it works. Thanks! – deadlock May 13 '19 at 18:34
  • I didn't get the notification. It is Frakcool* (double O, not double L). Then can we close this question as a duplicate? – Frakcool May 13 '19 at 18:44

0 Answers0