Results 1 to 14 of 14
Thread: Jlabel rotation property
- 01-18-2010, 01:43 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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
- 01-18-2010, 08:41 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
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.
- 01-18-2010, 11:14 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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
-
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).
- 01-19-2010, 08:43 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Thank you guys, now I take a look to graphics2d class, because i'm new with java graphics...thank you again
- 01-19-2010, 07:38 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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!!!!
- 01-19-2010, 09:49 PM #7
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); } }
-
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.
- 01-20-2010, 07:54 AM #9
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.
- 01-20-2010, 08:55 AM #10
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
thanks hardwire, i take a look to your code...thank you again
- 01-20-2010, 09:36 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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 replayLast edited by mix99; 01-20-2010 at 10:19 AM.
- 01-20-2010, 05:54 PM #12
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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!
- 01-20-2010, 08:14 PM #13
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); } } }; }
- 01-21-2010, 04:20 PM #14
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Be Elite Join Property Elite , Property Realtor Needed , Career With Us -
By mindyyong in forum Jobs OfferedReplies: 0Last Post: 06-25-2009, 05:28 AM -
Need help with 2d Array rotation!
By TrungTran in forum Advanced JavaReplies: 1Last Post: 11-23-2008, 05:35 AM -
Log rotation in ubuntu
By sarah11 in forum New To JavaReplies: 0Last Post: 11-04-2008, 07:46 AM -
Rotation in java 3D
By émilie- in forum Java AppletsReplies: 0Last Post: 02-13-2008, 10:37 AM -
affineTransform rotation
By MichYer in forum AWT / SwingReplies: 0Last Post: 07-18-2007, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks