• 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

Need help with "run"or threading

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code snippet displays the correct image in the emulator, but is slightly corrupted on the hardware:

protected void compute() {
// computationally intensive: over one second to execute
}
public void paint(Graphics g) {
...
g.drawRGB(xfrmImage, 0, POINTS, rx + 1, ry + 1, POINTS, POINTS, false);
}
public void run() {
int interval = 1000 * 2;
int count = 0;

while (mTrucking) {
System.out.println("count = " + count);
compute();
repaint();
try { Thread.sleep(interval); }
catch (InterruptedException ie) {}
if (count++ == 1)
mTrucking = false;
}
}
A hint of the error is in the print "count =" appearing twice:
count = 0
count = 0
count = 1
count = 1


The display, on the hardware, is fundamentally correct, but with visible errors. This suggest I do not adequately appreciate how "run" is implemented or how the threading works. It is as if a second "run" has been started before the first has finished. Any suggestions would be appreciated! Thanks,
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I haven't got my GUI APIs crossed, you probably should be invoking invalidate(), not repaint().

Invalidate() schedules repainting of selected (damaged/updated) parts of the display to run on the GUI thread, whereas repaint() forces immediate action.

I think.
 
Bob Ford
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply. My understanding is that invalidate() is not available in java me. Apparently, this has confused folks in the past, so I'm not alone; I just haven't found the solution yet. I believe it is connected with the run method containing the loop. Further testing shows that paint is not executed until the loop is completed, irrespective of repaint() or even serviceRepaints() in that loop.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic