Results 1 to 3 of 3
  1. #1
    Shayke_ is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Question on getGraphics() in a JComponent

    I have a situation where I need to update graphics in a JComponent, but I think repaint() is too slow. This is for two reasons:

    1. Within paintComponent() there are many items that get drawn, each involving calculation prior to drawing them. So even if I try to repaint() with a clipping rectangle, I still have to do all the calculations on all the items for the graphics engine to discover that most of them lie outside the clip rectangle.

    2. In many cases, I need to redraw several small objects that might be located all around the JComponent. So any clip rectangle that contains them all will include much that does not need to be redrawn. Conceivably I could break down the redrawing into several calls to repaint(), each with a smaller clipping rectangle enclosing just one item at a time, but then Problem #1 is made even worse. And anyway, that solution just goes against the grain.

    I have a solution I like, but I have read so much about the evils of this sort of thing that I want to check it out here in case someone can spot why this is not a good idea.

    I can sum it up in the following pseudo-code:

    Java Code:
    class MyComponent extends JComponent {
    
    	MyClass StuffToPaint;
    	MyClass StuffToRepaint;
    	
            void flush() {
    	      g = getGraphics();
                  if(g == null) repaint();
                  else if(StuffToRepaint has stuff in it) paintComponent(g)
    	 }
    
    	 public void paintComponent(Graphics g) {
                  if(StuffToRepaint has stuff in it) { repaint that stuff and empty StuffToRepaint}
                  else { repaint all of StuffToPaint }
    	 }
    }

    Basically, I organize what I need to repaint into an object StuffToRepaint, and then call flush() to get the job done. At all times the internal state of the data is consistent with StuffToRepaint, so there is no problem with painting things that later get garbled by system calls to paint().

    The only problem is the call to getGraphics() and the direct call to paintComponent(), which I've read are no-nos, but I don't know why in this case. In the overall program the only way I ever put things in StuffToRepaint is through graphical user interaction, so it would seem I am guaranteed a valid return on getGraphics(). My main responsibility is to make sure I never put anything in StuffToPaint and leave it there without calling flush() in the same method. I

    I do convert g into a Graphics2D object inside paintComponent, but at least I am careful to set the AffineTransform to what I want, and restore it to the original each time. Is there something else going on behind the scenes that could mess this scheme up?

    If there happens to be a way to call repaint() that does not first clear the area it redraws in, then I could just call that instead of flush().

    Another possible solution is for me to do all my drawing in a private off-screen image buffer, and just use paintComponent() to swap the buffer. But that seems like overkill somehow.

    Any thoughts from more experienced Java programmers appreciated...

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

  3. #3
    Shayke_ is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why not simply paint to a BufferedImage and then display that in the JComponent's paintComponent method?
    Yeah, I thought of that. I guess that's the approved method, but I was resisting it due to old-fashioned worries about conserving memory and having to manage the size of the buffer myself.

    I'll guess I'll do it, but I'm still wondering if there is something that could demonstrably go wrong with my other method.

Similar Threads

  1. Replies: 2
    Last Post: 10-01-2010, 08:18 PM
  2. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 11:14 PM
  3. getGraphics() method problem
    By Aleksas in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2010, 12:34 AM
  4. JApplet.getGraphics() draws but JFrame doesn't
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 07-27-2009, 11:02 PM
  5. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 07:06 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
  •