0

i have a use case like after adding JLabelComponent to pallet which i have to resize to custom level(re sizing bcz data is very large ) the added label component.Once i am done with re-sizing setting the component.setBounds all the coordinates. when i try to rotate the re sized label component to 90 degrees i am not getting proper shape. its head are cut off. please suggest

Here is my code:

if (selectedComponent instanceof LabelComponent) {
LabelComponent lbls = (LabelComponent) selectedComponent;
lbls.setAngle(Integer.parseInt(value));
lbls.repaint();
lbls.setSize(lbls.getPreferredSize());

and my paint method is

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    AffineTransform aT = g2.getTransform();    
   double sin = Math.abs(Math.sin(getAngle()));
   double cos = Math.abs(Math.cos(getAngle()));
   int originalWidth = getWidth();
   int originalHeight = getHeight();
   int newWidth = (int) Math.floor(originalWidth * cos + riginalHeight  * sin);
  int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);

  if(getAngle() == Integer.parseInt("90"))          
    {
  g2.translate((newWidth-originalWidth)/2, (newHeight-orginalHeight)/2);
    }
 g2.rotate(Math.toRadians(getAngle()), originalWidth/2, originalHeight/2);
 super.paint(g);    
    }
  • Can you provide a runnable example. Rotation of Swing components is problematic at the best of times – MadProgrammer Nov 20 '13 at 05:59
  • Hi, Right now i do not have running example since this code is integrated in the project, i am debugging in project. today i will do it. – kanakmohank Nov 20 '13 at 06:33

1 Answers1

1

when i try to rotate the re sized label component to 90 degrees i am not getting proper shape.

One problem is that you need to reset the preferred size of the label. That is the width and height change because of the rotation.

Instead of doing custom painting you could try to use the Rotated Icon.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • i tried to set the width and Height using setSize(height, width)(since iam rotating label for 90 degrees i swapped the width and height) even that did not work. the thing is i have print persons address on the label component so as you suggest if i use rotated icon will that helps? – kanakmohank Nov 20 '13 at 06:31