Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A rotated square panel in Java GUI

I wonder if it is possible to implement a GUI panel (possibly JPanel) that is of square shape but rotated 90 degrees. Obviously, there will be a top-level container which contains this panel, and visually the main panel is this rotated square panel within.

More specifically, I would divide a panel (called 'A') into 4 equal square sub-panels, and fill these sub-panels with JLabels, for which I am thinking to use GridLayout. And lastly, I would rotate 'A' 90 degrees to give what I want.

From my reading of other similar questions, it seems that you cannot rotate JPanel itself, but you can rotate what is contained within. Is this applicable to my case here? Would appreciate if someone could point out. Thanks.

like image 514
skyork Avatar asked Jun 13 '11 16:06

skyork


People also ask

What is a panel in Java GUI?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

How do I change the layout of a panel in Java?

You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.

How do you rotate a label in Java?

A JLabel can explicitly generate a PropertyChangeListener interface. By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().


4 Answers

The critical thing seems to be painting the components after rotating the graphics context. Here's an example:

enter image description here

Addendum 1:As @Atreys comments, the rotated components are drawn, but interact poorly. If the components must remain usable, event coordinates should also be transformed. Compare this (considerably) more complex example that mirrors components.

Addendum 2: If you also need to transform the mouse coordinates, this example may be helpful.

Addendum 3: Alternatively, consider the drawString() examples examined here.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/6333464 */
public class RotatePanel extends JPanel {

    public RotatePanel() {
        this.setPreferredSize(new Dimension(320, 240));
        this.add(new JLabel("Hello World!", JLabel.CENTER));
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int w2 = getWidth() / 2;
        int h2 = getHeight() / 2;
        g2d.rotate(-Math.PI / 2, w2, h2);
        super.paintComponent(g);
    }

    private void display() {
        JFrame f = new JFrame("RotatePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RotatePanel().display();
            }
        });
    }
}
like image 198
trashgod Avatar answered Oct 29 '22 23:10

trashgod


Check out JXTransformer in the SwingHelper project over at java.net. This class acts as a component decorator that allows you to apply an arbitrary affine transform to a component.

like image 33
jackrabbit Avatar answered Oct 30 '22 01:10

jackrabbit


Yes, you would have to have the top-level container (JPanel or other container) be the item that rotates the contents. Really you aren't rotating the items, you are rotating to the painting of the items.

like image 44
jzd Avatar answered Oct 30 '22 01:10

jzd


If all you need to do is rotate the text on a JLabel you could use a Rotated Icon, then you don't have to worry about rotating the panel.

like image 38
camickr Avatar answered Oct 30 '22 00:10

camickr