I write a method to apply Icon to JButton
, but it doesn't apply. I create JButton
array, after I use ToolKit
to getImage()
. After, I use ImageIcon
to apply new Imag
e into my button, but my game have a problem; when launch it not show ImageIcon
.
package mypack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class puzzleGame {
private JFrame jf;
private JPanel jpnlButton, jpnlPicture, jpnlMenu;
private JButton [][] jbt = new JButton[3][3];
private JLabel textSample, sample, time, move;
private int numTime = 0, numMove = 0;
GridLayout glButton = new GridLayout(3,3);
GridLayout glMenu = new GridLayout(5,1);
GridLayout glPicture = new GridLayout(1,1);
Font font = new Font(null, Font.BOLD, 20);
private JButton newgame, exit, music;
// Images
String[] urlImages = new String[]{ // link url images in my game
"images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg",
"images/6.jpg",
"images/7.jpg",
"images/8.jpg",
"images/9.jpg"
};
public puzzleGame(){
createMyGUI();
}
private void createMyGUI() {
jf = new JFrame("Picture Puzzle Game");
jf.setSize(700, 500);
jf.setLocation(300, 100);
jf.setResizable(false);
jf.setLayout(null);
// lam viec voi panel Button
jpnlButton = new JPanel();
jpnlButton.setSize(450, 450);
jpnlButton.setLocation(10, 10);
jpnlButton.setLayout(glButton);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
jbt[i][j] = new JButton();
jpnlButton.add(jbt[i][j]);
}
for(int i= 0; i<3; i++)
for(int j=0; j<3; j++)
readImages(jbt[i][j]);
// Label nam ke Panel Picture
textSample = new JLabel("Sample Picture");
textSample.setSize(200,50);
textSample.setFont(font);
textSample.setLocation(510,10);
// lam viec void panel Picture
jpnlPicture = new JPanel();
jpnlPicture.setSize(250,250);
jpnlPicture.setLocation(480, 30);
jpnlPicture.setLayout(glPicture);
sample = new JLabel();
sample.setFont(font);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image im = toolkit.getImage("images/game.jpg");
Image pic = im.getScaledInstance(200, 200, Image.SCALE_DEFAULT);
ImageIcon ic = new ImageIcon(pic);
sample.setIcon(ic);
jpnlPicture.add(sample);
// lam viec voi Panel Menu
jpnlMenu = new JPanel();
jpnlMenu.setSize(200,180);
jpnlMenu.setLocation(480, 270);
jpnlMenu.setLayout(glMenu);
newgame = new JButton("New Game");
exit = new JButton("Exit");
music = new JButton("Music");
move = new JLabel("Move: " + numMove);
time = new JLabel("Time: " + numTime);
move.setFont(font);
move.setHorizontalAlignment(SwingConstants.CENTER);
time.setFont(font);
time.setHorizontalAlignment(SwingConstants.CENTER);
jpnlMenu.add(move);
jpnlMenu.add(time);
jpnlMenu.add(newgame);
jpnlMenu.add(music);
jpnlMenu.add(exit);
jf.add(textSample);
jf.add(jpnlButton);
jf.add(jpnlPicture);
jf.add(jpnlMenu);
jf.setVisible(true);
}
private int randomURL() // random images
{
Random rd = new Random();
int max = 8, min = 0;
int num = rd.nextInt((max-min)+1)+min;
return num;
}
private void readImages(JButton bt){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image im = toolkit.getImage(urlImages[randomURL()]);
Image pic = im.getScaledInstance(150, 150, Image.SCALE_DEFAULT);
ImageIcon ic = new ImageIcon(pic);
//ImageIcon ic = new ImageIcon(urlImages[randomURL()]);
bt.setIcon(ic); // apply image but it's not working
}
public static void main(String [] args)
{
puzzleGame pu = new puzzleGame();
}
}
This is my link images if you want to test: Drive
And finally, I dont know how to use key narrow to swap image JButton
in the puzzle game.