• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic JPanel

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. Ive got a program thats an extension of JFrame and it had a JPanel component that I only want to repaint when I tell it to (for the sake of efficiency).

In my main program have a while(true) loop in which there is a thread sleeping and calls some functionality:

this is inside a try/catch btw


imagepanel.Set_image(database.get_current_image());
database.Update_image(direction_forward);
// imagepanel.revalidate();
// imagepanel.repaint();
// map_panel.repaint();
repaint();
jTabbedPane1.revalidate();
jTabbedPane1.repaint();

jMenuBar1.revalidate();
jMenuBar1.repaint();
t.sleep(time_interval);

as you can ee Ive tried a few things, but currently my JPanel will update, however it does it constantly.

this is the paintComponent method inside my extended JPanel class, imagepanel.

public void paintComponent(Graphics g) {

System.out.println(image_name);
g.drawImage(img, 0, 0, null);
super.paintComponent(g);
repaint();
}

this method works, however it calls it way too much. What I would like is to be able to call all repaints from my main method by using something like imagepanel.repaint(); and not calling the repaint(); inside imagepanel. If I do that it doesnt work. Nothing gets displayed.

Any thoughts?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Don't call repaint in paintComponent -- that's an infinite loop. To drive
repainting, use a javax.swing.Timer.

2. It looks like you are salting and peppering your code with revalidates and repaints. Often, you can get away with none:
 
Brendan Fosberry
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, thanks. Your code looks pretty complicated. Im not too keen on changing my current code too much. I need control over the sleep time and the speed of the images clocking and I dont want to have to create a new timer every single time I want to change the code. I just need to be able to have a JPanel, and when I call something from my main method it updates and draws the image once. Is there another way to update and paint JPanel?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would remove the call to repaint() inside paintComponent(). If the repaint request is honored, then paintComponent is called and then repaint() again. This could lead to infinite calls to paintComponent() provided that the requests are honored.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly, the code I gave above is not complicated. Have another look at it.
And if you want to speed up or slow down a timer, call its setDelay method --
you can call it as often as you like. Again, not complicated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic