Friday, 23 August 2013

java swing - JLabel not rotating

java swing - JLabel not rotating

I'm working on my university project ... I have an issue with rotation of
a jlabel. The JLabel I'd like to rotate is a ship. The label should
rotate, depending on the heading of the ship. I have the heading displayed
on a separate jpanel - I'm drawing the analogue control on startup, the
"hand" (or the arrow or whatever) of the control is drawn later with
getGraphics(). Here is some code:
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 .. I've read that getGraphics shouldn't be used, but
... I think it's too late for such kind of changes, the project is too big
.. and netbeans puts some limitations when it comes to editing
initComponents() ..
The issue is: The label is not rotating !!! 1st - Why isn't it rotating,
and how to rotate it (I'd like to stay with getGraphics, it will be to
time consuming to rebuild my project all over again with overriding
paintComponent method etc ...

No comments:

Post a Comment