View Single Post
Old 10-25-2005, 02:28 PM   #1 (permalink)
see-seA
Registered User
 
Join Date: Oct 2005
Posts: 2
see-seA is on a distinguished road
JApplet giving me grief - plz assist

I am busy working on an Applet that is meant to work on the web. The thing is that if I run the code as is, it works on my computer when run locally, however it refuses to work when I upload the class files to a webserver and try run the applet through a browser (on my comp I use AppletViewer).

Please could someone do me a HUGE favour and take a look at the [simplified] code below and let me know what I've been doing wrong.

Much appreciated.

Frogger.java
Code:
// <applet code="Frogger.class" width="465" height="400"></applet>

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

public class Frogger extends JApplet 
{

	private ArrayList<Car> cars;
	private Frog frog;
	private ImageIcon tree;

		
	public void init() 
	{		
		
		// places cars into arraylist on init
		cars = new ArrayList<Car>();
		Car car;
		
		for( int i = 0; i < 12; i++ )
		{
			if( i <= 3 || i >= 8 )
			{
				car = new Car(2);
				cars.add( car );
			}
			else if( i >= 4 && i <= 7 )
			{
				car = new Car(1);
				cars.add( car );
			}
		}
		
		// setup the frog
		frog = new Frog();

		
		// setup trees
		tree = new ImageIcon( "images/tree.gif" );
	}
	
	public void paint( Graphics page ) 
	{
		setBackground( new Color(21,205,56) );
		
		// trees
		tree.paintIcon( this, page, 20, 10 );
		tree.paintIcon( this, page, 320, 10 );
		tree.paintIcon( this, page, 70, 330 );
		tree.paintIcon( this, page, 270, 330 ); 
		
		// frog
		frog.getIcon().paintIcon( this, page, 50, 50 );
		
		// cars
		cars.get(2).getIcon().paintIcon( this, page, 100, 100 );
		cars.get(7).getIcon().paintIcon( this, page, 200, 200 );
	}
}
Car.java
Code:
import javax.swing.*;
import java.util.*;
import java.awt.*;



public class Car
{

	private ImageIcon carPic;
	private int direction;
	
	public Car( int way ) 
	{
		Random generator = new Random();
		int type = generator.nextInt( 4 ) + 1;		
		direction = way;

		
	
		if( type == 1 && direction == 1 )
			carPic = new ImageIcon( "images/car1left.gif" );
			
		else if( type == 1 && direction == 2 )
			carPic = new ImageIcon( "images/car1right.gif" );

		else if( type == 2 && direction == 1 )
			carPic = new ImageIcon( "images/car2left.gif" );
			
		else if( type == 2 && direction == 2 )
			carPic = new ImageIcon( "images/car2right.gif" );

		else if( type == 3 && direction == 1 )
			carPic = new ImageIcon( "images/car3left.gif" );

		else if( type == 3 && direction == 2 )
			carPic = new ImageIcon( "images/car3right.gif" );
			
		else if( type == 4 && direction == 1 )
			carPic = new ImageIcon( "images/car4left.gif" );

		else if( type == 4 && direction == 2 )
			carPic = new ImageIcon( "images/car4right.gif" );

		
	}
	

	public ImageIcon getIcon()
	{
		return carPic;
	}
	
	
}
Frog.java
Code:
import javax.swing.*;
import java.awt.event.*;


public class Frog extends JApplet
{
	private ImageIcon frogPic;
	
	public Frog()
	{
		frogPic = new ImageIcon( "images/frog.gif" );

	}
	

	public ImageIcon getIcon()
	{
		return frogPic;
	}
	

}

Thanks so much.
see-seA is offline   Reply With Quote