I think at least part of your problem is no layout manager.

Try to get it to work with a layout manager, vs. no layout manager.

The rotation stuff looks okay, but I might call setBounds() vs.
super.setBounds().  Actually, if you were to use a layout manager.... you
wouldn't call those directly, but would setPreferredSize...

Also, you only need to override paintComponent(), not paint(). That way,
you can still have a border around the text field. This would only work if
you size the component properly (vs. calling setBounds() directly).

The JBuilder compiler is less strict than the javac one. For instance, last
time I checked you can have multiple public classes in a .java file if you
want.

Thanks for your message at 10:23 AM 9/28/99 -0400, [EMAIL PROTECTED]:
> >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


John Zukowski    Focus on Java Guide / http://java.about.com
     [EMAIL PROTECTED] | [EMAIL PROTECTED]
Author Java AWT Reference / Zukowski's Definitive Guide to Swing
     MageLang Institute          / Mastering Java 2
     http://www.jguru.com  Big Brother Inside

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to