Results 1 to 11 of 11
  1. #1
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Question 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.
    Cheers
    Last edited by NextEpisode1; 02-09-2011 at 11:11 PM.

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

    Default

    Quote Originally Posted by NextEpisode1 View Post
    Hy , i have a class that extends JLabel , with the feature of changing the text angle:

    Java Code:
    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_ANTIALIASING,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.
    Cheers

    There's a bug alright. You need to call the super's paintComponent method as the first line of your override method.

  3. #3
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    There's a bug alright. You need to call the super's paintComponent method as the first line of your override method.
    nope , if i call the super's paint component as the first line i loose the rotation effect desired.

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

    Default

    Quote Originally Posted by NextEpisode1 View Post
    nope , if i call the super's paint component as the first line i loose the rotation effect desired.
    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.

  5. #5
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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.

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

    Default

    Quote Originally Posted by NextEpisode1 View Post
    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.
    That's not what the super method does -- it doesn't "show it to the user". Rather it clears the panel of the previous image, and the symptom of not calling it first is just what you describe.

  7. #7
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    That's not what the super method does -- it doesn't "show it to the user". Rather it clears the panel of the previous image, and the symptom of not calling it first is just what you describe.
    You are right it's a good practice , but besides clearing the panel it also draws the text, if you notice i have no drawString() there , i let the JFrames paintComponent() do the drawing.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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)
    WARNING: 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.
    In the link above you will find more explanation and alternatives to using this method

  9. #9
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    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 method
    This is getting more and more interesting as we go :)) thanks , i'l read some documentation now :P

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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

    Java Code:
    Rectangle r = getBounds();
    g.clearRect(r.x, r.y, r.width, r.height);
    ...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.

  11. #11
    NextEpisode1 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    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

    Java Code:
    Rectangle r = getBounds();
    g.clearRect(r.x, r.y, r.width, r.height);
    ...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.
    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

  1. graphics to graphics2d casting
    By rocklikeits99 in forum New To Java
    Replies: 2
    Last Post: 09-22-2010, 03:12 AM
  2. Graphics2D setRenderingHint not working
    By Crazy Caveman in forum Java 2D
    Replies: 2
    Last Post: 08-27-2010, 06:47 PM
  3. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  4. Replies: 3
    Last Post: 02-13-2009, 07:59 AM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM

Posting Permissions

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