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
  #11  
Old 26-Sep-2009, 11:30
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


Here's specifically what you need to import in the RefreshGraphic class to get it to work

JAVA Code:
import java.awt.Color;
import acm.graphics.GCompound;
import acm.graphics.GLine;
import acm.program.GraphicsProgram;
  #12  
Old 26-Sep-2009, 11:40
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


Ok, I'll give a shot again (won't be at this very minute, though) and see how it goes.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #13  
Old 26-Sep-2009, 11:44
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


Yes, please take your time. Far from critical!

Thanks so much,
Ethan
  #14  
Old 26-Sep-2009, 13:44
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


I rewrote the TestRefreshGraphic code so that it requires some threading in order to operate. Basically, if you can show me how to rewrite this and/or the RefreshGraphic class, I think I'll understand how to implement threading.

JAVA Code:
/* Simple program that requires threading to function
 */

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

public class TestRefreshGraphic extends GraphicsProgram {
	
	public void run() {
		GLabel hello = new GLabel("Hello, world of threading!");
		add(hello, (getWidth() - hello.getWidth()) / 2, (getHeight() - hello.getHeight()) / 2);
		
		GCompound gc = refresh.addTo(20, 20);
		add(gc, getWidth() / 2, (getHeight() + gc.getHeight()) / 2);
		refresh.animate(); // while(true) loop that needs to be interrupted after pause
		
		pause(10000); // We never get here
		
		removeAll();
		GLabel fun = new GLabel("Now, wasn't that fun?");
		add(fun, (getWidth() - fun.getWidth()) / 2, (getHeight() - fun.getHeight()) / 2);
	}
	
	private RefreshGraphic refresh = new RefreshGraphic();
}

Your tips are very much appreciated!
-Ethan
  #15  
Old 29-Sep-2009, 08:20
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


Ok, after getting by an ISP issue, and working with another PC (the other I tried to use initially only has gcj, and it might not have everything necessary), but I was able to get your classes to compile (finally) and I saw the counter-clockwise 'spinner', so at least this is a step forward!

So, I can tinker with this more now, and I've also come to the realization that it's been about 5, if not 6, years since I've tinkered with Java threads, so I might be back to a small learning curve myself . Some threading things I do recall have been deprecated, so I'll have some relearning to do as well. (which is not a problem, a refresher is not a bad idea at this point)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #16  
Old 29-Sep-2009, 11:37
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


To my surprise (they were supposed to cover the "Main" method) the professor covered threading in the class I just watched. I was able to rewrite the RefreshGraphic class and the TestRefreshGraphic class so that it worked just fine.

I'll post in a few moments when I get in.

Sone methods are deprecated from what I could see in the javadocs. I believe start() & interrupt() are what are used to begin & end a thread, but I'd be the wrong guy to confirm this
  #17  
Old 29-Sep-2009, 12:01
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


So in the interest of closure, I'll post the code that implements threading and appears to function properly.

First off, I went into the RefreshGraphic class as posted on page 1 of this topic, and changed the "public void animate()" method to "public void run()" Otherwise that code stayed the same and it still implements Runnable.

Then I re-wrote the TestRefreshGraphic class:

JAVA Code:
/* Simple program that requires threading to function
 */

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

public class TestRefreshGraphic extends GraphicsProgram {
	
	public void init() {
		GLabel hello = new GLabel("Hello, world of threading!");
		add(hello, (getWidth() - hello.getWidth()) / 2, (getHeight() - hello.getHeight()) / 2);
		
		GCompound gc = refresh.addTo(20, 20);
		add(gc, getWidth() / 2, (getHeight() + gc.getHeight()) / 2);
		Thread t = new Thread(refresh);
		t.start();
		
		pause(10000);
		t.interrupt();
		
		removeAll();
		GLabel fun = new GLabel("Now, wasn't that fun?");
		add(fun, (getWidth() - fun.getWidth()) / 2, (getHeight() - fun.getHeight()) / 2);
	}
	
	private RefreshGraphic refresh = new RefreshGraphic();
}

TurboPT, I know you jumped through some hoops to help me out. Thanks so much for taking the time out and pointing me in the right direction!

Of course, if you see any improvements to the code, I'd welcome them in the interest of edumacatin myself.

Cheers!
-Ethan
  #18  
Old 29-Sep-2009, 12:06
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 got something working too, and here's what I came up with:
RefreshGraphic class:
[I reworked parts of addTo(), added two functions, renamed animate() to run()]
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;
    boolean go = false;
    Thread T;
	
	// constructor
	public RefreshGraphic() 
    {         
        T = new Thread(this);
    }
	
	/** 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 = 360.0 / sides;
        double radians = 0.0;
		double angle = 0.0;
		double angleForComputation = 0.0;
		lines = new GLine[sides];
		
		for (int i = 0; i < sides; i++) {
			if (angle <= 90) angleForComputation = angle;
			else if (angle <= 180) angleForComputation = angle - 90;
			else if (angle <= 270) angleForComputation = angle - 180;
			else  angleForComputation = angle - 270;
			
            radians = Math.toRadians(angleForComputation);
			
            double firstX = ((Math.cos(radians)) * size) / 2;
			double firstY = ((Math.sin(radians)) * size) / 2; 
			double secondX = firstX * 2;
			double secondY = firstY * 2;
			
			if (angle <= 90) {
				lines[i] = new GLine(firstX, -firstY, secondX, -secondY);
			} else if (angle <= 180) {
				lines[i] = new GLine(-firstY, -firstX, -secondY, -secondX);
			} else if (angle <= 270) {
				lines[i] = new GLine(-firstX, firstY, -secondX, secondY);
			} else {
				lines[i] = new GLine(firstY, firstX, secondY, secondX);
			}
			
            lines[i].setColor(Color.BLUE);
			angle += degrees;
			gc.add(lines[i]);
        }
		
		return gc;
	}
	
    public void killAnimation()
    {
        go = false;
    }
    
    public void startAnimation()
    {
        go = true;
        T.start();
    }
    
	/** Animates the RefreshGraphic
	 */
	public void run() {
		System.out.println("animate() entered!");
        int i = 0;
		while(go) {
			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;
			}
		}
		System.out.println("animate() exited!");
	}
}
...and the TestRefreshGraphic class:
[I added extra labels and pause()'s for brief viewing in the GUI output]
JAVA Code:
import acm.graphics.GCompound;
import acm.graphics.GLabel;
import acm.program.GraphicsProgram;

public class TestRefreshGraphic extends GraphicsProgram {
    private RefreshGraphic refresh = new RefreshGraphic();

	public void run()
    {
        GLabel hello = new GLabel("Hello, world of threading!");
		add(hello, (getWidth() - hello.getWidth()) / 2, (getHeight() - hello.getHeight()) / 2);
		
        GCompound gc = refresh.addTo(20, 20);
		add(gc, getWidth() / 2, (getHeight() + gc.getHeight()) / 2);
    
        refresh.startAnimation(); // while(go) loop that needs to be interrupted after pause [it will!]
        pause(1000);
        
		GLabel hello2 = new GLabel("Animation already Running!");
		add(hello2, (getWidth() - hello2.getWidth()) / 2, (getHeight() + gc.getHeight() + hello.getHeight() * 4) / 2);
        System.out.println("Surpassed Start!");

		pause(10000); // We do get past this point now.
        
        hello2.setLabel("Passed the 10-sec. pause");
		System.out.println("Surpassed Pause!");
		pause(2000);
        
        hello2.setLabel("Stopping Animation");
        refresh.killAnimation();
		pause(2000);

        removeAll();
		GLabel fun = new GLabel("Now, wasn't THAT fun?");
		add(fun, (getWidth() - fun.getWidth()) / 2, (getHeight() - fun.getHeight()) / 2);
    }
}
The deprecated functions I know are: stop(), suspend(), resume(), and destroy() -- all Thread class functions.

EDIT:
So, somewhere between startAnimation() and killAnimation() is where the youtube retrieval stuff would go.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #19  
Old 29-Sep-2009, 12:17
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


Oh, that's interesting. You put the threading into the RefreshGraphic class itself. Is that better style than putting it into the class that calls it?

In a more complicated program is it better to have classes manage their own threads like that, or is it better to have them all in the main class?

Thanks again,
Ethan
  #20  
Old 29-Sep-2009, 12:18
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


Quote:
EDIT:
So, somewhere between startAnimation() and killAnimation() is where the youtube retrieval stuff would go.

Yup, I've already extrapolated to my other program! It works!
 
 

Recent GIDBlogProblems with the Navy (Chiefs) 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 18:12.


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