0
我正在研究我的大学项目......我有一个jlabel的旋转问题。我想旋转的JLabel是一艘船。标签应该旋转,取决于船的航向。我的标题显示在单独的jpanel上 - 我在启动时绘制模拟控件,稍后使用getGraphics()绘制控件的“手形”(或箭头或其他)。下面是一些代码:java swing - JLabel不旋转
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 = 7.2的NetBeans。我读过的getGraphics不应该使用,但是...我认为这是为时已晚,这样样的变化,该项目是太大..和netbeans提出一些限制,当编辑initComponents()..
问题是:标签不旋转!第一 - 为什么它不旋转,以及如何旋转它(我想留在getGraphics,这将是耗时重新重建我的项目重写paintComponent方法等...
使用耗时的路由,因为如果你使用的是swing,这是正确的方法,getGraphics()返回的图形对象不能保证有用 – kiheru
好吧,但是如何避免neatbeans ide(窗口生成器)设置的限制......? – nilux
对不起,这是我不熟悉的东西,如果netbeans不允许你正确地进行绘图。 – kiheru