2 Replies - 892 Views - Last Post: 08 August 2007 - 11:16 AM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Creating Threading in Applets

Posted 08 August 2007 - 01:12 AM

Ok, I only have one problem (that I know of...) with this code at line 33.

package welcomeapplet;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import javax.swing.*;

public class welcomeApplet extends JApplet {
	boolean isStandalone = false;
	BorderLayout borderLayout1 = new BorderLayout();

	//Get a parameter value
	public String getParameter(String key, String def) {
		return isStandalone ? System.getProperty(key, def) :
				(getParameter(key) != null ? getParameter(key) : def);
	}

	//Construct the applet
	public welcomeApplet() {
	}

	int x_pos = 10;
	int y_pos = 100;
	int radius = 20; 

	//Initialize the applet
	public void init(){ }
	public void start ()
	{

	  // LINE 33 BELOW!!! (define a new thread)
	  Thread th = new Thread(this);
	  // start this thread
	  th.start ();
	}
	public void stop() { }
	public void destroy() { }
	public void run()
	{	   
	  // lower ThreadPriority
	  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

	  // run along while(true) in this case always
	  while(true)
	  {
		  // changing the x - position of the ball/circle
		  x_pos ++;
		  
		  // repaint the applet
		  repaint();

		  try
		  {
			  // sleep for 20 milliseconds
			  Thread.sleep(20);
		  }
		  catch(InterruptedException ex)
		  {
			 // do nothing
		  }

		  // set ThreadPriority to maximum value
		  Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
	  }
	}
	public void paint (Graphics g) 
	{
	  // set color
	  g.setColor(Color.red);
	  
	  // paint a filled colored circle
	  g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); 
	  
	}
	
	{
		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//Component initialization
	private void jbInit() throws Exception {
		this.setSize(new Dimension(400, 300));
		this.getContentPane().setLayout(borderLayout1);
	}

	//Get Applet information
	public String getAppletInfo() {
		return "Applet Information";
	}

	//Get parameter info
	public String[][] getParameterInfo() {
		return null;
	}

	//Main method
	public static void main(String[] args) {
		welcomeApplet applet = new welcomeApplet();
		applet.isStandalone = true;

		JFrame frame = new JFrame();
		applet.isStandalone = true;
		//EXIT_ON_CLOSE == 3;
		frame.setDefaultCloseOperation(3);
	}

	//static initializer for setting look & feel
	static {
		try {
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
		}
	}
}



I think I need to use: public class welcomeApplet extends Thread
I tried it before "public void start ()" only to get another error. Anyone know what I need?

This post has been edited by Israel: 08 August 2007 - 01:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating Threading in Applets

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Creating Threading in Applets

Posted 08 August 2007 - 03:29 AM

try instead of Thread th = new Thread(this);
to put this Thread th = new Thread();
Was This Post Helpful? 0
  • +
  • -

#3 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Creating Threading in Applets

Posted 08 August 2007 - 11:16 AM

Yep, that took out the compiler error, but the red ball does move like it was supposed to. Oh well, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1