>Try getting it to compile first. You have two rjc variables. One local to
main and one local to inner class.
>
>E:\temp>javac RotatableJComponent.java
>RotatableJComponent.java:93: Variable 'rjc' is already defined in this
method.
> RotatableJComponent rjc =
>(RotatableJComponent)e.getSource();
> ^
>1 error
Ouch! That hurt...
I guess my JBuilder compiler is not as strict as the JDK compiler.
In any case, here's code that works with both JDK1.2 and JDK1.3.
(See attached file: RotatableJComponent.java)
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 rjc2 =
(RotatableJComponent)e.getSource();
int i = Integer.parseInt(rjc2.getText());
rjc2.setRotation(i);
}
});
frame.getContentPane().add(rjc);
frame.validate();
frame.setVisible(true);
}
}
Patrick H. Ryan
Senior Design Engineer
Hobart Technology Center
RotatableJComponent.java