11 Replies - 2130 Views - Last Post: 10 December 2009 - 12:20 AM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Applet isn't showing up

Posted 08 December 2009 - 06:09 PM

Hello all. How is everyone doing? I have wrote a program to display some national flags with information to go along with the pics. I have everything good for compiling, but I get a few errors when I run as applet.

Here is my DescriptivePanel class
import javax.swing.*;
import java.awt.*;

public class DescriptionPanel extends JPanel
{
	//Label for displaying an image icon and info
	private JLabel jlblImageTitle = new JLabel();
	
	//Test area for displaying text
	private JTextArea jtaDescription = new JTextArea();
	
	//Label properties
	public DescriptionPanel()
	{
		//Center the icon/text and place the text under the icon
		jlblImageTitle.setHorizontalAlignment(JLabel.CENTER);
		jlblImageTitle.setHorizontalTextPosition(JLabel.CENTER);
		jlblImageTitle.setVerticalTextPosition(JLabel.BOTTOM);
		
		//Set the font in the label/text field
		jlblImageTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
		jtaDescription.setFont(new Font("Serif", Font.PLAIN, 14));
		
		//Set linewrap/wrapStyleWord true for the text area
		//Wrapping Line
		jtaDescription.setLineWrap(true);
		//Wrapping Word
		jtaDescription.setWrapStyleWord(true);
		//Read only
		jtaDescription.setEditable(false);
		
		//Create a scroll pane to hold the text
		JScrollPane scrollPane = new JScrollPane(jtaDescription);
		
		//Set BorderLayout for the panel/add label and scrollpane
		setLayout(new BorderLayout(5, 5));
		add(scrollPane, BorderLayout.CENTER);
		add(jlblImageTitle, BorderLayout.WEST);
	}
	
	//The title
	public void setTitle(String title)
	{
		jlblImageTitle.setText(title);
	}
	
	//The Image Icon
	public void setImageIcon(ImageIcon icon)
	{
		jlblImageTitle.setIcon(icon);
	}
	
	//The description section
	public void setDescription(String text)
	{
		jtaDescription.setText(text);
	}
}


And here is my national flag program.


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

public class NationalFlag extends JApplet implements ActionListener
{
  //Description panel to display all information
  private DescriptionPanel infoPanel = new DescriptionPanel();
  
  //Declarations
  private int totalCountries = 0;
  private ImageIcon[] imageIcon;
  private String[] text;
  private String[] name;
  int current = 0;
  private Timer timer = new Timer(15000, this);

  //Initializing the applet
  public void init()
  {
	//Get the total countries parameter from the HTML page
	totalCountries = Integer.valueOf(getParameter("Countries")).intValue();
	System.out.println("Students " + totalCountries);

	//Create arrays for imageIcon, text and name
	imageIcon = new ImageIcon[totalCountries];
	text = new String[totalCountries];
	name = new String[totalCountries];

	//Initialize text, name and imageIcon
	for (int i = 0; i < totalCountries; i++)
	 {
		text[i] = getParameter("Description" + i);
		name[i] = getParameter("Name" + i);
	  imageIcon[i] = new ImageIcon(getClass().getResource("C:\\Users\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\image\\flag" + i + ".gif"));
	}

	//Set applet layout and add text area and panel
	getContentPane().add(infoPanel, BorderLayout.CENTER);
		show(current % totalCountries);

	//Starting the program
	timer.start();
  }

  //Start Timer
  public void start()
  {
	timer.start();
  }

  //Stop Timer
  public void stop() 
  {
	timer.stop();
  }

  //Run a slide show
  public void actionPerformed(ActionEvent e) {
	current = current + 1;
	   //Showing current slide  
	 show(current % totalCountries);	
  }

  //Display information
  private void show(int current)
  {
	//Show text
	infoPanel.setDescription(text[current]);
	//Show name
	infoPanel.setTitle(name[current]);
	//Show imageIcon
	infoPanel.setImageIcon(imageIcon[current]);
  }
}


When I run it, the applet viewer pops up, but at the bottom, it says "Start: applet not initialized", and "Applet" at the top in the white. This is what my run i/o has in it when I run it,

----jGRASP exec: appletviewer jgrasphta.htm

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.valueOf(Integer.java:553)
at NationalFlag.init(NationalFlag.java:28)
at sun.applet.AppletPanel.run(AppletPanel.java:417)
at java.lang.Thread.run(Thread.java:619)

----jGRASP: operation complete.



Any ideas???

Is This A Good Question/Topic? 0
  • +

Replies To: Applet isn't showing up

#2 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: Applet isn't showing up

Posted 08 December 2009 - 08:52 PM

Hope it is NationalFlag.class that is in your .html file and not DescriptionPanel.class ? :)
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Applet isn't showing up

Posted 08 December 2009 - 09:03 PM

Not related to your issue, but your code here makes me think about existence
	 // inside init()
	timer.start();
  }

  //Start Timer
  public void start()
  {
	timer.start();
  }



Just stick the timer.start() in one place, the start method.
Was This Post Helpful? 1
  • +
  • -

#4 lbarowski  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-December 09

Re: Applet isn't showing up

Posted 09 December 2009 - 02:16 PM

getParamater("Countries") returns null because there is no such parameter in the HTML.

1) Your code should handle this situation with at least a message to stderr, like "Error: Countries parameter is missing." and a clean exit (and the same goes for the Name parameter).

2) You need to modify the HTML used to launch the applet so it supplies the parameters you need. In jGRASP you can do that by creating a project and then modifying the HTML text in the compiler settings. Or you could create your own HTML file and launch from outside jGRASP.

Also, you should specify the icon image location with a relative path, so you can move your project (or turn it in) and it will still work.
Was This Post Helpful? 1
  • +
  • -

#5 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Applet isn't showing up

Posted 09 December 2009 - 03:32 PM

View Postpbl, on 8 Dec, 2009 - 07:52 PM, said:

Hope it is NationalFlag.class that is in your .html file and not DescriptionPanel.class ? :)



I didn't make the html file, I just used JGrasp and I ran as an applet....
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Applet isn't showing up

Posted 09 December 2009 - 05:10 PM

View Posttheautokustomizer, on 9 Dec, 2009 - 04:32 PM, said:

View Postpbl, on 8 Dec, 2009 - 07:52 PM, said:

Hope it is NationalFlag.class that is in your .html file and not DescriptionPanel.class ? :)



I didn't make the html file, I just used JGrasp and I ran as an applet....


If there's no hand-made html file, then there is no code parameter called Countries, thus throwing an exception here:
totalCountries = Integer.valueOf(getParameter("Countries")).


Was This Post Helpful? 0
  • +
  • -

#7 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: Applet isn't showing up

Posted 09 December 2009 - 05:20 PM

View PostDogstopper, on 9 Dec, 2009 - 04:10 PM, said:

If there's no hand-made html file, then there is no code parameter called Countries, thus throwing an exception here:

Thus an exception is throwwed... the init() method does not finish and the Applet is not initialized :)
Was This Post Helpful? 0
  • +
  • -

#8 lbarowski  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-December 09

Re: Applet isn't showing up

Posted 09 December 2009 - 06:05 PM

View Posttheautokustomizer, on 9 Dec, 2009 - 02:32 PM, said:

View Postpbl, on 8 Dec, 2009 - 07:52 PM, said:

Hope it is NationalFlag.class that is in your .html file and not DescriptionPanel.class ? :)



I didn't make the html file, I just used JGrasp and I ran as an applet....


Right, jGRASP makes a temporary one for you, but you can't change what is in it unless you create a project and add your source files. Ability to change the HTML for applets is only in the project settings. If you are doing this for a class assignment, you will probably have to turn in the HTML file anyway though, right?
Was This Post Helpful? 0
  • +
  • -

#9 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: Applet isn't showing up

Posted 09 December 2009 - 06:22 PM

View Postlbarowski, on 9 Dec, 2009 - 05:05 PM, said:

Right, jGRASP makes a temporary one for you, but you can't change what is in it unless you create a project and add your source files. Ability to change the HTML for applets is only in the project settings. If you are doing this for a class assignment, you will probably have to turn in the HTML file anyway though, right?

I can't imagine JGrasp, or any other IDE, to figure out what your Applet parameters should be
So you will have to eventually write the .html file
but for unit testing you can cheat :)
 
try {
	totalCountries = Integer.valueOf(getParameter("Countries"));
}
catch (Exception e) {
	// OK doing testing from JGrasp let's hardcode the value
	totalCountries = 1;
}


Was This Post Helpful? 0
  • +
  • -

#10 lbarowski  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-December 09

Re: Applet isn't showing up

Posted 09 December 2009 - 07:13 PM

View Postpbl, on 9 Dec, 2009 - 05:22 PM, said:

I can't imagine JGrasp, or any other IDE, to figure out what your Applet parameters should be
So you will have to eventually write the .html file


You can specify what is in the temporary HTML file used to run applets in jGRASP (a variable supplies the applet class name). That makes testing a bit more convenient, since you can then push a single button in jGRASP to run it. Also, there is no way to run the debugger on the applet using an external HTML file.
Was This Post Helpful? 0
  • +
  • -

#11 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: Applet isn't showing up

Posted 09 December 2009 - 07:42 PM

View Postlbarowski, on 9 Dec, 2009 - 06:13 PM, said:

View Postpbl, on 9 Dec, 2009 - 05:22 PM, said:

I can't imagine JGrasp, or any other IDE, to figure out what your Applet parameters should be
So you will have to eventually write the .html file


You can specify what is in the temporary HTML file used to run applets in jGRASP (a variable supplies the applet class name). That makes testing a bit more convenient, since you can then push a single button in jGRASP to run it. Also, there is no way to run the debugger on the applet using an external HTML file.

I'll continue using Eclipse :D
Was This Post Helpful? 0
  • +
  • -

#12 lbarowski  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-December 09

Re: Applet isn't showing up

Posted 10 December 2009 - 12:20 AM

View Postpbl, on 9 Dec, 2009 - 06:42 PM, said:

I'll continue using Eclipse :D


In this respect, they work about the same.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1