Hey guys, I'm for the most part a backend programmer but I'm taking a GUI class as a elective, and I'm haveing some problems with an applet. Part of the assigment is to create a logo design that displays a bunch of pentagons in a circle with different colors and rotating. I made the applet and I was able to run it under my IDE's applet simulator; however, once I throw the code in an html, I get the notinited error. Pretty much all of the work is done in the paint container, and I was under the impression that if all you have is a container in your code, then you do not need to include an init method (I've done it this way before with smaller applets) A friend suggested I might need to include a specific jar file in the html, but he couldn't remember the name of the jar. If thats not the problem then it must mean I would need an init method, but I'm not too sure how to put that in so it works. Any advise would be greatly appreciated.
Here's my code:

import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.util.Random;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pentagons extends JApplet{
	public void init(){
		
	}
	public void paint(Graphics g){
		super.paint(g);
		Random random = new Random();
		int xPoints[] = {0, 25, 75, 100, 50};
		int yPoints[] = {50, 100, 100, 50, 0};
		
		Graphics2D g2d = (Graphics2D) g;
		GeneralPath pentagon = new GeneralPath();
		pentagon.moveTo(xPoints[0], yPoints[0]);
		
		for (int count =1; count< xPoints.length; count++)
			pentagon.lineTo(xPoints[count], yPoints[count]);
		pentagon.closePath();
		g2d.translate(200, 200);
		for (int i =1; i <= 20; i++){

			g2d.rotate(Math.PI/ 10.0);
			g2d.setColor(new Color( random.nextInt(256),
			     random.nextInt(256), random.nextInt(256)));	
			g2d.fill(pentagon);
		
		}
		
	}

}

thanx
matt

package projectA;



import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.util.Random;
import java.jama;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pentagons extends JApplet {
	public int xPoints[] = new int [5];
	public int yPoints[] = new int [5];
	
	public void init() {
			xPoints[0] = 0;
			xPoints[1] = 25;
			xPoints[2] = 75;
	        xPoints[3] = 100;
	       	xPoints[4] = 50;
			yPoints[0] = 50;
	        yPoints[1] = 100;
	        yPoints[2] = 100;
	        yPoints[3] = 50;
	        yPoints[4] = 0;	
	}
	public void paint(Graphics g){
		super.paint(g);
		Random random = new Random();
		
		
		Graphics2D g2d = (Graphics2D) g;
		GeneralPath pentagon = new GeneralPath();
		pentagon.moveTo(xPoints[0], yPoints[0]);
		
		for (int count =1; count< xPoints.length; count++)
			pentagon.lineTo(xPoints[count], yPoints[count]);
		pentagon.closePath();
		g2d.translate(200, 200);
		for (int i =1; i <= 20; i++){

			g2d.rotate(Math.PI/ 10.0);
			g2d.setColor(new Color( random.nextInt(256),
			     random.nextInt(256), random.nextInt(256)));	
			g2d.fill(pentagon);
		
		}
		
	}
}

Ok so I've tried a few more things with this and included a init() method but I'm still getting the same error, and now I'm really out of ideas... anyone have any thoughts please let me know ... i need some more things to try ... thanx

Ok, so I got it running, I figure I would post the working code for anyone interested ....

import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.util.Random;
import javax.swing.JApplet;

public class Pentagons extends JApplet {
	
	public int xPoints[] = new int [5];
	public int yPoints[] = new int [5];
	
	public Pentagons() {
	}
	/**Initialize the applet*/
	
	public void init() {
		try {
			jbInit();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void jbInit() {
		xPoints[0] = 0;
		xPoints[1] = 25;
	       	xPoints[2] = 75;
	        xPoints[3] = 100;
	       	xPoints[4] = 50;
		yPoints[0] = 50;
	        yPoints[1] = 100;
	        yPoints[2] = 100;
	        yPoints[3] = 50;
	        yPoints[4] = 0;	
	}

	public void paint(Graphics g){
		super.paint(g);
		Random random = new Random();
		
		
		Graphics2D g2d = (Graphics2D) g;
		GeneralPath pentagon = new GeneralPath();
		pentagon.moveTo(xPoints[0], yPoints[0]);
		
		for (int count =1; count< xPoints.length; count++)
			pentagon.lineTo(xPoints[count], yPoints[count]);
		pentagon.closePath();
		g2d.translate(200, 200);
		for (int i =1; i <= 20; i++){

			g2d.rotate(Math.PI/ 10.0);
			g2d.setColor(new Color( random.nextInt(256),
			     random.nextInt(256), random.nextInt(256)));	
			g2d.fill(pentagon);
		
		}
	}	

	/**Main method*/
	public static void main(String[] args) {
		Pentagons applet = new Pentagons();
		applet.init();
		applet.start();
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.