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]

# 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