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?