aspose file tools*
The moose likes Applets and the fly likes Why is the image not scrolling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Why is the image not scrolling?" Watch "Why is the image not scrolling?" New topic
Author

Why is the image not scrolling?

Nitesh Panchal
Ranch Hand

Joined: Dec 23, 2008
Posts: 48
hello,
i am using drawImage() function and increasing the X value so that it appears as though the image is scrolling (similar to marquee tag) But the code isn't working. The X values are increased but still the image is getting drawn at the same place again and again. Please tell me why is this happening?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Putting aside thread synchronization issues, most likely it's due to this part, since "width" is always 0:




[Jess in Action][AskingGoodQuestions]
Nitesh Panchal
Ranch Hand

Joined: Dec 23, 2008
Posts: 48
Silly me! yeah i forgot to initialise the width itself. But what do you mean by thread synchronization? in this case no objects are shared between threads and moreover only 1 thread is there so why to synchronize?
Also what should i synchronize?
will this snippet work?



Is this the correct way of doing it?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

There are two threads both accessing the variable "x": the one that you created that increments "x", and the AWT event thread that paints the window, which reads it and uses it to position the image. In theory, any time you write a variable in one thread and read it from another, those access must explicitly be synchronized, or the variable must be marked "volatile"; otherwise, it's possible for one or both threads to cache thread-local values of the variable, so that the changes made by the one thread will not be seen by the other. In practice, you can often get away without synchronization; but on a different machine (for example, a multicore or multiprocessor machine) or a different JVM implementation, you might not.

In this case, the easiest thing to do is mark "x" volatile:

volatile int x;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is the image not scrolling?