I am trying to create a whack-a-mole type of game in Java but when I run my program the images do not show up.
Here is my code so far and I will add the images as well. I am also fairly new to coding so this might be an easy fix.
This is what I see when I run my program
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class hellokitty {
int screenWidth = 700;
int screenHeight = 800;
JFrame frame = new JFrame("Catch Hello Kitty!");
JLabel textL = new JLabel();
JPanel textP = new JPanel();
JPanel boardP = new JPanel();
JButton[] board = new JButton[9];
ImageIcon kittyIcon;
ImageIcon kuroIcon;
JButton currentKittyTile;
JButton currentKurTile;
Random r = new Random();
Timer setKittyTimer;
Timer setKuroTimer;
hellokitty(){
//frame.setVisible(true);
frame.setSize(screenWidth, screenHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textL.setFont(new Font("Comic Sans MS", Font.PLAIN, 70));
textL.setHorizontalAlignment(JLabel.CENTER);
textL.setText("Score: 0");
textL.setOpaque(true);
textP.setLayout(new BorderLayout());
textP.add(textL);
frame.add(textP, BorderLayout.NORTH);
boardP.setLayout(new GridLayout(3,3));
boardP.setBackground(Color.pink);
frame.add(boardP);
Image KittyImg = new ImageIcon(getClass().getResource("./kitty.gif")).getImage();
kittyIcon = new ImageIcon(KittyImg.getScaledInstance(150, 150, java.awt.Image.SCALE_SMOOTH));
Image KuronomiImg = new ImageIcon(getClass().getResource("./kuronomi.gif")).getImage();
kuroIcon = new ImageIcon(KuronomiImg.getScaledInstance(150, 150, java.awt.Image.SCALE_SMOOTH));
for (int i = 0; i < 9; i ++){
JButton tile = new JButton();
board[i] = tile;
boardP.add(tile);
// tile.setIcon(kuroIcon);
tile.setFocusable(false);
}
setKittyTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentKittyTile != null) {
currentKittyTile.setIcon(null);
currentKittyTile = null;
}
int num = r.nextInt(9);
JButton tile = board[num];
currentKittyTile = tile;
currentKittyTile.setIcon(kittyIcon);
}
});
setKuroTimer = new Timer(1500, new ActionListener() {
public void actionPerformed(ActionEvent e){
if (currentKurTile != null){
currentKurTile.setIcon(null);
currentKurTile = null;
}
int num = r.nextInt(9);
JButton tile = board[num];
currentKurTile = tile;
currentKurTile.setIcon(kuroIcon);
}
});
setKittyTimer.start();
setKuroTimer.start();
frame.setVisible(true);
}
}
First of all, the easiest way to use
getResourceis to put the resource in the same folder as the associated .class file. In your case that would mean putting files kitty.gif and kuronomi.gif in the same folder as file hellokitty.class. Then the URL of the resource can be obtained by calling:But even after you successfully obtain the resource, scaling the image as you do in your code will not work1. I am referring to these lines of your code:
Instead I used
BufferedImage. Refer to Why getScaledInstance() does not work?Here is my rewrite of your code – including a
mainmethod that uses a lambda expression. Also I changed some of the identifiers so that they adhere to Java naming conventions.Here is a screen capture of the running app. Of-course the tiles that contain images change all the time due to your timers.
Now I guess you need to write
ActionListeners to update the score whenever the user successfully "whacks a mole".1I could not discover why.