0

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 ...

nilux
  • 3
  • 3
  • Go with the time consuming route, because if you're using swing that's the correct way to do it. The graphics object returned by `getGraphics()` is not guaranteed to be useful in swing. – kiheru Aug 23 '13 at 10:12
  • Alright, but how to avoid the limitations set by the neatbeans ide (the window builder) ... ?! – nilux Aug 23 '13 at 10:22
  • Sorry, that's something I'm not familiar with. I'd be surprised if netbeans didn't allow you to do the drawing correctly though. – kiheru Aug 23 '13 at 10:35
  • Well, i just found an easy way to do it. It's a bit lame ... but .. I use a global boolean, which is initially false. When I get a value (heading, which I have to draw) I just call repaint() method of the jpanel. Since I've override already the paintComponent(), there I check if my global boolean is true. If it is, I get the value (from another global int or double whatever ...) and draw it :) – nilux Aug 23 '13 at 10:42
  • It "looks" like you are trying to effect the Graphicsc context of an existing componet by using getGraphics, this isn't going to work (as you seem to have found), check out [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for some more tips – MadProgrammer Aug 23 '13 at 10:53
  • You could start by taking a look at [this example](http://stackoverflow.com/questions/16865391/slow-movement-using-paintcomponent-method/16867943#16867943) – MadProgrammer Aug 23 '13 at 10:58
  • That's quite helpful, thank you, MadProgrammer. – nilux Aug 23 '13 at 11:17

1 Answers1

1

You can use a JLabel with a Rotated Icon.

camickr
  • 321,443
  • 19
  • 166
  • 288