我希望绘制和重新绘制一个 JPanel,它可以与图像一起旋转 90 度。初始绘画不会引起任何问题。当我尝试重绘()面板时,它不会立即执行此操作,而是等待第二次重绘()。
我的第二个问题是,当我按照下面的代码中所述将面板垂直转动时,它根本不会重新绘制。我至少看不到红色、黄色或绿色的图像,但图像“光”仍然存在。
感谢您花时间阅读本文。
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(vertical){
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(-Math.PI / 2);
g2d.translate(-img_light.getWidth(null) / 2, -img_light.getHeight(null) / 2);
g2d.drawImage(img_light, 0, 0, this);
if(showRed){
g2d.translate(-img_red.getWidth(null) / 2, -img_red.getHeight(null) / 2);
g2d.drawImage(img_red, 0, 0, null);
}
if(showYellow){
g2d.translate(-img_yellow.getWidth(null) / 2, -img_yellow.getHeight(null) / 2);
g2d.drawImage(img_yellow, 0, 0, null);
}
if(showGreen){
g2d.translate(-img_green.getWidth(null) / 2, -img_green.getHeight(null) / 2);
g2d.drawImage(img_green, 0, 0, null);
}
}else{
g2d.drawImage(img_light, 0, 0, this);
if(showRed){
g2d.drawImage(img_red, 0, 0, null);
}
if(showYellow){
g2d.drawImage(img_yellow, 0, 0, null);
}
if(showGreen){
g2d.drawImage(img_green, 0, 0, null);
}
}
}