My program complies fine but whenever I run it the applet loads but its blank and at the bottom it gives me Applet Not Initialized. I have no idea whats causing this if someone could help me out would be greatly appreciated.

The Errors im getting are:

at javax.swing.ImageIcon.(init)(ImageIcon.java:167)
at TourPanel.(init)(tour.java:52)
at Tour.init(tour.java:12)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)

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

public class Tour extends JApplet
{
	
	public void init()
	{
		add(new TourPanel());		
	}
}

class TourPanel extends JPanel
{

	private JPanel panel, buttonPanel, centerPanel;
	private JButton next, prev;
	
	private int numImages = 5;
	private int currentImage = 0;
	private ImageIcon[] pics = new ImageIcon[numImages];;
	
	public TourPanel()
	{
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.blue);
		add(panel);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2,15,15));
		buttonPanel.setBackground(Color.red);
		
		ButtonHandler bHandler = new ButtonHandler();
		
		prev = new JButton("<< Previous Image");
		prev.addActionListener(bHandler);
		buttonPanel.add(prev);
		
		next = new JButton("Next Image >>");
		next.addActionListener(bHandler);
		buttonPanel.add(next);
		
		panel.add(buttonPanel, BorderLayout.SOUTH);
		
		for(int x = 0; x < numImages; x++)
		{
			URL url = this.getClass().getResource("image/pic" + x + ".jpg");
			pics[x] = new ImageIcon(url);
		}
	}
	
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String cmd = e.getActionCommand();
			
			if (cmd.equals("<< Previous Image") && currentImage > 0)
			{
				currentImage--;
			}
			else if (cmd.equals("Next Image >>") && currentImage < numImages -1)
			{
				currentImage++;
			}
			
			repaint();
				
		}
	}	
	
	public void paintComponent(Graphics g)
	{
		Dimension d = getSize();
		g.drawImage(pics[currentImage].getImage(), 50, 70, d.width/2, d.height/2, this);

		repaint();
	}

}

My HTML file is:

<html>
<! NOTE - html code for Tour.java>
<head>
<title>Tour</title>
</head>
<body>
<applet  code=Tour.class width=800 height=800>

</applet>
</body>
</html>

My program complies fine but whenever I run it the applet loads but its blank and at the bottom it gives me Applet Not Initialized. I have no idea whats causing this if someone could help me out would be greatly appreciated.

The Errors im getting are:

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

public class Tour extends JApplet
{
	
	public void init()
	{
		add(new TourPanel());		
	}
}

class TourPanel extends JPanel
{

	private JPanel panel, buttonPanel, centerPanel;
	private JButton next, prev;
	
	private int numImages = 5;
	private int currentImage = 0;
	private ImageIcon[] pics = new ImageIcon[numImages];;
	
	public TourPanel()
	{
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.blue);
		add(panel);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2,15,15));
		buttonPanel.setBackground(Color.red);
		
		ButtonHandler bHandler = new ButtonHandler();
		
		prev = new JButton("<< Previous Image");
		prev.addActionListener(bHandler);
		buttonPanel.add(prev);
		
		next = new JButton("Next Image >>");
		next.addActionListener(bHandler);
		buttonPanel.add(next);
		
		panel.add(buttonPanel, BorderLayout.SOUTH);
		
		for(int x = 0; x < numImages; x++)
		{
			URL url = this.getClass().getResource("image/pic" + x + ".jpg");
			pics[x] = new ImageIcon(url);
		}
	}
	
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String cmd = e.getActionCommand();
			
			if (cmd.equals("<< Previous Image") && currentImage > 0)
			{
				currentImage--;
			}
			else if (cmd.equals("Next Image >>") && currentImage < numImages -1)
			{
				currentImage++;
			}
			
			repaint();
				
		}
	}	
	
	public void paintComponent(Graphics g)
	{
		Dimension d = getSize();
		g.drawImage(pics[currentImage].getImage(), 50, 70, d.width/2, d.height/2, this);

		repaint();
	}

}

My HTML file is:

<html>
<! NOTE - html code for Tour.java>
<head>
<title>Tour</title>
</head>
<body>
<applet  code=Tour.class width=800 height=800>

</applet>
</body>
</html>

Hello,

I changed some things around in your code to where now, the applet will start but when you click one of your buttons, it doesn't seem to do whatever it is supposed to do. Honestly, I'm just a student myself so I don't even fully understand what I did to get it to that point but here's what I came up with, maybe it will help a little. If not, I understand. LOL :)

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

public class Tour extends JApplet implements ActionListener
{
	
	
	JPanel buttonPanel = new JPanel();
	int numImages = 5;
	int currentImage = 0;
	ImageIcon[] pics = new ImageIcon[numImages];;
	JButton	prev = new JButton("<< Previous Image");
	JButton next = new JButton("Next Image >>");	
	JPanel panel = new JPanel();
	ButtonHandler bHandler = new ButtonHandler();
	
	public void init()
	{
		
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.blue);
		add(panel);
		
		
		buttonPanel.setLayout(new GridLayout(1,2,15,15));
		buttonPanel.setBackground(Color.red);
		
		
		
		
		prev.addActionListener(bHandler);
		buttonPanel.add(prev);
		
		
		next.addActionListener(bHandler);
		buttonPanel.add(next);
		
		panel.add(buttonPanel, BorderLayout.SOUTH);
		
				
		validate();
	}	
	
	
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			for(int x = 0; x < numImages; x++)
			{
				URL url = this.getClass().getResource("image/pic" + x + ".jpg");
				pics[x] = new ImageIcon(url);
				validate();
			}

			
			String cmd = e.getActionCommand();
			
			if (cmd.equals("<< Previous Image") && currentImage > 0)
			{
				currentImage--;
			}
			else if (cmd.equals("Next Image >>") && currentImage < numImages -1)
			{
				currentImage++;
			}
			
			
			validate();	
		}
	}	
	
	public void paintComponent(Graphics g)
	{
		Dimension d = getSize();
		g.drawImage(pics[currentImage].getImage(), 50, 70, d.width/2, d.height/2, this);
		validate();
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		
			String cmd = e.getActionCommand();
			
			if (cmd.equals("<< Previous Image") && currentImage > 0)
			{
				currentImage--;
			}
			else if (cmd.equals("Next Image >>") && currentImage < numImages -1)
			{
				currentImage++;
			}
			
			repaint();
			validate();	

		
	}

}

I also happened to notice that it looks like you are working with more advanced stuff than I am and I have a thread near yours that people have viewed but no one has replied to. It's for this week's assignment and I have been working on it the whole week. I have until tomorrow at 10:00pm to get it working and submitted so I'm getting a bit desperate. I was wondering if you get a moment, could you take a look at it and see if you have any suggestions for me?

It's supposed to ask the user a question such as "what is 6 times 7?", let them enter their answer, and then they click the button to check the answer. If it's wrong, it draws the string "No. Please try again." (It does all that ok) But then when the answer is right, it draws the string "Very good!!!" but it draws it over the other one instead of replacing it. It should also reset the random numbers when they get the right answer and it doesn't do that either. I've tried repaint() but it just hides the strings that are supposed to be drawn. Anyway, here's my code:

import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class NewJApplet extends JApplet implements ActionListener
{
   public Graphics brush;
	public Graphics brush1;
	Random rand = new Random();
	int number1 = rand.nextInt(9) + 1;
	int number2 = rand.nextInt(9) + 1;
	JLabel question = new JLabel("What is " + number1 + " times " + number2 + "?");
	JTextField answer = new JTextField(3);
	JButton checkAnswer = new JButton("Check Answer");
	Font font1 = new Font("Teen", Font.BOLD, 30);
	Font font2 = new Font("Teen", Font.ITALIC, 36);
	String right = "Very good!!!";
	String wrong = "No.  Please try again.";
	Container con = getContentPane();
	

	public void init()
	{	
		
		setLayout(new FlowLayout());
		con.setBackground(Color.BLUE);
		question.setLocation(20, 20);
		question.setFont(font1);
		con.add(question);
		answer.setLocation(20, 40);
		con.add(answer);
		checkAnswer.setLocation(20, 60);
		con.add(checkAnswer);
		




		checkAnswer.addActionListener(new ActionListener()
		{
			
			public void actionPerformed(ActionEvent e)
			{
				
				int ans = Integer.parseInt(answer.getText());
				
				if(ans == number1 * number2)
				{

					answer.setText("");
					Random rand = new Random();
					int number1 = rand.nextInt(9) + 1;
					int number2 = rand.nextInt(9) + 1;
					brush1 = getGraphics();
					brush1.setFont(font2);
					brush1.setColor(Color.YELLOW);
					brush1.drawString(right, 20, 120);
					validate();
				}

				if(ans != number1 * number2)
				{
					
					answer.setText("");
					brush = getGraphics();
					brush.setFont(font2);
					brush.setColor(Color.RED);
					brush.drawString(wrong, 20, 120);
					
					validate();
				}
				
			
			}
			
		}
		
		);
		
	}
	

	@Override
	public void actionPerformed(ActionEvent e)
	{
		
		answer.setText("");
		Random rand = new Random();
		int number1 = rand.nextInt(10);
		int number2 = rand.nextInt(10);
		
	}
	
}

Hi,

I know I had asked if you had any thoughts on my code but I did eventually get it figured out. Just thought I'd add that in case you don't check this site often. Anyway, I hope your program works out. Good luck!

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.