Girando a imagem com o AffineTransform
Eu tenho classe chamadaAirplane
. Dentro desta classe eu tenho variávelimg
que é umBufferedImage
tipo. O que é mais eu tenho classeWorldMap
que substitui a funçãopaintComponent(Graphics g)
:
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null);
drawAirplanes(g2d);
}
FunçãodrawAirplanes()
parece com isso:
private void drawAirplane(Graphics2D g){
for(Samolot i: s){
i.rotateAirplane();
g.drawImage(i.getImg(),i.getX(),i.getY(),(int)i.getDim().getWidth(),(int)i.getDim().getHeight(), null);
}
}
Basta apenas 1) girar o avião (BufferedImage dentro do objeto Airplane) 2) desenhá-lo.
Minha função Airplane.rotateAirplane () se parece com isso:
public void rotateSamolot() {
AffineTransform tx = new AffineTransform();
tx.translate(10,10); //10, 10 is height and width of img divide by 2
tx.rotate(Math.PI / 2);
tx.translate(-10,-10);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage =new BufferedImage(20, 20, img.getType()); //20, 20 is a height and width of img ofc
op.filter(img, newImage);
this.img = newImage;
}
ofc quando estou executando meu programa apenasmapa
objeto é desenhado. quando estou a eliminar esta faixa
this.img = newImage;
Eu tenho ofc meu avião também, mas não rodado.