0

i have an extended JLabel class, called OrientationLabel, holding a single character. That character is '>' and inside my extended class is a function called flip which basically changes the character from '>' to '<'. anyway what i want to do is rotate the label in order for it to align with an edge.

Note: I also have an edge class which consists of two Point objects. inside that class is a function called center() which returns a computed center Point object in between the two points that form the edge. and also an angle() function that computes the angle that the edge makes.

i pass the angle and center point to my constructor of my orientation label and here is my resulting paintComponent function:

@Override
public void paintComponent(Graphics g){

    Graphics2D g2 = (Graphics2D)g;

    this.setLocation(Math.round(center.getX() - (getWidth()/2)), Math.round(center.getY() - (getHeight()/2)));
    g2.rotate(-rotation, getWidth() / 2, getHeight() / 2);

    super.paintComponent(g);

}

The problem is that as i rotate the label, sometimes it does not align exactly with the line. It points along the direction ok and the flip() function works perfectly but like i said sometimes you can tell its not perfectly aligned. Also i feel it sometimes comes out a little skewed.

I'm guessing this has something to do with the rotational center. If the width of the character is an odd number then the center will be a pixel rather than half a pixel?

Keep in mind i have been using java for a very long time in uni however we never really explored using the Graphics/Graphics2D classes therefore i would also appreciate some good resources where i can quickly learn these concepts/hierarchies a little better.

Rotating the label in 90 degrees intervals does not skew the label, it is when i have angles like 53 degrees or something. plus i am trying to keep the label aligned with my line or edge after the rotation has been performed. i do not believe this specific problem has been addressed here in these forums. Please at least help provide me with good and quick to learn resources for Graphics/Graphics2D or swing in general

Ali Abdallah
  • 33
  • 10
  • The limitations of rotating a `JLabel` are examined in this [duplicate](http://stackoverflow.com/q/6333464/230513). – trashgod Oct 31 '14 at 17:36
  • 1
    Rotating components are more complicated then just rotating the paint context. When you rotate the component, the size changes, which you've not recalculated. Take a look at [this example](http://stackoverflow.com/questions/25252127/java-rotating-non-square-jpanel-component/25253453#25253453) and [this example](http://stackoverflow.com/questions/22976226/is-there-any-way-i-can-rotate-this-90-degrees/22976755#22976755) – MadProgrammer Oct 31 '14 at 20:30
  • 1
    @trashgod not only do you need to translate the events, but the components layout hints ;) there are better alternatives to simply rotating the graphics context ;) – MadProgrammer Oct 31 '14 at 20:32
  • Thanks guys i feel i have a good idea what to look for now :) – Ali Abdallah Oct 31 '14 at 20:53

0 Answers0