rotate button 90 degrees
Hi,
I have a problem with rotating a jButton. I can rotate the text , but the size of the button is not changed, although I use the setSize and setPreferredSize method. Here is a part of my source code:
public class RotatedButton extends JButton {
private String doocsRotate = "0";
...
public void setDoocsRotate(String rotate){
if ((rotate.equalsIgnoreCase("0") && getHeight()>getWidth()) ||
(!rotate.equalsIgnoreCase("0") && getHeight()<getWidth())) {
setPreferredSize(new Dimension(getHeight(), getWidth()));
setSize(getHeight(), getWidth());
}
}
doocsRotate = rotate;
}
public String getDoocsRotate(){
return doocsRotate;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform tr = g2.getTransform();
if (doocsRotate.equalsIgnoreCase("90")) {
tr.rotate( Math.PI / 2 , getWidth()/2.0, getHeight()/2.0);
}
if (doocsRotate.equalsIgnoreCase("270")) {
tr.rotate( - Math.PI / 2 , getWidth()/2.0, getHeight()/2.0);
}
g2.setTransform( tr );
super.paint(g);
}
}
Do you have any ideas what I am doing wrong?
Thanks,
Elke>
[1267 byte] By [
meiera] at [2007-11-27 8:43:10]

# 2
Thanks for your answer.
In the meantime I found a solution for rotating buttons. You have to override the BasicButtonUI. Here is the source code:
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;
public class DOOCSButtonUI extends BasicButtonUI {
protected int angle;
DOOCSButtonUI( int angle ) {
super();
this.angle = angle;
}
public Dimension getPreferredSize(JComponent c) {
Dimension dim = super.getPreferredSize(c);
return new Dimension( dim.height, dim.width );
}
private static Rectangle paintIconR = new Rectangle();
private static Rectangle paintTextR = new Rectangle();
private static Rectangle paintViewR = new Rectangle();
private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
public void paint(Graphics g, JComponent c) {
JButton button = (JButton)c;
String text = button.getText();
Icon icon = (button.isEnabled()) ? button.getIcon() : button.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;
Graphics2D g2 = (Graphics2D) g;
AffineTransform tr = g2.getTransform();
if( angle==90 ) {
g2.rotate( Math.PI / 2 );
g2.translate( 0, - c.getWidth() );
paintViewR.x = c.getHeight()/2 - (int)fm.getStringBounds(text, g).getWidth()/2;
paintViewR.y = c.getWidth()/2 - (int)fm.getStringBounds(text, g).getHeight()/2;
}
if (angle==270) {
g2.rotate( - Math.PI / 2 );
g2.translate( - c.getHeight(), 0 );
paintViewR.x = c.getHeight()/2 - (int)fm.getStringBounds(text, g).getWidth()/2;
paintViewR.y = c.getWidth()/2 - (int)fm.getStringBounds(text, g).getHeight()/2;
}
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 (button.isEnabled()) {
paintText(g,c,new Rectangle(paintViewR.x,paintViewR.y,textX,textY),text);
} else {
paintText(g,c,new Rectangle(paintViewR.x,paintViewR.y,textX,textY),text);
}
}
g2.setTransform( tr );
}
}
# 3
Maybe it's of interest to anybody, how I implemented a vertical toolbar with a vertical button:
btPanel = new JToolBar();
btPanel.setOrientation(JToolBar.VERTICAL);
btPanel.setFloatable(false);
btPanel.setRollover(true);
btTree = new JButton();
btTree.setFocusable(false);
btTree.setMaximumSize(new Dimension(22, 60));
Icon graphicIcon = UIManager.getIcon("Tree.closedIcon");
//btTree.setIcon(graphicIcon);
/*Here is one way to show vertical text on the JButton:
*http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html
*You only need these two classes:
*http://www.macdevcenter.com/mac/2002/03/22/examples/VTextIcon.java
*http://www.macdevcenter.com/mac/2002/03/22/examples/CompositeIcon.java
*and the following three code lines will do the job: */
VTextIcon textIcon = new VTextIcon(btTree, "Tree", VTextIcon.ROTATE_LEFT);
CompositeIcon icon = new CompositeIcon(graphicIcon, textIcon, CompositeIcon.BOTTOM);
btTree.setIcon(icon);
btPanel.add(btTree);
btPanel.setPreferredSize(new Dimension(28, 0));
getContentPane().add(btPanel, BorderLayout.WEST);