Results 1 to 7 of 7
  1. #1
    knuth is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Redraw panel in timer event

    Hi,

    I have a map with cars in it, and in order to make them move, I call repaint in a timer event-which redraws everyting. However this is not performance friendly, since the map does not have to be redrawn, just the cars in it.
    So I thought about buffering the map and then call drawCars() in the timer. As a first step, I am calling drawEveryting() in a timer, instead of calling repaint of the panel. So all I did was putting the same code from repaint in drawEveryting(). However, the map is flickering a lot, even though the cpu use remains at 5%. Here the content of drawEveryting():
    Graphics2D g2 = (Graphics2D)getGraphics();
    super.paintComponent(g2);
    cars.draw(g2);

    Any suggestions?

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    you should override the paintComponent(...) method of your panal and do everything there.

    Java Code:
    public void paintComponent(Graphics g)
    {
       Graphics2D g2d = (Graphics2D)g;
       //super.paintComponent(g); //do not call this if you have your own back image
       ...
       //1) paint map image
       //2) paint cars
    }
    Edit: your timer or your redraw thread then just calls repaint()
    Last edited by mrmatt1111; 10-01-2009 at 09:32 PM.
    My Hobby Project: LegacyClone

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    You don't want to do that.
    There's another thread here about rendering problems (flicker), in which camickr posted a link to this thread at the Sun forums. Essentially render offscreen, then "blit" (OK, this is Java, but the concept's the same) to the screen (ie paintComponent).

    If you like, call it buffering...

  4. #4
    knuth is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Ok thx for the tip!
    But I am struggling with another issue: In order to change my code to off screen rendering, I have to revert this transformation:
    g2.scale(scale, -scale);
    g2.translate(-minLon, -(minLat + (float) bounds.getHeight()));
    g2.setStroke(new BasicStroke(1.0f / scale));

    I tried this, which did not work:
    g2.scale(1/scale, -1/scale);
    g2.translate(+minLon, +(minLat + (float) bounds.getHeight()));
    g2.setStroke(new BasicStroke(1.0f * scale));

  5. #5
    knuth is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    ..another trivial issue, how can I change the background color of the panel in paintcomponent()? I tried setPaint and setBackground, which does not really work - I have the map in a JScrollPane, and when I first draw a rectangle the panel size and fill it red, I see a red line only at the border

  6. #6
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    This works for me when reverting scale and translation:

    Java Code:
    g2d.translate(-pos.x*_scale,-pos.y*_scale); //pos: top left of visible rectangle
    g2d.scale(_scale,_scale);  //scale is zoom level
    ...
    //draw my objects at a position relative to Point "pos"
    ...
    g2d.translate(pos.x, pos.y);
    g2d.scale(1/_scale,1/_scale);
    //draw minimap
    ...
    "background color of the panel in paintcomponent()"

    you could set the background color of the panel and in then call:

    Java Code:
    super.paintComponent(g);
    Edit: oops, i meant to say: call the "super.paintComponent(g);" in your paintComponent(g)
    Last edited by mrmatt1111; 10-03-2009 at 07:53 PM.
    My Hobby Project: LegacyClone

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

    Default

    You probably shouldn't be changing the background color of a JComponent from within its paintComponent method. Change it elsewhere; for instance if it is an event-driven change of state do it in a listener, and then call repaint on the JComponent. Also be sure that super.paintComponent(g) is the first method called in your paintComponent method.

    edit: I suppose that you could fill a rectangle that has as its boundaries the boundaries of the JComponent, but if you did this, you would do this in place of super.paintComponent(g). I recommend against this however unless you have a good reason to do so (say if you needed a gradient background).
    Last edited by Fubarable; 10-03-2009 at 08:07 PM.

Similar Threads

  1. can i set a timer delay on mouseEntered event
    By chan in forum AWT / Swing
    Replies: 1
    Last Post: 08-30-2009, 05:55 AM
  2. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 PM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. How to output to panel as a .gif
    By Teh Brian in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 05:26 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 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
  •