Rotate JLabel containing a imgicon

I want to rotate a jlabel when its clicked. The label is containing a imgicon.

I have tried to do like

http://www.codeguru.com/java/articles/199.shtml

but the label dont rotate, just the image (i think).

The image image is a vertical filled rectangle, but when i try to click it, it becomes a square...

The code for adding the ship to the contentpane

ImageIcon img =new ImageIcon("bigship.gif");

JLabel jLship =new JLabel("ship", img, SwingConstants.LEFT );

jLship.setName("ship");

jLship.setBounds(

ship.getPosition().x * (jlPOpoGame.getWidth() / 11) + 5,

ship.getPosition().y * (jlPOpoGame.getHeight() / 11) + 5,

img.getIconWidth(),

img.getIconHeight());

getJPOwnGame().add(jLship ,new Integer(1), 0);

And the code for changing the label inside a mouselistener.

selected.setUI(new VerticalLabelUI(true));

And the code for the UI:

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.plaf.basic.*;

class VerticalLabelUIextends BasicLabelUI

{

static{

labelUI =new VerticalLabelUI(false);

}

protectedboolean clockwise;

VerticalLabelUI(boolean clockwise )

{

super();

this.clockwise = clockwise;

}

public Dimension getPreferredSize(JComponent c)

{

Dimension dim = super.getPreferredSize(c);

returnnew Dimension( dim.height, dim.width );

}

privatestatic Rectangle paintIconR =new Rectangle();

privatestatic Rectangle paintTextR =new Rectangle();

privatestatic Rectangle paintViewR =new Rectangle();

privatestatic Insets paintViewInsets =new Insets(0, 0, 0, 0);

publicvoid paint(Graphics g, JComponent c)

{

JLabel label = (JLabel)c;

String text = label.getText();

Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

if ((icon ==null) && (text ==null)){

return;

}

FontMetrics fm = g.getFontMetrics();

paintViewInsets = c.getInsets(paintViewInsets);

paintViewR.x = paintViewInsets.left;

paintViewR.y = paintViewInsets.top;

// Use inverted height & width

paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);

paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;

paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

String clippedText =

layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

Graphics2D g2 = (Graphics2D) g;

AffineTransform tr = g2.getTransform();

if( clockwise )

{

g2.rotate( Math.PI / 2 );

g2.translate( 0, - c.getWidth() );

}

else

{

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

g2.translate( - c.getHeight(), 0 );

}

if (icon !=null){

icon.paintIcon(c, g, paintIconR.x, paintIconR.y);

}

if (text !=null){

int textX = paintTextR.x;

int textY = paintTextR.y + fm.getAscent();

if (label.isEnabled()){

paintEnabledText(label, g, clippedText, textX, textY);

}

else{

paintDisabledText(label, g, clippedText, textX, textY);

}

}

g2.setTransform( tr );

}

}

Why dont the JLabel rotate?

[5820 byte] By [huba] at [2007-11-27 2:19:00]
# 1

I found the error, but one new occured... :(

I rotate the label with

selected.setBounds(e.getX(), e.getY(), selected.getHeight(), selected.getWidth());

But the rotation works fine only one time, then it gets wrong! Look at

http://mrserver.pointclark.net/tmp/rotate.jpg

to how it looks.

Picture 1: Start position.

Picture 2: First click, rotate around where i clicked, OK.

Picture 3: Second click, the image gets cropped! The label is rotated thoug, i can select it and drag it if i click where the image should be.

Picture 4: Third click. The image is back to horizontal, looks ok!

And then it switched between 3 and 4 when i continue to click!

Can somebody help me?

huba at 2007-7-12 2:18:49 > top of Java-index,Desktop,Core GUI APIs...