Hello!
I have to make a "Dice Applet" for school. It is all finished but the dice pictures do not display properly. It only appears in a line (not the full image.) But they seem to display fine if you maximize the applet then restore the applet to its original size.
Can some body figure out how to solve this?
*Note: We have to use Image Icons (Although it would be easier to just get the image and draw it in the graphics class. which we were told to delete.)*
Thanks!
// The "DiceApplet" class.
import javax.swing.*; //Class with JButtons and JLabels
import java.awt.*; //Art class
import java.awt.event.*; //Class that handels user clicks
import java.applet.Applet; //Make applets
public class DiceApplet extends Applet
implements ActionListener
{
// Place instance variables here
JButton RollButton;
ImageIcon Dice1, Dice2, Dice3, Dice4, Dice5, Dice6;
double rand;
JLabel instructions, image, dicepic;
public void init ()
{
//JLabel instructions = new JLabel ("Click \"roll to roll the dice!");
instructions = new JLabel ("Click the button to roll the dice");
add (instructions);
RollButton = new JButton ("Roll");
RollButton.addActionListener (this);
RollButton.setActionCommand ("roll");
add (RollButton);
dicepic = new JLabel (" ");
add (dicepic);
Dice1 = createImageIcon ("Dice1.jpg");
Dice2 = createImageIcon ("Dice2.jpg");
Dice3 = createImageIcon ("Dice3.jpg");
Dice4 = createImageIcon ("Dice4.jpg");
Dice5 = createImageIcon ("Dice5.jpg");
Dice6 = createImageIcon ("Dice6.jpg");
// Place the body of the initialization method here
} // init method
public void actionPerformed (ActionEvent e) //When user clicks action (below)is preformed
{
if (e.getActionCommand ().equals ("roll")) //If user clicked on the add button
{
rand = Math.random () * 6 + 1;
rand = Math.rint (rand);
if (rand == 1)
{
dicepic.setIcon (Dice1);
}
else if (rand == 2)
{
dicepic.setIcon (Dice2);
}
else if (rand == 3)
{
dicepic.setIcon (Dice3);
}
else if (rand == 4)
{
dicepic.setIcon (Dice4);
}
else if (rand == 5)
{
dicepic.setIcon (Dice5);
}
else if (rand == 6)
{
dicepic.setIcon (Dice6);
}
}
}
protected ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = getClass ().getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
} // DiceRoll class