Hello I'm trying to do a ratsuk game that is chess but only with the knight.
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class Knight {
private Icon image;
private int w;
private int k;
private Random rand;
public Knight() {
image = new ImageIcon(getClass().getResource("redKnight.gif"));
w = rand.nextInt(9);
k = rand.nextInt(9);
}
public void Caballo(JButton[][] matriz, int i, int j) {
matriz[i][j].setIcon(image);
matriz[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
matriz[i][j].setBackground(Color.RED);
}
});
}
}
So I was trying to do a recursive method which I am not really sure will work.
But the problem is that inside the addActionListener, netbeans tells me that the variables have to be final which I don't really get why. Once I run it the image doesn't show at all
Here is the rest of the code
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JPanel;
import java.util.Random;
public class Tablero {
private JButton[][] mesa;
private Random rad;
public Tablero() {
mesa = new JButton[8][8];
}
public void cuadriculado(JFrame ventana) {
JPanel panel = new JPanel(new GridLayout(8, 8, 0, 0));
for (int i = 0; i < mesa.length; i++) {
for (int j = 0; j < mesa[0].length; j++) {
mesa[i][j] = new JButton();
mesa[i][j].setPreferredSize(new Dimension(40, 40));
panel.add(mesa[i][j]);
}
}
for (int r = 0; r < mesa.length; r++) {
for (int t = 0; t < mesa[0].length; t++) {
if (r % 2 == 0 || r == 0) {
if (t % 2 == 0 || t == 0) {
mesa[r][t].setBackground(Color.BLACK);
} else {
mesa[r][t].setBackground(Color.WHITE);
}
} else {
if (t % 2 == 0 || t == 0) {
mesa[r][t].setBackground(Color.WHITE);
} else {
mesa[r][t].setBackground(Color.BLACK);
}
}
}
}
ventana.setContentPane(panel);
ventana.setSize(500, 500);
ventana.setVisible(true);
Knight kn =new Knight();
kn.Caballo(mesa, rad.nextInt(9), rad.nextInt(9));
}
}
Any help will greatly appreciated. I am really new to Java and none of this was explained to me, so I have been struggling a lot.
matriz[i][j].setBackground(Color.RED);
You are trying to access "matriz" from an annonymous inner class so the variable needs to either be a class variable or a final variable.
I would question why you made this method belong to the Knight class. This method should be part of the Tablero class since that is where you define the array as a class variable. Then you won't have the compiler problem.
But if you really want to keep the method in the Knight class then the code should be:
public void Caballo(**final** JButton[][] matriz, int i, int j) {
Once i run it the image doesnt show at all
image = new ImageIcon(getClass().getResource("redKnight.gif"));
You just create an Icon. You need to add it to a label and then add the label to the GUI.
i am really new to java and nothing of this was explained to me
Start by reading the Swing tutorial. Maybe the section on How to Use Icons would be a good place to start.
Try changing this
public void Caballo(JButton[][] matriz, int i, int j)
to this
public void Caballo(final JButton[][] matriz, final int i, final int j)
The reason the variables need to final is because they are accessed inside the ActionListener's actionPerformed. Java needs to know that these variables arent going to change before it gets to use them. It goes back to a Java Garbage collector thing that I would prefer not ramble on about, but that's the jest of it.
Related
I was making a Nokia snake game. I have already made the Frame and everything and already written my algo. Currently I was doing something so that I can perform actions using my keys (such as left, right, up, down).
I made a class named Frame that extends JFrame and implements action listener, somewhere I saw on Stack Overflow that I need to write my code in key released function so as to make my code run while I press the key.
But when I press the keys nothing happened.
Here is my Java code:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Frame extends JFrame implements KeyListener {
private static final int BOARD_SIZE = 51;
private static final int FRAME_SIZE = 700;
private static final Color SNAKE = Color.GREEN;
private static final Color BOARD = Color.WHITE;
private static final Color FOOD = Color.BLUE;
private static enum DIRECTION {
right, left, up, down;
}
private static DIRECTION curr = DIRECTION.right;
public Frame() {
super.setTitle("<<SNAKE GAME>>");
super.setSize(this.FRAME_SIZE, this.FRAME_SIZE);
GridLayout layout = new GridLayout(this.BOARD_SIZE, this.BOARD_SIZE);
super.setLayout(layout);
int val = this.randomNum(this.BOARD_SIZE * this.BOARD_SIZE - 7);
int count = 0;
for (int i = 0; i < this.BOARD_SIZE; i++) {
for (int j = 0; j < this.BOARD_SIZE; j++) {
JButton btn = new JButton();
super.add(btn);
this.buttons[i][j] = btn;
if (i == this.BOARD_SIZE / 2 + 1 && j > this.BOARD_SIZE / 2 - 5
&& j <= this.BOARD_SIZE / 2 + 6) {
this.snakeLL.snake.AddFirst(btn);
btn.setBackground(this.SNAKE);
} else if (count == val) {
btn.setBackground(FOOD);
count++;
} else {
btn.setBackground(this.BOARD);
count++;
}
}
}
super.addKeyListener(this);
super.setResizable(false);
super.setVisible(true);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyLocation();
if (key == KeyEvent.VK_UP) {
this.curr = DIRECTION.up;
}
}
, somewhere i saw on stack overflow that i need to write my code in key released function so as to.. make my code run while i press the key...
I don't think you got that advice here.
We always advise that you should be using Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for basic information.
You can also check out Motion Using the Keyboard which contains a working example of using Key Bindings to animate a component.
I made a minesweeper game in Java using Swing, but when i run it, JFrame doesn't pop up? (No errors)
It runs, but no window shows up. Tried to debug with no success.
I really appreciate your help :)
Note that in class GameBoard:
imgs = new Image[13]; //Number of images used
//Loading images, used later
for (int i = 0; i < 13; i++) {
imgs[i] = (new ImageIcon(i + ".png")).getImage();
}
These lines load numbers representing images (13 images), created by me, and these imgs are loadid depending on the mouseadapter.
This is the MineSweeper class with the main method:
package minesweeper;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class MineSweeper extends JFrame {
public JLabel label;
public MineSweeper(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setLocationRelativeTo(null);
this.setTitle("Minesweeper");
label = new JLabel("");
this.add(label, BorderLayout.SOUTH);
GameBoard game = new GameBoard(label);
this.add(game);
setResizable(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MineSweeper jf = new MineSweeper();
jf.setVisible(true);
}
});
}
}
An this is the GameBoard class:
package minesweeper;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameBoard extends JPanel {
//Adding constant variables
private final int SIZE_OF_CELL = 20;
private static final int NUM_OF_ROWS = 20;
private static final int NUM_OF_CL = 20;
private final int SIZE_OF_FIELD = NUM_OF_ROWS * NUM_OF_CL;
//Adding other variables
private int NUM_OF_MINES_LEFT;
private Image[] imgs;
private static int[][] gameboard;
public static int mines = 40;
private int all_cells;
private static JLabel label;
boolean gamestarted = true; // Game is in progress, already started
//Constructor 1 parameter
public GameBoard(JLabel inputlabel) {
this.label = inputlabel;
imgs = new Image[13]; //Number of images used
//Loading images, used later
for (int i = 0; i < 13; i++) {
imgs[i] = (new ImageIcon(i + ".png")).getImage();
}
setDoubleBuffered(true);
addMouseListener(new Manager());
StartNewGame();
}
//Find mines around cell[i][j]
public static int FindMines(int ro, int co){
int cnt=0;
for(int y=-1;y<=1;y++){
for(int x = -1; x<=1;x++){
if(x==0 && y ==0) continue;
if(ro+y<0) continue;
if(co+x<0) continue;
if(gameboard[ro+y][co+x]==19) cnt++;
}
}
return cnt;
}
public static void StartNewGame(){
//NUM_OF_MINES_LEFT = 40; // Default value for number of mines is 30
gameboard = new int[20][20];
int minesleft=mines;
int row,col;
// int mines = NUM_OF_MINES_LEFT;
Random rng=new Random();
label.setText(Integer.toString(minesleft));
//initialize mine field
for(int i=0;i<20;i++){
for (int j=0;j<20;j++){
gameboard[i][j]=10;//default value is 10 -> its covered
}
}
//Set mines in random positions
while (mines>0){
row=rng.nextInt(NUM_OF_ROWS);
col=rng.nextInt(NUM_OF_CL);
if ((gameboard[row][col])!=19){
gameboard[row][col]+=9;;
minesleft--;
}
}
//Set numbers
for(int i=0;i<20;i++){
for (int j=0;j<20;j++){
if(gameboard[i][j]==19) gameboard[i][j]=19;
if(gameboard[i][j]==10){
gameboard[i][j]+= FindMines(i,j);
}
}
}
}
//public int FindEmptyCells(){
//}
#Override
public void paintComponent(Graphics grap){
int gamewon=0;
int[][] temp = new int[20][20];
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
temp[i][j]=gameboard[i][j];
if(gamestarted && temp[i][j]==9) gamestarted=false;
if(gamestarted == false){
if(temp[i][j]==19){
temp[i][j]= 9;
}else if(temp[i][j]==29){//10+11 for mark
temp[i][j]=11;
}else if(temp[i][j]>9){
temp[i][j]=10;
}
}else{
if (temp[i][j] > 19)
temp[i][j] = 11;
else if (temp[i][j] > 9) {
temp[i][j] = 10;
gamewon=1;
}
}
int toload= temp[i][j];
grap.drawImage(imgs[toload],j*15,i*15,this);
}
}
if(gamestarted==true && gamewon==0){
gamestarted=false;
label.setText("You won!");
}else if(gamestarted==false){
label.setText("You Lost!");
}
}
class Manager extends MouseAdapter{
#Override
public void mousePressed(MouseEvent ev){
boolean newpaint = false;
//Get event coordinates
int x= ev.getX();
int y= ev.getY();
int hit_cl= x / 15;
int hit_row= y/ 15;
if(gamestarted==false){
StartNewGame();
repaint();
}
if( (x < 20 * 15) && (y < 20 * 15) ){
if(ev.getButton() == MouseEvent.BUTTON3){
if(gameboard[hit_cl][hit_row] > 9){
newpaint=true;
if(gameboard[hit_cl][hit_row] <= 19){
if(mines > 0){
mines--;
String show=Integer.toString(mines);
gameboard[hit_cl][hit_row]+=11;
label.setText(show);
}else{
label.setText("Marks: 0");
}
}else{
mines++;
String show=Integer.toString(mines);
label.setText(show);
gameboard[hit_cl][hit_row]-=11;
}
}
}else{
if(gameboard[hit_cl][hit_row] > 19){
return;
}
if((gameboard[hit_cl][hit_row] > 9) && (gameboard[hit_cl]
[hit_row] <29)){
newpaint=true;
gameboard[hit_cl][hit_row]-=10;
if(gameboard[hit_cl][hit_row] == 9) gamestarted=false;
//if(gameboard[hit_cl][hit_row] == 10); //find_empty();
}
}
if(newpaint==true) repaint();
}
}
}
}
What did you do to debug? :)
The most obvious reason for the program to run forever is that it never leaves a loop.
while (mines>0){
row=rng.nextInt(NUM_OF_ROWS);
col=rng.nextInt(NUM_OF_CL);
if ((gameboard[row][col])!=19){
gameboard[row][col]+=9;;
minesleft--;
}
}
This part of your StartNewGame() method (GameBoard class) is causing an endless loop, mines variable is never updated inside the loop.
Giving a fast look to your code i can note some bad practices, for example :
You should not control the game state or setting labels text inside
paintComponent() method. This method is called automatically, and it
should only paint all the components. All the "logic" inside it
could slow down your program, because you can not control how many
times the method gets called (of course you can force the method to
be called with repaint() method, for example).
You should follow java naming conventions
Hope this helps :)
I have an int a; in the main class and i'd like to use it in the Action Listner class. I've checked for answers but didnt really understand what i had to do since I'm still just a begginer in any kind of programing so if its possible i'd really appreciate a simple solution.
here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Start {
public static void main(String[] args){
JFrame okno = new JFrame("Nonogram");
okno.setVisible(true);
okno.setSize(700, 700);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
okno.add(panel);
JButton [][] gumbi = new JButton[15][15];
JPanel polje = new JPanel(new GridLayout(15, 15));
panel.add(polje, BorderLayout.CENTER);
int a = 1;
int b = 1;
for(int i = 0; 0 < 15; i++){
for(int j = 0; j < 15; j++){
if(i < 5 && j < 5){
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.RED);
//gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
}else if(i < 5 || j < 5){
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.YELLOW);
//gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
gumbi[i][j].setEnabled(false);
}else{
if(Math.random() <= 0.6){
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("3");
polje.add(gumbi[i][j]);
}else {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("4");
polje.add(gumbi[i][j]);
}
}
if(gumbi[i][j].getText() == "3"){
a += 1;
}
if(i == 14 && j == 14){
gumbi[i][j].setText("" + a);
}
}
}
}
}
and this is what i have in Action Listener
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Listener implements ActionListener {
JButton[][] gumbi;
public Listener(JButton[][] gumbi) {
this.gumbi = gumbi;
}
public void actionPerformed(ActionEvent e){
JButton gumb = (JButton) e.getSource();
if( gumb.getBackground() == Color.WHITE){
gumb.setBackground(Color.BLACK);
} else if (gumb.getBackground() == Color.BLACK){
gumb.setBackground(Color.WHITE);
}
}
}
Thanks for your time.
Define that int a as static with public access and remove it's definition from within your main method as below:
public class Start {
public static int a = 1;//static since you want to use in static method
public static void main(String[] args){
And then in your listener call, you could use .variable like below:
int number = Start.a;
in your current implementation int a is defined in main method and anything declared in a method will be local to the method, you really have to get the basics right , for now u can define the int a public static int a outside the main method , and then access the variable with the class name reference as shown below
public class Start {
public static int a ;
public static void main() {
//your logic
}
}
public class Listener implements ActionListener {
int temp = Start.a;
}
While I agree with another answer that you can make a static in the main class, there might be a neater solution, even though that it is not exactly what was asked.
Instead of attempting to get the value of a from the main, it makes more sense to send along the value of a in the constructor of Listener.
public class Listener implements ActionListener {
JButton[][] gumbi;
final int a;
public Listener(JButton[][] gumbi, int a) {
this.gumbi = gumbi;
this.a = a;
}
...
In the main method simply change the method that adds the listener as follows:
gumbi[i][j].addActionListener(new Listener(gumbi, a));
I already checked this duplicate question and other similar ones and it didn't help. I am trying to add an png to a button when it is clicked. The program is a variable sized tic-tac-toe game for school.
Right now I have:
private ImageIcon X_MARK = new ImageIcon("x.png");
private ImageIcon O_MARK = new ImageIcon("o.gif");
private JButton[][] cells;
...
cells = new JButton[size][size];
JPanel board = new JPanel(new GridLayout(size, size));
board.setBorder(new LineBorder(Color.BLACK, 1));
ButtonListener listener = new ButtonListener();
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
cells[i][j] = new JButton();
cells[i][j].addActionListener(listener);
board.add(cells[i][j]);
}
JFrame ttt = new JFrame();
ttt.add(board);
ttt.setTitle("Show GUI Components");
ttt.setSize(60*size, 60*size);
ttt.setLocation(0, 0);
ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ttt.setVisible(true);
...
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (e.getSource() == cells[i][j]) {
if ((i + j) % 2 == 0) {
cells[i][j].setBackground(Color.GREEN);
cells[i][j].setIcon(X_MARK);
} else {
cells[i][j].setBackground(Color.CYAN);
cells[i][j].setIcon(O_MARK);
}
}
}
}
That is all the relevant code I think. I am using Eclipse and I have x.png and o.png in the src folder and the bin folder of the the project. I have also tried a couple of variants I have seen on SO and google searches like, new ImageIcon("C:/Users/BigBoy/workspace_1/EventDriven/src/x.png");, new ImageIcon("src/x.png");, and some other ones involving getClass().getResource among other things. I don't know what else to try. I know I've done this in the past and didn't have this much trouble.
I added .setBackground(Color.GREEN); just to make sure my clicks were registering properly and they are, the problem to me seems to be with the declaring/initializing of the ImageIcon.
NOTE: Right now my button listener just makes the checker board pattern, I will get to actually putting each player's mark after I figure out this icon problem.
You need to understand resources which is what you will want to use. They are located relative to the class files. If the images are with the class files, then
get your image as a resource
Create an ImageIcon from the Image.
i.e., something like:
package whateverpackeyouareusing;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class DefaultFoo {
public static void main(String[] args) throws IOException {
String resource = "x.png";
URL url = Class.class.getResource(resource);
BufferedImage img = ImageIO.read(url);
Icon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
}
}
Edit: A better example per Andrew Thompson:
package some.package;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class PlayWithImages {
public static final String X_RESOURCE = "x.png";
private Icon xIcon;
public PlayWithImages() throws IOException {
URL xImgUrl = getClass().getResource(X_RESOURCE);
Image xImg = ImageIO.read(xImgUrl);
xIcon = new ImageIcon(xImg);
}
public Icon getXIcon() {
return xIcon;
}
public static void main(String[] args) {
try {
PlayWithImages playWithImages = new PlayWithImages();
Icon xIcon = playWithImages.getXIcon();
JOptionPane.showMessageDialog(null, xIcon);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have a simple puzzle game. There is an image consisting of 16 tiles (randomly placed). Images are stored in an array and when game is launched they're added to main JPanel.
Game works in this way : Each image has atributes 'place' and 'number'. 'Place' is the current place on grid (either correct or not) and 'number' is the desired place for the image. When a user clicks image their 'place' and 'number' attributes are checked. If they match nothing happens. If not game checks if any image is currently in memory. If there is none, then this image's 'place' and 'number' are stored. If there is some image in memory, then the currently clicked image's 'plac'e is checked with stored image's 'number'. When they match - their places are exchanged. This part works properly. But now, I'm calling addComponent method on my JPanel with updated images and simply nothing happens. Shouldn't the new images be added to JPanel replacing the old ones ?
package Bonus;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
import java.awt.*;
class Puzzle extends JPanel implements ActionListener {
private int selected_nr=-1;
private int selected_pl=-1;
private boolean memory=false;
private static Img[] images;
public Puzzle(){
JFrame f = new JFrame("Smile");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.setSize(252,252);
f.setVisible(true);
setLayout(new GridLayout(4, 4));
images = new Img[16];
int[] buttons = new int[16];
for(int i=0; i<16; i++){
buttons[i] = i;
}
int rand;
int temp;
Random random;
random = new Random(System.currentTimeMillis());
for (int i = 0; i < buttons.length; i++) {
rand = (random.nextInt() & 0x7FFFFFFF) % buttons.length;
temp = buttons[i];
buttons[i] = buttons[rand];
buttons[rand] = temp;
}
for (int i = 0; i < 16; i++) {
images[i] = new Img(i, buttons[i]);
}
addComponents(images);
}
public void addComponents(Img[] im){
this.removeAll();
for(int i=0; i<16; i++){
im[i].addActionListener(this);
im[i].setPreferredSize(new Dimension(53,53));
add(im[i]);
}
this.validate();
}
public void actionPerformed(ActionEvent e) {
Img b = (Img)(e.getSource());
int num = b.getNumber();
int pl = b.getPlace();
if(!(b.rightPlace())){
if(memory){
if(pl == selected_nr){
images[pl].setPlace(selected_pl);
images[selected_pl].setPlace(selected_nr);
selected_nr = -1;
selected_pl = -1;
memory = false;
addComponents(images);
}
else{
System.out.println("Try other image");
}
}
else{
memory = true;
selected_nr = num;
selected_pl = pl;
}
}
else{
System.out.println("OK !");
}
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Puzzle();
}
});
}
}
class Img extends JButton {
int number;
int place;
ImageIcon img;
public Img(int p, int n){
number = n;
place = p;
img = new ImageIcon("u"+number+".jpg", BorderLayout.CENTER);
setIcon(img);
}
public boolean rightPlace(){
boolean correct=false;
if(number == place){
correct = true;
}
return correct;
}
public void setPlace(int i){
place = i;
}
public int getNumber(){
return number;
}
public int getPlace(){
return place;
}
}
EDIT: Changed the code to use the answers, but still no luck. addComponents() gets updated images[] but doesn't revalidate them.
Rather than relying on precut image files, here's an example of slicing an existing image and shuffling the resulting pieces. It combines the helpful (+1) suggestions of both #Frederick and #akf.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ImageLabelPanel extends JPanel implements ActionListener {
private static final int N = 4;
private final List<JLabel> list = new ArrayList<JLabel>();
private final Timer timer = new Timer(1000, this);
ImageLabelPanel() {
this.setLayout(new GridLayout(N, N));
BufferedImage bi = null;
try {
bi = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
int w = bi.getWidth() / N;
int h = bi.getHeight() / N;
BufferedImage b = bi.getSubimage(c * w, r * h, w, h);
list.add(new JLabel(new ImageIcon(b)));
}
}
createPane();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
timer.start();
}
private void createPane() {
this.removeAll();
for (JLabel label : list) add(label);
this.validate();
}
#Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list);
createPane();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ImageLabelPanel();
}
});
}
}
You are adding all of your components again to your JPanel without actually removing any of them. In your addComponents() method, I would first call removeAll(). You might want to rename that method to highlight the side-effects, as it no longer would only be adding components. Perhaps, resetComponents() would be better.
After changing the components, you need to 'refresh' the Swing component by calling invalidate() or revalidate().