gpt4 book ai didi

java swing - JLabel不旋转

转载 作者:行者123 更新时间:2023-12-01 14:12:25 25 4
gpt4 key购买 nike

我正在开展我的大学项目...我遇到 jlabel 轮换问题。我想要旋转的 JLabel 是一艘船。标签应根据船的航向旋转。我将标题显示在单独的 jpanel 上 - 我在启动时绘制模拟控件,稍后使用 getGraphics() 绘制控件的“手”(或箭头或其他)。这是一些代码:

public void drawHeading (int getheading) {
int course = getheading;
int x,y;
double radians;
// appendEvent (" heading " + Integer.toString(course));

if (course != oldHeading){
//HeadingControl is the jpanel where I draw the analogue heading control
HeadingControl.validate();
HeadingControl.repaint();
}
oldHeading = course;
radians = Math.toRadians(course) - Math.PI/2;
//this puts info in textfield
appendEvent (" course " + Integer.toString(course));

x = 120 + (int)(70*Math.cos(radians));
y = 80 + (int)(70*Math.sin(radians));
//i get the graphics .. then add the "hand"
Graphics2D gfx = (Graphics2D) HeadingControl.getGraphics();
gfx.setColor(Color.red);
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

gfx.drawLine(x, y, 120, 80);
gfx.drawLine(x, y, 120, 80);
gfx.drawLine(x, y, 120, 80);

AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(90));
//the label is not rotated, tried ship.rotate(radians) (gfx2.rotate(radians) ... //didn't work
Graphics2D gfx2 = (Graphics2D) ship.getGraphics();
gfx2.transform(tx);

}

IDE = NetBeans 7.2 ..我读到不应使用 getGraphics,但是...我认为对于此类更改来说已经太晚了,该项目太大了..并且 netbeans 在使用时设置了一些限制开始编辑 initComponents() ..

问题是:标签不旋转!!! 1 - 为什么它不旋转,以及如何旋转它(我想继续使用 getGraphics,使用重写的 PaintComponent 方法等重新构建我的项目将非常耗时......

最佳答案

您可以将 JLabel 与 Rotated Icon 一起使用.

关于java swing - JLabel不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com