This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes for loop in paint method........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "for loop in paint method........" Watch "for loop in paint method........" New topic
Author

for loop in paint method........

sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
HI all,

i am doing animation using applet.I finished some work.
Now i am drawing some image. i want it auto. if i press a button that image should be draw 5 times in row. 1 after 1. on applet after some time delay.

like train - after drawing 1st image after some time same image append to first one.

i used thread also time thread. but its not working.

what should i use for this. Give some guidelines.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
Welcome to JavaRanch.

what should i use for this.

You should start by reading ItDoesntWorkIsUseless and TellTheDetails.


Ping & DNS - my free Android networking tools app
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
i tried for loop inside paint method for showing image 5 times with some thread sleep.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
That is certainly possible, but you haven't provided enough information to tell what is going on. Posting a short, relevant excerpt from the code would help, as would a more detailed description of what is happening.
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
Sir,
This is what i am doing.
For this i want put JButton as Play and Pause.

For pause button Animation should stop and after press Play button it start from that point.





Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
You misunderstand how threads work in Java. The "Xeq" thread doesn't do anything, so putting it to sleep doesn't accomplish anything. The first few pages of the concurrency tutorial should help: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

The solution involves implementing the Runnable interface, and then starting a thread with that Runnable. Within that thread's "run" method you would set up all variables the paint method needs for painting, and then call "repaint" at each iteration.


if (Xeq == null) {
Xeq.start();
}

What?
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
Here I am update this code with using Runnable interface.
Now if i put the for loop in run is just moving the image.

I want to append the image to previous image.
How should i use this for loop?


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
I want to append the image to previous image.

What does this mean? Images aren't "appended" to one another; you'll need to tell what kind of operation that is, preferably with example images.
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
I am showing the auto animation of linked list.
In above code using node method i am drawing image which showing a one node of list.
Now i want to show animation for 5 node in list. Like after one node we are adding another node.

but In above working code inside for loop if i am calling node method that node image is just moving forward.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
Sorry, I don't understand what "showing a one node of list" means, and consequently also not how "show animation for 5 node in list. Like after one node we are adding another node. " would look like. What are "nodes" - text? images? something else? What do "nodes" look like on the screen? How would several of them look like?
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
Sir,

Node is a just circle and text on it. All node's are the same.

want to show list like this

{12}-->-{45} -->-{50}

Now i want like this 12 is showing on circle after some time interval want to show 45 and so on
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
Sir,

Here i am sending the snap shot of my applet.
Now as in image after first element i want to shown second element after some time interval and so on


[Download list.bmp] Download

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
OK, now we're making progress. You need to write the paint method in such a way that it draws the correct things no matter when it's being called. In other words, it needs to be able to read all state that is needed for drawing from some instance variables.

One approach would be to have an instance field (maybe "count") that is counted up in step with the loop variable in the "run" method. The run method would then simply increment "count", call repaint, and sleep. The paint method would look at "count", and then draw an appropriate number of nodes.

Edit: Just saw that you tried to post an image - you're better off using a format that can be viewed in a browser (gif, png, jpeg), and make sure it's small in size and dimensions.
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
Thank you, Sir.

I will do this.
sachin upatil
Greenhorn

Joined: May 17, 2010
Posts: 29
sir,

i tried as you told its working fine.

But the problem is each time in increment the all node is moving moving forward.


This method is drawing same image of node.

This is last line of method:
x = x+80;

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
You need a loop somewhere that iterates over all nodes, and decides which one to draw -and which one not to draw- based on value of the counter. The actual coordinates of each node should not depend on the value of the counter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop in paint method........
 
Similar Threads
paint method problem
Flickering of Canvas
Canvas HELP!!!
Displaying image on jsp
creating Image