Border lost when rotating JPanel

Hi every one.

I've subclassed a JPanel to make a rotatable JPanel .

I can rotate the panel to 90 180 and 270 degrees , but the problem is that if I rotate the panel 180 degrees the Border (created with BorderFactory.createLineBorder(Color.BLACK) ) is lost.

This is the code of paintComponent :

public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

if (pos == Position.RIGHT) {

g2.translate(this.getHeight(), 0);

g2.rotate(Math.PI/2);

} else if (pos == Position.SOUTH) {

g2.translate(this.getWidth(), this.getHeight());

g2.rotate(Math.PI);

} else if (pos == Position.LEFT) {

g2.translate(0, this.getWidth());

g2.rotate(-Math.PI/2);

}

super.paintComponent(g);

}

where Position is :

enum Position { NORTH, SOUTH,LEFT, RIGHT };

The border is set in the Constructor .

How can I solve the problem?

Thanks

Marcello

[972 byte] By [HayArmsa] at [2007-11-27 5:38:19]
# 1

Not sure if this is the problem or not, but the border is painted after the component is painted so read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/painting/concepts2.html]The Graphics Object and Graphics2D [/url], especially the part that says

The Graphics object should have the same state when you're finished painting as it had when you started.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your reply and the article you posted!

I can't restore the Graphics object for some reason :( If I restore the Graphics object before exiting paintComponent() then all the transformations I did in the paintComponent are lost. Do you know how to fix this?

Anyway, this is a sample code (quite messed, I'm experimenting), but I doubt you can use it , because I use a little image "empty.jpg" to fill some spaces that are empty without this image and I don't know hot to attach it :

public class Test {

enum Rotation { NORMAL, CCLOCK90, CLOCK90, REVERSE };

enum Position { NORTH, SOUTH,LEFT, RIGHT };

public class GUISpace extends JPanel {

private GridLayout layout;

private JLabel label;

private JPanel internal_panel;

private JLabel labels[];

private ImageIcon ic;

private ImageIcon ic2;

private ImageIcon icp[];

private Position pos;

public GUISpace(String name, Position pos) {

super();

this.pos = pos;

this.setBorder(BorderFactory.createLineBorder(Color.black, 2));

ic = new ImageIcon("images/empty.jpg");

icp = new ImageIcon[6];

icp[0] = new ImageIcon("images/player11.jpg");

icp[1] = new ImageIcon("images/player21.jpg");

icp[2] = new ImageIcon("images/player31.jpg");

icp[3] = new ImageIcon("images/player41.jpg");

icp[4] = new ImageIcon("images/player51.jpg");

icp[5] = new ImageIcon("images/player61.jpg");

labels = new JLabel[6];

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

internal_panel = new JPanel(new GridLayout(3,2));

label = new JLabel(name);

internal_panel.setAlignmentX(JComponent.LEFT_ALIGNMENT);

label.setAlignmentX(JComponent.LEFT_ALIGNMENT);

label.setFont(new Font("Font", label.getFont().getStyle(), 8));

label.setPreferredSize(new Dimension(ic.getIconWidth()*2, 10));

for (int counter = 0; counter < 6; counter++) {

labels[counter] = new JLabel(ic);

internal_panel.add(labels[counter]);

}

this.add(label);

this.add(internal_panel);

}

public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g.create();

if (pos == Position.RIGHT) {

g2.translate(this.getHeight(), 0);

g2.rotate(Math.PI/2);

} else if (pos == Position.SOUTH) {

g2.translate(this.getWidth(), this.getHeight());

g2.rotate(Math.PI);

} else if (pos == Position.LEFT) {

g2.translate(0, this.getWidth());

g2.rotate(-Math.PI/2);

}

}

public void setPlayerOn(int player_number) {

labels[player_number].setIcon(icp[player_number]);

}

public void setPlayerOff(int player_number) {

if (pos == Position.NORTH || pos == Position.SOUTH)

labels[player_number].setIcon(ic);

if (pos == Position.LEFT || pos == Position.RIGHT)

labels[player_number].setIcon(ic2);

}

}

/**

* @param args

*/

public static void main(String[] args) {

Test test = new Test();

test.guiGen();

}

private void guiGen() {

JFrame main_frame = new JFrame("Test!");

main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GUISpace sp = new GUISpace("Bastioni Gran Sasso", Position.SOUTH);

main_frame.add(sp);

main_frame.pack();

main_frame.setVisible(true);

}

}

Thanks for your help

HayArmsa at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...
# 3
The [url http://java.sun.com/docs/books/tutorial/]2D Graphics[/url] tutorial has a working demo that does rotations and other stuff.Can't test your code since I use JDK1.4.2.
camickra at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...
# 4
I don't know why , but I think like if there's something really bad in the code I've written (I don't know very well how Swing drawing works, well, I know that paintComponent() is called, but not much more)Is someone able to spot how it should be done?Thanks
HayArmsa at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Is someone able to spot how it should be done?Yes, the tutorial has a working example, so you start with that code and then change it to your requirements.
camickra at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

The problem is that on those example there's no transform example on swing elements.

What I'd like to do is overriding the paintComponent() of JPanel, transforming the Graphics object passed as parameter, paint the component with the super.paintComponent() using the transformed Graphics object to make it look rotated and then clearing up the transformed Graphics object to it's original state .

The problem is that I feel like there's something wrong in this . I paint nothing by my self on the screen (calling some drawing method) , but I let do everything to the original drawing method of JPanel.

Also, if I reset the Graphics object the rotation doesn't happen, so I feel like the rotation of the object is made AFTER the exit from my overridden paintComponent method.

I'm confused :-(

Marcello

Message was edited by:

HayArms

HayArmsa at 2007-7-12 15:11:42 > top of Java-index,Desktop,Core GUI APIs...