Question

Is there any way to correct the dimensions for a rotated component?

In my swing application I’d like to be able to rotate a single panel, and have it respond correctly to resize events. The rotation is straight-forward, but on resizing, the height and width dimensions are reversed. It could almost would work if a call to setSize was called from an overridden paint() call to swap the dimensions, but that doesn’t work, because setSize causes another call to paint to occur, and recursion ensues... I’ve also tried adding a ComponentAdapter to handle the swapping on resize events, but got the same result.

So, here’s a simplified example of what I’m working with. The components here are buttons, but the logic applies to a JComponent like a JPanel too. Button c is rotated with a JXTransformer, but this doesn’t resize (it’s commented out in the code, but you can add the JXTransformer class to the classpath if you wish). If you compile the sample, try resizing the window and see how the rotated button behaves. Screenshot: (It said I can't post screenshots, but these links appear to be live..)

http://i.stack.imgur.com/S3qmb.png

If I add in a scale transformation, the resizing is correct, but the component is distorted beyond usability. Screenshot:

http://i.stack.imgur.com/K4l9e.png

I’ve seen lots of questions on here that discuss the rotating part, but nothing about the resizing issue. For instance, A rotated square panel in Java GUI

Thanks!

Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class RotatingTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());

        // The button rotates, but the height/width dimensions are incorrect
        RotatedButton a = new RotatedButton("ROTATED CONTENTS!");

        JButton b = new JButton("Normal Contents");

        JButton c = new JButton("Transformer Contents");
//      JXTransformer t = new JXTransformer(c);
//      t.rotate(Math.toRadians(90));

        panel.add(a, BorderLayout.CENTER);
        panel.add(b, BorderLayout.WEST);
//      panel.add(t, BorderLayout.EAST);

        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }
}
class RotatedButton extends JButton{

    private static final long serialVersionUID = -3355833063753337573L;

    RotatedButton(String string){
        super(string);
    }


    @Override
    protected void paintComponent(Graphics g) {

        int width = getWidth();
        int height = getHeight();

        Graphics2D graphics = (Graphics2D) g;
        AffineTransform txOrig = graphics.getTransform();
        AffineTransform transformer = new AffineTransform(txOrig);

        transformer.translate(width/2, height/2);
        transformer.rotate(Math.toRadians(90));
        transformer.translate(-height/2, -width/2);

        // this scaling fits the button to the window, but distorts the contents
//      double coef1 = (double)width / (double)height;
//      double coef2 = (double)height / (double)width;
//      transformer.scale(coef2, coef1);

        // this line sets the rotation, comment out to disable
        graphics.setTransform(transformer);

        super.paintComponent(graphics);
        graphics.setTransform(txOrig);
        graphics.dispose();

        System.out.println("Parent size: "+getRootPane().getParent().getSize());
        System.out.println("this size: "+getSize());


    }
}

No correct solution

OTHER TIPS

So, the problem here is that JComponents do not care what is actually on them when setting their size. When you modify something in paintComponent() it only affects superficial portions of the component, and not anything that will actually be returned with from the component. What you are going to want to do, is remodel your paintComponent() method, or change how you are resizing the component.

Think of it like when you place an image on a button that is too large. The button will simply display what it can, and the rest does not matter as far as the button is concerned. Rotating the graphics on a JComponent is the exact same way. You can create the graphics ahead of time, and set your size to that (this is the complicated way), or if you are only rotating 90 degrees, simply change what you put int for setSize(). I have always found the second to be very simple, as you can constantly change the size of it with no extra code, you just have to remember to switch all your dimensions.

If, however, you wish to put it at an angle that is not a multiple of 90 degrees, you will have to make a larger square for the component to sit in. (using the Pythagorean theory) This may seem silly, but think about how all coordinates are made, from the top left corner. that corner defines that object's location, but if it is not at an absolute (the highest leftmost point) then anything working with that component would have to manually look at every part of the component instead of just calculating it.

Hope this helps, late as it may be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top