Results 1 to 14 of 14
  1. #1
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default Jlabel rotation property

    Hi guys, i have a problem and I hope in your helping...
    I have a class that extend JLabel class. public myJLabel extends JLabel. I would to add a rotation property to this new class. I don't want to roteate it's content (like Image or Text) but I would to rotate all myJLabel. Can you help me? Alternately, can you suggest me another class to extend that have rotation property!! Thank you in advance and sorry for my wrong english

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    You can solve your problem with AffineTransforms. They are not easy to get started with,
    but if you are rotating things now, you will probably want to do more of that later.
    Java has several tutorials on AffineTransforms and Graphics2D,
    for instance Transforming Shapes, Text, and Images

    The one thing you need to know is that the argument to paint() usually coded as a Graphics is in fact a full Graphics2D object.

  3. #3
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Thank you so much for replay, zweibieren! I take a look to AffineTransform and I understand it! It uses matrix transformation for rotation...now the problem is to render and draw the object after transformation...can i use paint method or graphics2d object?? thank you again for replay

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could use the Graphics2D object provided by the JLabel's paintcomponent method, but be sure to call the transform prior to calling super.paintComponent(g).

  5. #5
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Thank you guys, now I take a look to graphics2d class, because i'm new with java graphics...thank you again

  6. #6
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Hi guys! I rotate myJLabel object, with AffineTransform, but i have another problem that follows this transformation. Now I will explain you this inconvenient.
    First of all, when I try to rotate myJlabel, it seems that there is a Container that remains the same...what i see it's a rotation inside this container. The container itself hide the rotated object. See the attached figure (after rotation). The control of the object (in this case a jbutton) remains to the container (if i click where you see red ball, it's like a press the button). I solve saving original shape (Shape oldshape = g2.getClip(); ) that I give after transformation: here the complete code inside painComponent(Graphics g)

    AffineTransform aT = g2.getTransform();

    Shape oldshape = g2.getClip();
    aT.rotate(0.1);
    g2.transform(aT);
    g2.setTransform(aT);
    g2.setClip(oldshape);

    now the render it's ok (entire object rotated), but the control remains old (not rotated)...
    Any idea?
    Thank you so much guys!!!!
    Attached Thumbnails Attached Thumbnails Jlabel rotation property-after_rotation.jpg  

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class RotLabel {
        int angle = 0;
    
        private JPanel getContent() {
            label.setFont(new Font("Monospaced", Font.PLAIN, 24));
            label.setBorder(BorderFactory.createEtchedBorder());
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(label, new GridBagConstraints());
            return panel;
        }
    
        private JLabel label = new JLabel("hello world") {
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D)g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                AffineTransform aT = g2.getTransform();
                Shape oldshape = g2.getClip();
                double x = getWidth()/2.0;
                double y = getHeight()/2.0;
                aT.rotate(Math.toRadians(angle), x, y);
    //            g2.transform(aT);
                g2.setTransform(aT);
                g2.setClip(oldshape);
                super.paintComponent(g);
            }
        };
    
        private JSlider getSlider() {
            final JSlider slider = new JSlider(-180, 180, angle);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    angle = slider.getValue();
                    label.getParent().repaint();
                }
            });
            return slider;
        }
    
        public static void main(String[] args) {
            RotLabel rotLabel = new RotLabel();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(rotLabel.getContent());
            f.add(rotLabel.getSlider(), "Last");
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hardwire, I have a problem with your solution. If I rotate the label 90 degrees and then compress it along the x-axis, it changes the displayed text just as if I were shrinking the width of the label (but I'm not since the width is aligned up and down). Instead of showing the complete text of the label, it truncates the text and displays ellipsis (...). Please see the image to see what I mean.
    Attached Thumbnails Attached Thumbnails Jlabel rotation property-rotated-label1.jpg  

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    In the RotLabel example I was trying to demonstrate a way around what I thought mix99 was having difficulty with: The container itself hide the rotated object. I never thought of resizing the frame as you did.
    Playing around with it I have concluded:
    The parent container has no idea that the graphics context has been transformed to 90 degrees rotation. When the container is resized below the child JLabels preferredSize.width the text is truncated.
    This is one of the problems with trying to draw with components. I usually approach this with graphics, viz, a JPanel and drawing the text in paintComponent.
    I did see a SourceForge (I think) project in which the author claimed to have developed a system that would draw components in transformed space.
    I have experimented with these kinds of things before and discovered the I don't have much patience with them; graphics is much easier.

  10. #10
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks hardwire, i take a look to your code...thank you again

  11. #11
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    thank you hardwire it works very well! Just a question: My Rotlabel object is contained into another Rotlabel object. So there are some controls that avoid the inner object to exit from it's content (the external RotLabel). When I rotate the object, this control not works, because it compute using old coordinates. getX(), getY(), getWidth(), getHeigth(), getAlignmentX(), getAlignmentY() Jlabel methods return same value before and after transformation... So, what i can do? Have I to compute X, Y, width, heigh( that will remain the same with rotation) with same transofrmation formula and then set X, Y, width, ecc?

    In figure: what new x, y after rotation?

    Thank you very much for your replay
    Attached Thumbnails Attached Thumbnails Jlabel rotation property-label_rot.jpg  
    Last edited by mix99; 01-20-2010 at 10:19 AM.

  12. #12
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    i think that this is a java limitation. I substitute the jlabel rotation sample provided by Hardwired with Jbutton Rotation..try to press the button after rotating: the control remains the same, only visualization rotate (see attached image).See button_rotated_pressed.png image. If I press where appears "h" letter, control doesn't work because it is not rotated. Can I rotate also control? So i can press the rotated jbutton everywhere. thank you again!
    Attached Thumbnails Attached Thumbnails Jlabel rotation property-button_normal.jpg   Jlabel rotation property-button_pressed.jpg   Jlabel rotation property-button_rotated_pressed.jpg  

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class RotHit extends JPanel {
        int angle = 0;
        boolean selected = false;
        Rectangle rect = new Rectangle(100, 100, 150, 35);
        AffineTransform at = new AffineTransform();
    
        public RotHit() {
            addMouseMotionListener(mml);
        }
    
        private void setSelected(boolean selected) {
            this.selected = selected;
            repaint();
        }
    
        private boolean isSelected() {
            return selected == true;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);    
            g2.setPaint(Color.blue);
            g2.draw(rect);
            Shape s = at.createTransformedShape(rect);
            if(isSelected()) {
                g2.setPaint(Color.pink);
                g2.fill(s);
            }
            g2.setPaint(Color.red);
            g2.draw(s);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(400,300);
        }
    
        private JSlider getSlider() {
            final JSlider slider = new JSlider(-180, 180, angle);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    angle = slider.getValue();
                    double x = rect.getCenterX();
                    double y = rect.getCenterY();
                    double theta = Math.toRadians(angle);
                    at.setToRotation(theta, x, y);
                    repaint();
                }
            });
            return slider;
        }
    
        public static void main(String[] args) {
            RotHit test = new RotHit();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getSlider(), "Last");
            f.pack();
            f.setLocation(100,100);
            f.setVisible(true);
        }
    
        private MouseMotionListener mml = new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                // Look for the point inside the transformed rect.
                Shape xfRect = at.createTransformedShape(rect);
                boolean overXformRect = false;
                if(xfRect.contains(p)) {
                    overXformRect = true;
                    if(!isSelected()) {
                        setSelected(true);
                    }
                }
                if(!overXformRect && isSelected()) { // reset
                    setSelected(false);
                }       
            }
        };
    }

  14. #14
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 06-25-2009, 05:28 AM
  2. Need help with 2d Array rotation!
    By TrungTran in forum Advanced Java
    Replies: 1
    Last Post: 11-23-2008, 05:35 AM
  3. Log rotation in ubuntu
    By sarah11 in forum New To Java
    Replies: 0
    Last Post: 11-04-2008, 07:46 AM
  4. Rotation in java 3D
    By émilie- in forum Java Applets
    Replies: 0
    Last Post: 02-13-2008, 10:37 AM
  5. affineTransform rotation
    By MichYer in forum AWT / Swing
    Replies: 0
    Last Post: 07-18-2007, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •