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

Edited 5 Years Ago by javanoob101: n/a

After calling setIcon, call validate and/or repaint on the component (or, even better, on the container that contains the component).

Thank-you masijade for your help!
I put in the validate(); command in my code, and now the images are displayed properly!

Thank-you so much!

This question has already been answered. Start a new discussion instead.