9 Replies - 3216 Views - Last Post: 20 November 2009 - 05:40 PM Rate Topic: -----

#1 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Start Applet Not Initialized

Post icon  Posted 18 November 2009 - 03:01 PM

Can someone please tell me why I'm getting the error message, "Start applet not initialized" when I try to run my program.
import java.awt.*;
import java.applet.*;

@SuppressWarnings("serial")
public abstract class Animation extends Applet implements Runnable {
	
		Thread runner;
		int Xpos = 500;
		
		public void start(){
		if(runner == null) {
			runner = new Thread(this);
			runner.start();
		}
	}

public void stop(){
if(runner != null)
	runner = null;
	}
	

public void run() {
	while(true) {
		try {
			Thread.sleep(30);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		} 
	
	}

public void paint(Graphics g) {
	g.drawString("Hello Java World", Xpos, 30);
	Xpos --;
	if (Xpos <- 200);
		Xpos = 500;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Start Applet Not Initialized

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Start Applet Not Initialized

Posted 18 November 2009 - 03:56 PM

I believe that in applets, you are required to have an init method, public void init(). This is the equavalent to a main method in standard java programs.
Was This Post Helpful? 1
  • +
  • -

#3 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Start Applet Not Initialized

Posted 18 November 2009 - 05:05 PM

View Postnick2price, on 18 Nov, 2009 - 02:56 PM, said:

I believe that in applets, you are required to have an init method, public void init(). This is the equavalent to a main method in standard java programs.


Thank you. Now my next question is getting the text to scroll from right to left. How do I accomplish this?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Start Applet Not Initialized

Posted 18 November 2009 - 06:38 PM

Your class Animation is an abstract class
You cannont make an instance of it
So you cannot make an Animation object out of it
so your .html file cannot start/init an Applet out of it
Was This Post Helpful? 0
  • +
  • -

#5 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Start Applet Not Initialized

Posted 19 November 2009 - 07:17 AM

View Postpbl, on 18 Nov, 2009 - 05:38 PM, said:

Your class Animation is an abstract class
You cannont make an instance of it
So you cannot make an Animation object out of it
so your .html file cannot start/init an Applet out of it


I don't quite understand what you are telling me. Could you explain further? Start by clarifying what abstract means. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Start Applet Not Initialized

Posted 19 November 2009 - 05:13 PM

View Postrollsroyce, on 19 Nov, 2009 - 06:17 AM, said:

View Postpbl, on 18 Nov, 2009 - 05:38 PM, said:

Your class Animation is an abstract class
You cannont make an instance of it
So you cannot make an Animation object out of it
so your .html file cannot start/init an Applet out of it


I don't quite understand what you are telling me. Could you explain further? Start by clarifying what abstract means. Thanks.

If you don't you what absract means wht did you write

public abstract class Animation extends Applet implements Runnable {

at the first place ?

http://java.sun.com/...I/abstract.html

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
   // declare fields
   // declare non-abstract methods
   abstract void draw();
}



When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Remove the keyword abstract from your class declaration and it might work
Was This Post Helpful? 0
  • +
  • -

#7 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Start Applet Not Initialized

Posted 20 November 2009 - 08:25 AM

View Postpbl, on 19 Nov, 2009 - 04:13 PM, said:

View Postrollsroyce, on 19 Nov, 2009 - 06:17 AM, said:

View Postpbl, on 18 Nov, 2009 - 05:38 PM, said:

Your class Animation is an abstract class
You cannont make an instance of it
So you cannot make an Animation object out of it
so your .html file cannot start/init an Applet out of it


I don't quite understand what you are telling me. Could you explain further? Start by clarifying what abstract means. Thanks.

If you don't you what absract means wht did you write

public abstract class Animation extends Applet implements Runnable {

at the first place ?

http://java.sun.com/...I/abstract.html

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
   // declare fields
   // declare non-abstract methods
   abstract void draw();
}



When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Remove the keyword abstract from your class declaration and it might work



In the first place, my IDE was showing an error. It gave me the option of making the class abstract and so I did just to see what would happen without knowing what abstract really means. Anyway, thank you for your clarity on abstract classes. I have since taken the abstract modifier out. What do I need to do inorder to get the "Hello Java World" to move from right to left or left to right? Should I post my new code up?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Start Applet Not Initialized

Posted 20 November 2009 - 04:04 PM

You have to call repaint() after you sleep so your paint() method will be called

   while(true) {
		try {
			Thread.sleep(30);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		repaint();	<---- force repaint


Might work but it is not the good/recommended way of doing it
You should use a Swing Timer

Get rid of your start() and run() method and have your class to implement ActionListener rather than runnable


public class Animation extends Applet implements ActionListener {
	
		int Xpos;
	
		public init() {
		   Xpos = 500;
		   Timer timer = new Timer(500, this);
		   timer.start();
		}

		public void actionPerformed(ActionEvent e) {
			repaint();
		}

		public void paint(......
}



Happy coding
Was This Post Helpful? 0
  • +
  • -

#9 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Start Applet Not Initialized

Posted 20 November 2009 - 04:40 PM

View Postpbl, on 20 Nov, 2009 - 03:04 PM, said:

You have to call repaint() after you sleep so your paint() method will be called

   while(true) {
		try {
			Thread.sleep(30);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		repaint();	<---- force repaint


Might work but it is not the good/recommended way of doing it
You should use a Swing Timer

Get rid of your start() and run() method and have your class to implement ActionListener rather than runnable


public class Animation extends Applet implements ActionListener {
	
		int Xpos;
	
		public init() {
		   Xpos = 500;
		   Timer timer = new Timer(500, this);
		   timer.start();
		}

		public void actionPerformed(ActionEvent e) {
			repaint();
		}

		public void paint(......
}



Happy coding



Hi. Thanks for the input. However, when I used your code the animation did not work. The words just sit there and not move. If I need to repost the code, just let me know. Can you tell me what I'm doing wrong?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Start Applet Not Initialized

Posted 20 November 2009 - 05:40 PM

Yes repost your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1