Results 1 to 11 of 11
Thread: Graphics2D repaint bug
- 02-09-2011, 11:08 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Graphics2D repaint bug
Hy , i have a class that extends JLabel , with the feature of changing the text angle:
public class AngledJLable extends JLabel {
private static final long serialVersionUID = 1L;
private Integer angle;
public AngledJLable(String text , Integer angle, Color color ,Font font) {
super(text);
this.angle = angle;
setFont(font);
setForeground(color);
setBackground(new Color(0,0,0,100));
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,RenderingHints.VALUE_ANTIALIAS_OFF);
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.setTransform(aT);
g2.setClip(oldshape);
super.paintComponent(g);
}
}
but it seems each time it gets redrawn the previous text is not cleared. I have tried setting the text to "" but it seems not to work.
If any1 is willing to tell me what am i missing , i would appreciate it.
CheersLast edited by NextEpisode1; 02-09-2011 at 11:11 PM.
-
- 02-09-2011, 11:22 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
-
Then you're doing your rotation wrong as you need to call super first. I cannot compile anything right now, but perhaps you need to reset your AT back to its original state or do your drawing in a copy of the Graphics object. But seriously regardless of what needs to be done, super.paintComponent(g) should come first.
- 02-09-2011, 11:35 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
well im still a noobie in rich clients , and this code is pretty much copied from a forum :D (damn that hurts) . It seems i have managed to fix the issue by repainting the whole window (pretty expensive operation i might say).
Regarding the super first thing, why do you think i must call super.paintComponent(Graphics g) first? As i said be4 i dont have a rich experience in java graphics but common sense dictates : rotate it then show to the user.
-
- 02-09-2011, 11:53 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
- 02-09-2011, 11:56 PM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Here's a tip: become extremely familiar with the API's of the classes you are using. Case in point (and something Fubarable alluded to): the setTransform method in graphics. Quote from Graphics2D (Java Platform SE 6)
In the link above you will find more explanation and alternatives to using this methodWARNING: This method should never be used to apply a new coordinate transform on top of an existing transform because the Graphics2D might already have a transform that is needed for other purposes, such as rendering Swing components or applying a scaling transformation to adjust for the resolution of a printer.
- 02-10-2011, 12:00 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
- 02-10-2011, 05:30 AM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
I've found that when one of the canned swing components doesn't do what you want out of the box, it's usually easier to create your own custom component by extending JComponent than to extend an existing component. But if you really want to extend JLabel, you can manually clear the canvas yourself by simply calling
...this is actually sloppy, as you should take the insets into account as well. This certainly isn't a Graphics2D bug though. There is nothing in the API that states it will automatically clear the canvas for you.Java Code:Rectangle r = getBounds(); g.clearRect(r.x, r.y, r.width, r.height);
- 02-10-2011, 12:50 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
The bug is in my code not in graphics class , i was ambiguous with the post name , sry . And with the clearRect() , been there tried that. I have this label on a transparent window , so i cant clear it directly from the class.
I have found a pretty decent workaround : i added the lbl to a panel and repaint that panel
Similar Threads
-
graphics to graphics2d casting
By rocklikeits99 in forum New To JavaReplies: 2Last Post: 09-22-2010, 03:12 AM -
Graphics2D setRenderingHint not working
By Crazy Caveman in forum Java 2DReplies: 2Last Post: 08-27-2010, 06:47 PM -
repaint every
By 3xpr1ment in forum AWT / SwingReplies: 10Last Post: 03-23-2010, 05:39 PM -
How can i draw multi Graphics2D with my java code?
By SUTEKI in forum Java 2DReplies: 3Last Post: 02-13-2009, 07:59 AM -
Graphics2D: stack overflow error
By rosh72851 in forum New To JavaReplies: 11Last Post: 10-15-2008, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks