Applet Problems

Can't get my applet running?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1902 Views - Last Post: 19 October 2008 - 10:37 AM Rate Topic: -----

#1 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Applet Problems

Post icon  Posted 13 October 2008 - 10:53 PM

Hi, I have a problem with making a java applet.

I have coded a simple program and would like to see if I can put it on a webpage so others can try it, but after following the guides on java.sun I still get the "Applet notinited" problem. I have looked for solution on the Internet but I just don't get it.

Where could I possibly go wrong? I have even tried the example code on sun.java:

package betaTest;

import javax.swing.JApplet;
import java.awt.Graphics;

/**
 *
 * @author Raymond
 */
public class testApp extends JApplet {
	@Override
	public void paint(Graphics g) {
	g.drawRect(0, 0, 
		   getSize().width - 1,
		   getSize().height - 1);
		g.drawString("Hello world!", 5, 15);
	}
}



And in the web page source I've written:
<html>
<body>
<applet code=testApp.class width="200" height="200">
	</applet> 
</body>
</html>



Did I miss anything or do anything wrong?
Thanks for help > <"

Is This A Good Question/Topic? 0
  • +

Replies To: Applet Problems

#2 Unknown Hero  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 51
  • Joined: 04-September 07

Re: Applet Problems

Posted 14 October 2008 - 01:52 AM

Every applet has to have init() method.
Was This Post Helpful? 0
  • +
  • -

#3 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 14 October 2008 - 09:41 AM

View PostUnknown Hero, on 14 Oct, 2008 - 01:52 AM, said:

Every applet has to have init() method.


I've tried to add the init() method but I am not sure what to put in.
It says I can just leave the content blank like this?

public class testApp extends JApplet {
	@Override
	public void init(){
		
	}
	
	@Override
	public void paint(Graphics g) {
	g.drawRect(0, 0, 
		   getSize().width - 1,
		   getSize().height - 1);
		g.drawString("Hello world!", 5, 15);
	}
}


When I compile and run using Netbean 6.1 (both with and without the init() method) I can get the applet work, but when I tried to use a browser to open the applet, I keep getting the "Applet XXXXX notinited" problem.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 14 October 2008 - 08:36 PM

100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing
Was This Post Helpful? 0
  • +
  • -

#5 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 14 October 2008 - 10:55 PM

View Postpbl, on 14 Oct, 2008 - 08:36 PM, said:

100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing


I don't quite get it. So what do I have to write in init()? And does it mean I have to write the start() method as well?

Below is the code I've written so far.
It works when I run and compile on Netbean, but not on browsers :S

package betaTest;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

import javax.swing.JApplet;

/**
 *
 * @author Raymond
 */
public class possMain extends JApplet implements ActionListener {

	private JButton b1;
	private JTextArea textArea;
	private possCalc possRes;
	private final static String newline = "\n";

	@Override
	public void init() {
		setLayout(new GridBagLayout());

		possRes = new possCalc(30);

		possChance[] possNumList = {
			new possChance(10),
			new possChance(30),
			new possChance(50),
			new possChance(70),
			new possChance(90),
		};
		//Create the combo box
		JComboBox possList = new JComboBox(possNumList);
		possList.setSelectedIndex(1);
		possList.addActionListener(this);

		//Create text area to show generation details
		textArea = new JTextArea(20, 20);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);

		//Create the generate button
		b1 = new JButton("Generate");
		b1.setVerticalTextPosition(AbstractButton.CENTER);
		b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales

		b1.setMnemonic(KeyEvent.VK_G);
		b1.setActionCommand("generate");

		//Listen for actions on buttons 1 and 3.
		b1.addActionListener(this);

		b1.setToolTipText("Click to start Generation.");

		//Add content to the container
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridwidth = GridBagConstraints.REMAINDER;

		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(b1, gbc);

		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(possList, gbc);

		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		add(scrollPane, gbc);
	}

	@Override
	public void start() {
		System.out.println("Applet starting.");
	}

	@Override
	public void stop() {
		System.out.println("Applet stopping.");
	}

	@Override
	public void destroy() {
		System.out.println("Destroy method called.");
	}

	public void actionPerformed(ActionEvent e) {
		//JComboBox cb = (JComboBox) e.getSource();
		//possChance newPoss = (possChance) cb.getSelectedItem();
		//possRes.setPossHold(newPoss.getChance());

		if ("generate".equals(e.getActionCommand())) {
			possRes.startGen();
			textArea.append("(" + possRes.getPoss() + ") " + possRes.getRes() + newline);
		} else {
			JComboBox cb = (JComboBox) e.getSource();
			possChance newPoss = (possChance) cb.getSelectedItem();
			possRes.setPossHold(newPoss.getChance());

		}
	}
}


Same problem "Applet XXXXX notinitied" ... OTZ
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 15 October 2008 - 02:57 PM

View Postpbl, on 14 Oct, 2008 - 08:36 PM, said:

100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing

OK, I have checked... the init() and start() method in java.applet do nothing (both of them) so you don't really have to overload them
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 15 October 2008 - 06:24 PM

Wouldn't bet a month of salary on that but I guess your applet as a size of (0,0)

I assume that Netbean (as it does not call the Applet from a web page) does init it with a default width and heigh which is not the case when you call it from the .html page

Your init method must at least do

public void init() {
   int width = Integer.parseInt(getParameter("width"));
   int height = Integer.parseInt(getParameter("height"));
   getContentPane.setSize(widht, height);
}


Was This Post Helpful? 0
  • +
  • -

#8 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 16 October 2008 - 02:09 PM

View Postpbl, on 15 Oct, 2008 - 06:24 PM, said:

Wouldn't bet a month of salary on that but I guess your applet as a size of (0,0)

I assume that Netbean (as it does not call the Applet from a web page) does init it with a default width and heigh which is not the case when you call it from the .html page

Your init method must at least do

public void init() {
   int width = Integer.parseInt(getParameter("width"));
   int height = Integer.parseInt(getParameter("height"));
   getContentPane.setSize(widht, height);
}



I have tried again today, and here are the code and results...

package betaTest;

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Raymond
 */
public class NewAppletTest extends Applet{
	JButton button;
	
	/**
	 * Initialization method that will be called after the applet is loaded
	 * into the browser.
	 */
	@Override
	public void init() {
		setSize(200,200);
		setLayout(new BorderLayout(1,2));
		
		button = new JButton("Test Button");
		add("Center",button);
		
	}
}


I tried to keep things simple as for testing. It compiles and run with Netbean as shown below:
Posted Image

But when I tried on browser again (both IE and firefox) the applet just won't load.
Posted Image

Below is the log from java console:
Posted Image

And I honestly don't understand what it means the .class cannot be found :S
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 16 October 2008 - 02:52 PM

Is your .html file and your NewAppletTest.class files in the same directory by any chance ?
If it is the case you'll have to remove

package betaTest;

from your code.

Works for me...

or you'll have to put your .class in directory betaTest and say that code=betaTest.NewAppTest.class

Don't tell me we spent all these efforts to fix your problem for a stupid directory problem :-)

This post has been edited by pbl: 16 October 2008 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 16 October 2008 - 09:19 PM

You houu !mumeisyuu!!!
No news ? No thanks ?
Was This Post Helpful? 0
  • +
  • -

#11 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 17 October 2008 - 02:13 AM

View Postpbl, on 16 Oct, 2008 - 02:52 PM, said:

Is your .html file and your NewAppletTest.class files in the same directory by any chance ?
If it is the case you'll have to remove

package betaTest;

from your code.

Works for me...

or you'll have to put your .class in directory betaTest and say that code=betaTest.NewAppTest.class

Don't tell me we spent all these efforts to fix your problem for a stupid directory problem :-)


Sorry for my late reply > <

Yeah, they are in the SAME directory:
Posted Image

And I tried both removing the "package betaTest;" from the code or adding the the "betaTest.NewAppletTest.class" in the .html source~
But none works for me, still get the same problem OTZ...

Just another question, coz when I check the file in Netbean, it shows my NewAppletTest is a .java file instead of .class, could this be a reason? Or .java and .class are just purely the same?

Sorry for being so dumb and thanks for all you help > <"
Really appreciate~ <(_ _)>
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 17 October 2008 - 02:54 PM

Your Applet is on the Web.. and it works :D

http://www.pblinc.ca/numeisyuu/

So must be something wrong with your browsers.....

Code is also on the web page
Was This Post Helpful? 0
  • +
  • -

#13 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 17 October 2008 - 11:25 PM

View Postpbl, on 17 Oct, 2008 - 02:54 PM, said:

Your Applet is on the Web.. and it works :D

http://www.pblinc.ca/numeisyuu/

So must be something wrong with your browsers.....

Code is also on the web page


How come? O.O|||

Just a question~ is your code (for the Button class) in the format .class or .java? I meant, is it:

NewAppletTest.java

or

NewAppletTest.class

Because my codes are all in .java format, would this be a reason?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Applet Problems

Posted 18 October 2008 - 08:45 AM

View Postmumeisyuu, on 17 Oct, 2008 - 11:25 PM, said:

How come? O.O|||

Just a question~ is your code (for the Button class) in the format .class or .java? I meant, is it:

NewAppletTest.java

or

NewAppletTest.class

Because my codes are all in .java format, would this be a reason?


.class you do not recompile the source code everytime a web page is loaded
Was This Post Helpful? 0
  • +
  • -

#15 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Applet Problems

Posted 18 October 2008 - 12:36 PM

View Postpbl, on 18 Oct, 2008 - 08:45 AM, said:

View Postmumeisyuu, on 17 Oct, 2008 - 11:25 PM, said:

How come? O.O|||

Just a question~ is your code (for the Button class) in the format .class or .java? I meant, is it:

NewAppletTest.java

or

NewAppletTest.class

Because my codes are all in .java format, would this be a reason?


.class you do not recompile the source code everytime a web page is loaded


So I see I have been using the wrong file :S

Thanks for the help, just managed to build the .class file, though still having a little problem to get it together with the .html and make the applet works. But I will give it a try myself first and see if I can solve it. If not I shall come back and ask :(

Thanks for all your help :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2