Hey all wonder if you can spot my mistake or point me in the correct direction. I'm new to Java applets after spending quite some time learning/using standard applications. This simple program is basically converting a standard application into an applet, but for the life of me i can't see where i'm going wrong. I've got two classes dieTest and appletMain. The idea is that appletMain calls dieTest and adds the dieTest Jpanel to the appletMain container. But objects on my diePanel don't seem to be appearing when calling it from within appletMain. I've tried running the dieTest as a single applet with a init() function and it works fine, showing all the objects, but when i try to call the dieTest from within the appletMain class i get nothing. If someone could spot where i'm going wrong i would be most grateful!

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JApplet;

public class appletMain extends JApplet{
	private dieTest fDieTest;
	
	public void init(){
		fDieTest = new dieTest();
		
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		c.add(fDieTest);
	}

}
/**
 * Class used to create a value for the die amount, both graphically and numerically. Class also used to physically
 * roll the dice, using a die button.
 * @param dieImage used to represent die value as an image
 * @param dieValTF JTextField used for input of die value (numerically)
 * @param rollBtn JButton used to roll the die and invoke the methods to display the die amount (graphically and numerically)
 * @param dieImageLabel used to create an instance of the buffered image dieImage
 * @param numDieLabel used to add the text "Numerical Die Value"
 * @param graDieLabel used to add the text "Graphical Die Value"
 * @param dieNum variable used to store the value of the die, initially set to 1
 * @author Vincent Ashby-Smith
 */
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class dieTest extends JPanel implements ActionListener {
	static Random rand = new Random();
	private BufferedImage dieImage;
	private JTextField dieValTF;
	private JButton rollBtn;
	private JLabel dieImageLabel, numDieLabel, graDieLabel;
	private int dieNum = 1;

	/**
	 * Sole constructor for DiePanel class. Creates several components (JTextField, JButton and JLabels)
	 * and adds them to the panel. Constructor also invokes displayDieValue() and displayDie() methods.
	 */
	public void dieTest(){

		// DieValTF JTextField properties
		dieValTF = new JTextField();
		dieValTF.setPreferredSize(new Dimension(32, 32));
		dieValTF.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
		dieValTF.setBackground(Color.white);
		dieValTF.setForeground(Color.gray);
		dieValTF.setHorizontalAlignment(JTextField.CENTER);
		dieValTF.setEditable(false);

		// RollBtn JButton properties
		rollBtn = new JButton("Roll Die!");
		rollBtn.setPreferredSize(new Dimension(80, 32));
		rollBtn.setBackground(new Color(204, 204, 204));
		rollBtn.setForeground(Color.gray);
		rollBtn.addActionListener(this);

		// NumDieLabel, GraDieLabel, DieImage, dieImageLabel JLabel properties
		numDieLabel = new JLabel("Numerical Die Value:");
		numDieLabel.setForeground(Color.gray);

		graDieLabel = new JLabel("Graphical Die Value:");
		graDieLabel.setForeground(Color.gray);

		dieImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
		dieImageLabel = new JLabel(new ImageIcon(dieImage));
		dieImageLabel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));

		// Add components to panel
		add(graDieLabel);
		add(dieImageLabel);
		add(Box.createHorizontalStrut(76));
		add(numDieLabel);
		add(dieValTF);
		add(rollBtn);

		// Run methods
		displayDieValue();
		displayDie();
	}
	/**
	 * Method used to draw the dots of the die, according to the die value, using the fillOval method inherited from
	 * the graphics class. Method disposes of this graphics context once it is no longer referenced.
	 * Code adapted from: http://www.leepoint.net/notes-java/examples/graphics/rolldice/rolldice.html
	 */
	void displayDie() {
		Graphics g = dieImage.getGraphics();
		try {
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 32, 32);
			g.setColor(Color.gray);
			switch (dieNum) {
			case 1:
				g.fillOval(12, 12, 7, 7); // middle dot
				break;
			case 2:
				g.fillOval(3, 3, 7, 7); // top left dot
				g.fillOval(21, 21, 7, 7); // bottom right dot
				break;
			case 3:
				g.fillOval(3, 3, 7, 7); // top left dot
				g.fillOval(12, 12, 7, 7); // middle dot
				g.fillOval(21, 21, 7, 7); // bottom right dot
				break;
			case 4:
				g.fillOval(3, 3, 7, 7); // top left dot
				g.fillOval(3, 21, 7, 7); // bottom left dot
				g.fillOval(21, 3, 7, 7); // top right dot
				g.fillOval(21, 21, 7, 7); // bottom right dot
				break;
			case 5:
				g.fillOval(3, 3, 7, 7); // top left dot
				g.fillOval(3, 21, 7, 7); // bottom left dot
				g.fillOval(12, 12, 7, 7); // middle dot
				g.fillOval(21, 3, 7, 7); // top right dot
				g.fillOval(21, 21, 7, 7); // bottom right dot
				break;
			case 6:
				g.fillOval(3, 3, 7, 7); // top left dot
				g.fillOval(3, 12, 7, 7); // middle left dot
				g.fillOval(3, 21, 7, 7); // bottom left dot
				g.fillOval(21, 3, 7, 7); // top right dot
				g.fillOval(21, 12, 7, 7); // middle right dot
				g.fillOval(21, 21, 7, 7); // bottom right dot
				break;
			}
			repaint();
		} finally {
			g.dispose();
		}
	}

	/**
	 * Method used to roll the die and create a value for the dieNum variable using the rand function. Method also
	 * invokes to other methods when run; displayDieValue() and displayDie().
	 */
	void rollDie() {
		dieNum = rand.nextInt(6) + 1;
		displayDieValue();
		displayDie();
	}

	/**
	 * Method used to get the dieNum value.
	 * @return the value of DieNum
	 */
	public int getDie() {
		return dieNum;
	}

	/**
	 * Method used to set the value of dieNum.
	 */
	public void setDie(int dieNum) {
		this.dieNum = dieNum;
	}

	/**
	 * Method used to enable the roll button after it has been disabled.
	 */
	public void enableRollBtn() {
		rollBtn.setEnabled(true);
	}

	/**
	 * Method used to disable the roll button, for example when the player lands on an obstacle square, therefore they
	 * must answer the question correctly before being able to move on, so the roll button is disabled
	 */
	public void disableRollBtn() {
		rollBtn.setEnabled(false);
	}

	/**
	 * Method used to set the text of the JTextField (dieValTF) with the value of dieNum after converting it from
	 * a int to a String.
	 */
	void displayDieValue() {
		dieValTF.setText(String.valueOf(dieNum));
	}

	/**
	 * Method that is invoked when an action occurs, in this case when the roll button is pressed. When the roll button is
	 * pressed this method invokes the rollDie() method and two external methods; dieMove() and questReset().
	 */
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == rollBtn) {
			rollDie();
		}
	}
}

Recommended Answers

All 2 Replies

Here is the main problem

public void dieTest(){

Constructors don't specify a return type. What you wrote there is a method, not a constructor. Drop the "void" and you're good to go.

Hahah good spot there, had forgot about that, always helps to have a fresh set of eyes! Thanks very much!

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.