I have a requirement to be able to rotate JTextFields, JEditorPanes, etc.
I'm close, but I need help from the list.
If you run the (small) attached standalone app, you can type 0, 90, 180 or
270 in the text field and it rotates itself.
Any help or suggestions appreciated.
Thanks.
-Pat.
Patrick H. Ryan
Senior Design Engineer
Hobart Technology Center
(See attached file: RotatableJComponent.java)
Here's the code if you don't want to extract the attachment:
package untitled4;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
RotatableJComponent can be rotated 90, 180 or 270 degrees.
The main() test driver allows the user to enter 0, 90, 180 or 270 in
the textField to see the results.
*/
public class RotatableJComponent extends JTextField {
private int rotation;
private int normalX, normalY, normalWidth, normalHeight;
public RotatableJComponent() {
super();
setBorder(null);
}
/**
Capture the normal bounds and retain.
*/
public void setBounds(int x, int y, int width, int height){
normalX = x;
normalY = y;
normalWidth = width;
normalHeight = height;
super.setBounds(x, y, width, height);
setToolTipText("x=" + getX() + " y=" + getY() + " width=" + getWidth() + "
height=" + getHeight() + " rotation=" + rotation);
}
public void setRotation(int degrees){
this.rotation = degrees;
switch(rotation){
case 0:
super.setBounds(normalX, normalY, normalWidth, normalHeight);
break;
case 90:
// Reverse width and height.
super.setBounds(normalX, normalY, normalHeight, normalWidth);
break;
case 180:
super.setBounds(normalX, normalY, normalWidth, normalHeight);
break;
case 270:
// Reverse width and height.
super.setBounds(normalX, normalY, normalHeight, normalWidth);
break;
}
revalidate();
setToolTipText("x=" + getX() + " y=" + getY() + " width=" + getWidth() + "
height=" + getHeight() + " rotation=" + rotation);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
if(rotation != 0){
Rectangle rectangle = g2d.getClipBounds();
System.out.println("clipBounds=" + rectangle + " getX()=" + getX() + "
getY()=" + getY() + " getWidth()=" + getWidth() + " getHeight()
=" + getHeight());
AffineTransform at = AffineTransform.getRotateInstance((this.rotation *
java.lang.Math.PI) / 180,
rectangle.getCenterX(), rectangle.getCenterY());
g2d.transform (at);
}
super.paint(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setSize(new Dimension(400, 300));
RotatableJComponent rjc = new RotatableJComponent();
rjc.setBackground(Color.yellow);
rjc.setText("0");
rjc.setBounds(10, 20, 100, 10);
rjc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
RotatableJComponent rjc =
(RotatableJComponent)e.getSource();
int i = Integer.parseInt(rjc.getText());
rjc.setRotation(i);
}
});
frame.getContentPane().add(rjc);
frame.validate();
frame.setVisible(true);
}
}
RotatableJComponent.java