0

I tried rotating JLabel text on based selected angles 30, 90 etc.I wrote CustomJLabel class:

public CustomJLabel extends JLabel {

    private int angle=360; //setter and getters

    @Override 
    public void paintComponent(Graphics g) {   
        super.paintComponent(g);   
        Rectangle rectangle = g2d.getClipBounds(); 
        g2d.rotate((getAngle() * java.lang.Math.PI) / 4, rectangle.getCenterX(), rectangle.getCenterY());   
    } 
}

Main.java:

CustomJLabel cst=new CustomJLabel ();
cst.setAngle(value);
cst.repaint();
cst.setSize(getPrefferedsize());

When running above code. Its rotated the text correct based on angle. But its not correctly the rectangle shape. Please check the below images.

Horizontal (360)

Horiztal(360)

Vertical (90)

enter image description here

enter image description here

Please observe Red Color Circles and the image was Vertical 90 degress and check the. Here is the missing top and bottom lines for JLabel. Why is it missing?

Pochmurnik
  • 780
  • 6
  • 18
  • 35
tech2504
  • 947
  • 4
  • 19
  • 34

1 Answers1

2

Recall that Graphics specifies the following:

Operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen that hangs down and to the right * of the anchor point on the path.

When you draw a Rectangle having the dimensions returned by getClipBounds() using the identity transform, it will be clipped on the right and bottom. When you draw the same Rectangle using a different transform, the result will be clipped in a way that depends, in part, on rounding error.

* emphasis added.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • That means before rotate text, can i use AffineTransform? – tech2504 Feb 21 '13 at 02:01
  • 1
    I don't understand; clipping occurs _after_ any transform is applied. This [example](http://stackoverflow.com/a/5594424/230513) compares two approaches. – trashgod Feb 21 '13 at 02:04