Search code examples
javaswingappletjbutton

Why don't these images show up?


I am working on a slot machine in Java and so far I created a button that will randomly generate two pictures. For some reason one of the pictures shows up while the other picture does not. I do not understand the issue as the code for both of these pictures is exactly the same. Here is my code. Please help!

import java.applet.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.imageio.*;
import java.net.URL;
import java.io.*;
import java.awt.image.*;





public class slotmachine extends JApplet implements Runnable {
  JButton b1 = new JButton("START");
  JPanel p;
  int int1, int2;
  BufferedImage img= null;
  BufferedImage img2 = null;
  BufferedImage img3 = null;
  BufferedImage img4 = null;
  BufferedImage img5 = null;
  BufferedImage img6 = null;
  BufferedImage img7 = null;
  BufferedImage img8 = null;
  BufferedImage img9 = null;
  BufferedImage img10 = null;
  public slotmachine(){
    init();    
  }


  public void init() {

    this.setLayout(null);
    this.setSize(10000,10000);

    b1.setBounds(100,100,100,100);
    b1.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e){
        Random random1 = new Random();
        int1 = random1.nextInt(10);
        Random random2 = new Random();
        int2 = random2.nextInt(10);
        repaint();

      }


    });


    getContentPane().add(b1);

    try {

      img = ImageIO.read(new File("question.png"));
      img2 = ImageIO.read(new File("banana.png"));
      img3 = ImageIO.read(new File("chocolate.png"));
      img4 = ImageIO.read(new File("icecream.png"));
      img5 = ImageIO.read(new File("bell.png"));
      img6 = ImageIO.read(new File("apple.png"));
      img7 = ImageIO.read(new File("money.png"));
      img8 = ImageIO.read(new File("number-7.png"));
      img9 = ImageIO.read(new File("necklace.png"));
      img10 = ImageIO.read(new File("gloves.png"));
    } catch (IOException e) {
    }  

    repaint();

    this.setVisible(true);  
  }


  public void paint(Graphics g) {
    super.paintComponents(g);
    g.drawString("Int 1 is" + int1,30,30);
        g.drawString("Int 2 is" + int2,30,80);
    switch (int1) {
      case 0:

        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img, 300, 500, this);
        break;
      case 1:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img2,300,500,this);
        break;
      case 2:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img3,300,500,this);
        break;
      case 3:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img4,300,500,this);
        break;
      case 4:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img5,300,500,this);
        break;
      case 5:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img6,300,500,this);
        break;
      case 6:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img7,300,500,this);
        break;
      case 7:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img8,300,500,this);
        break;
      case 8:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img9,300,500,this);
        break;
      case 9:
        g.setColor(Color.white);
        g.fillRect(300,300,300,500);
        g.drawImage(img10,300,500,this);
        break;

    }


    switch (int2) {
      case 0:

        g.setColor(Color.white);
        g.fillRect(300,300,800,500);

        g.drawImage(img, 800, 500, this);
        break;
      case 1:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img2,800,500,this);
        break;
      case 2:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img3,800,500,this);
        break;
      case 3:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img4,800,500,this);
        break;
      case 4:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img5,800,500,this);
        break;
      case 5:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img6,800,500,this);
        break;
      case 6:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img7,800,500,this);
        break;
      case 7:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img8,800,500,this);
        break;
      case 8:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img9,800,500,this);
        break;
      case 9:
        g.setColor(Color.white);
        g.fillRect(300,300,800,500);
        g.drawImage(img10,800,500,this);
        break;

    }

        this.setVisible(true);


  }

}

Solution

  • Instead of extending JApplet and overriding it paint and init methods just use a JLabel and set it to have an image icon.

    For example.

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.ImageIcon;
    
    public class Frame extends javax.swing.JFrame{
    
    //Random varible
    private Random rand = new Random();
    //Array List to hold the file names and locations
    private ArrayList<File> pictures;
    
    //Class call
    public Frame(){
        initComponents();
    }
    
    //Generates the gui. I Used netbeans to make this for me.                        
    private void initComponents() {
    
        pictureLable1 = new javax.swing.JLabel();
        pictureLable2 = new javax.swing.JLabel();
        generateButton = new javax.swing.JButton();
    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });
    
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pictureLable1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(pictureLable2, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(151, 151, 151)
                .addComponent(generateButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pictureLable2, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                    .addComponent(pictureLable1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 139, Short.MAX_VALUE)
                .addComponent(generateButton)
                .addContainerGap())
        );
    
        pack();
    }                   
    
    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        this.generate();
    }                                              
    
    /**
     * Generates the picture and then sets the labels accordingly.
     */
    public void generate(){
        //Init the array list
        pictures = new ArrayList<>();
    
        //Add the pictures to the array list
        pictures.add(new File("C:/Users/UserName/Desktop/TestPictures/Picture1.png"));
        pictures.add(new File("banana.png"));
        pictures.add(new File("chocolate.png"));
        pictures.add(new File("icecream.png"));
        pictures.add(new File("bell.png"));
        pictures.add(new File("apple.png"));
        pictures.add(new File("money.png"));
        pictures.add(new File("number-7.png"));
        pictures.add(new File("necklace.png"));
        pictures.add(new File("gloves.png"));
    
        //Gets the random number for the picture
        int number = rand.nextInt((pictures.size() -1/*Max*/ - 0/*min*/) + 1) + 0/*Min*/;
    
        //Set the image icon to a randomly chosen image
        this.pictureLable1.setIcon(new ImageIcon(pictures.get(number).getAbsolutePath()));
    
        //Gets the random number again for the second picture
        number = rand.nextInt((pictures.size() -1/*Max*/ - 0/*min*/) + 1) + 0/*Min*/;
    
        //Set the second image icon to a randomly chosen image
        this.pictureLable1.setIcon(new ImageIcon(pictures.get(number).getAbsolutePath()));
    }
    
    /**
     * Your run of mill main method.
     *
     * @param args
     */
    public static void main(String args[]){
        new Frame().setVisible(true);
    }
    
    // Variables declaration                
    public javax.swing.JButton generateButton;
    public static javax.swing.JLabel pictureLable1;
    public static javax.swing.JLabel pictureLable2;
    
    }
    

    Hope this helps.

    Edit:

    Make sure you set the path name like the first image so java knows where the image is.

    Also try not to name your classes with a non-capital letter in the beginning, Use Frame instead of frame, just good practice is all:).