GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Sep-2009, 18:09
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

How to continue program operation while animation loop is running


I wrote a program, as an challenge. It basically pulls realtime youtube View Count stats off of the web and creates a graph.

The program works fine, but I decided to up the ante, so to speak, and create a little animated "refresh" graphic the runs while the data is being downloaded. It is a junky GLine version of the typical refresh graphic you see all the time (at least on macintoshes). It's a circle of lines that appear to rotate.

The problem I have now is the animation is a while loop, so as soon as I tell the refresh graphic class to animate, the program is stuck in that loop and no more operations take place. The data is never pulled from the web. Game over.

I've nearly completed the Stanford methodology course and I don't think this was ever covered - how do I create a looped animation in such a way that the program continues to operate?

Thanks so much. The questions I asked on this forum when I first started the course seem so simple to me now
-Ethan
  #2  
Old 23-Sep-2009, 18:10
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to operations while an animation loop is running


Okay, can't edit the title. nice...
  #3  
Old 23-Sep-2009, 19:19
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: How to operations while an animation loop is running


Quote:
Originally Posted by ebot9000
Okay, can't edit the title. nice...

Tell me what you want the new title to be and I will do it for you.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 23-Sep-2009, 19:27
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: How to operations while an animation loop is running


Quote:
Originally Posted by ebot9000
... I decided to up the ante, so to speak, and create a little animated "refresh" graphic the runs while the data is being downloaded. It is a junky GLine version of the typical refresh graphic you see all the time (at least on macintoshes). It's a circle of lines that appear to rotate.

The problem I have now is the animation is a while loop, so as soon as I tell the refresh graphic class to animate, the program is stuck in that loop and no more operations take place. The data is never pulled from the web. Game over...
Threading will be needed to get 'more than one operation' (simultaneously) from the application. See this for a starting point.

EDIT:
Not seeing what's been done [no code?], it MAY be possible to change/update the graphic during the processing of the youtube retrieval, but depending on how the retrieval is handled might also slow the animation considerably.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 23-Sep-2009, 20:41
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to operations while an animation loop is running


Quote:
Originally Posted by LuciWiz
Tell me what you want the new title to be and I will do it for you.

Best regards,
Lucian

"How to continue program operation while animation loop is running" would work better if it fits. Thanks!
  #6  
Old 23-Sep-2009, 20:43
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to operations while an animation loop is running


Quote:
Originally Posted by TurboPT
Threading will be needed to get 'more than one operation' (simultaneously) from the application. http://java.sun.com/docs/books/tutorial/essential/concurrency/ for a starting point.

EDIT:
Not seeing what's been done [no code?], it MAY be possible to change/update the graphic during the processing of the youtube retrieval, but depending on how the retrieval is handled might also slow the animation considerably.

Thank you this is very enlightening. I'll post the code when I get home, if I can't get threading to work based on this documentation.
  #7  
Old 24-Sep-2009, 13:32
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to continue program operations while animation loop is running


I read through the documentation on that Sun site as well as several other tutorials and took a stab at it... unsuccessfully.

Unfortunately, some of the concepts referred to in those tutorials are still beyond my comprehension. I understand the concept of threading and I understand the difference between implementing it in another subclass versus extending the Thread class directly. I think my purposes are better served implementing Runnable in this case.

But, while the course has been very good for understanding general methodology in programming and understanding how to get Java to execute those concepts, it hasn't gotten into some of the information I would need to read this documentation and understand it. As a beginner programming course, the goal isn't to turn you into a Java expert as much as it is to expose you to general programming methodology.

For instance, init and run methods are covered, but I'm still 2 classes away from covering the main method. Perhaps that will help shed more light. But also the concept of how to implement a class within another subclass in a general way (where I could look at this documentation and immediately understand how to do it) is not covered and probably won't be touched upon since I'm nearly done with the course.

Here is the code for the "RefreshGraphic" class in question.

JAVA Code:
import java.awt.Color;
import acm.graphics.*;
import acm.program.*;


public class RefreshGraphic extends GraphicsProgram implements Runnable {
	GCompound gc = new GCompound();
	GLine[] lines;
	
	// constructor
	public RefreshGraphic() {
	}
	
	/** adds the refresh compound to the canvas
	 * @param size radius of the graphic
	 * @param sides number of lines in the graphic
	 */
	public GCompound addTo(double size, int sides) {
		double degrees = (double) 360 / sides;
		double angle = 0;
		double angleForComputation = 0;
		lines = new GLine[sides];
		
		for (int i = 0; i < sides; i++) {
			if (angle <= 90) angleForComputation = angle;
			if (angle <= 180 && angle > 90) angleForComputation = angle - 90;
			if (angle <= 270 && angle > 180) angleForComputation = angle - 180;
			if (angle < 360 && angle > 270) angleForComputation = angle - 270;
			
			double firstX = ((Math.cos(Math.toRadians(angleForComputation))) * size) / 2;
			double firstY = ((Math.sin(Math.toRadians(angleForComputation))) * size) / 2; 
			double secondX = (Math.cos(Math.toRadians(angleForComputation))) * size;
			double secondY = (Math.sin(Math.toRadians(angleForComputation))) * size;
			
			if (angle <= 90) {
				lines[i] = new GLine(firstX, -firstY, secondX, -secondY);
				lines[i].setColor(Color.BLUE);
			} else if (angle <= 180 && angle > 90) {
				lines[i] = new GLine(-firstY, -firstX, -secondY, -secondX);
				lines[i].setColor(Color.BLUE);
			} else if (angle <= 270 && angle > 180) {
				lines[i] = new GLine(-firstX, firstY, -secondX, secondY);
				lines[i].setColor(Color.BLUE);
			} else if (angle < 360 && angle > 270) {
				lines[i] = new GLine(firstY, firstX, secondY, secondX);
				lines[i].setColor(Color.BLUE);
			}
			
			angle += degrees;
		}
		
		for (int i = 0; i < sides; i++) {
			gc.add(lines[i]);
		}
		return gc;
	}
	
	/** Animates the RefreshGraphic
	 */
	public void animate() {
		int i = 0;
		while(true) {
			lines[i].setColor(Color.RED);
			if (i == 0) {
				lines[lines.length - 1].setColor(Color.WHITE);
				lines[lines.length - 2].setColor(Color.BLUE);
			} else if (i == 1) {
				lines[0].setColor(Color.WHITE);
				lines[lines.length - 1].setColor(Color.BLUE);
			} else {
				lines[i - 1].setColor(Color.WHITE);
				lines[i - 2].setColor(Color.BLUE);
			}
			pause(40);
			if (i < lines.length - 1) {
				i++;
			} else {
				i = 0;
			}
		}
	}
}

So the class that calls this just needs the constructor plus the following:

Code:
GCompound rg = refreshGraphic.addTo(20, 20); add(rg, x, y); refreshGraphic.animate();

The graphic is far from pretty, but I haven't learned any more advanced graphics concepts. But, it's close enough that the user knows what it represents.

That aside, the obvious problem in my program is the animate method contains an infinite loop.

I could step through the animation while another method retrieves the youtube data line by line, but that would almost certainly make the animation graphic look even junkier! It would be better to have the two processes happening concurrently and be able to interrupt the animation process when all the data is finished downloading.
Last edited by LuciWiz : 24-Sep-2009 at 13:51. Reason: Please insert your Java code between [java] & [/java] tags
  #8  
Old 24-Sep-2009, 17:13
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to continue program operations while animation loop is running


Sorry about that LuciWiz, used the code tag by accident. Thanks for the fix.
-e
  #9  
Old 25-Sep-2009, 19:44
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: How to continue program operation while animation loop is running


I'm having trouble with the acm package.

I've downloaded the acm.jar, but compiling still gives many errors.
So, do I need more acm stuff, or is there more that I might need to know?
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 25-Sep-2009 at 20:28.
  #10  
Old 26-Sep-2009, 01:55
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: How to continue program operation while animation loop is running


That's the entire code for the class including imports. I just checked and it runs fine here. I'm using Eclipse on a Mac and the acm stuff came with the software when I installed it.

I wrote a quick little tester program just to make sure. Displays fine (in all its intricate graphic beauty)

JAVA Code:
/* Tests RefreshGraphic
 */

import acm.graphics.GCompound;
import acm.program.GraphicsProgram;

public class TestRefreshGraphic extends GraphicsProgram {
	
	public void init() {
		GCompound gc = refresh.addTo(20, 20);
		add(gc, 100, 100);
		refresh.animate();
	}
	
	private RefreshGraphic refresh = new RefreshGraphic();
}
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:09.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.