SlideShare a Scribd company logo
1 of 6
Download to read offline
Whenever I run my application, my Game appears with the picture cut up and shuffled. Whenever
I click a button to move the tiles, I get this error:
Cannot invoke "javax.swing.JPanel.removeAll()" because "this.this$0.panel" is null
How do I fix this?
code starts here:
package slidePuzzle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Game extends JFrame {
// declare variables, arrays, and constants
private Buttons emptyButton;
private ArrayList<Buttons> buttons;
private ArrayList<Point> answer;
private final int TOTAL_BUTTONS = 12;
private final int TOTAL_WIDTH = 400;
private JPanel panel;
private Image img;
private BufferedImage src;
private BufferedImage mod;
private int width;
private int height;
// default constructor
public Game() {
// call initializer
initializeInterface();
}
private void initializeInterface() {
answer = new ArrayList<Point>();
buttons = new ArrayList<Buttons>();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setLayout(new GridLayout(4, 3, 0, 0));
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
answer.add(new Point(x, y));
}
}
try {
src = loadImg();
int height = modifyHeight(src.getWidth(), src.getHeight());
mod = resizeImg(src, TOTAL_WIDTH, height, BufferedImage.TYPE_INT_ARGB);
}
catch (IOException exception) {
JOptionPane.showMessageDialog(this, "Image is unable to be rendered.", "Error",
JOptionPane.ERROR_MESSAGE);
}
height = mod.getHeight(null);
width = mod.getWidth(null);
for (int x = 0; x < 4; x++) {
for (int y = 0; y <3; y++) {
img = createImage(new FilteredImageSource(mod.getSource(),
new CropImageFilter(x * width / 3, y * height / 4, (width / 3), height / 4)));
Buttons button = new Buttons(img);
button.putClientProperty("position", new Point(x, y));
if (x == 3 && y == 2) {
emptyButton = new Buttons();
emptyButton.setBorderPainted(false);
emptyButton.setContentAreaFilled(false);
emptyButton.setEmptyButton();
emptyButton.putClientProperty("position", new Point(x, y));
} else {
buttons.add(button);
}
}
}
Collections.shuffle(buttons);
buttons.add(emptyButton);
for (int z = 0; z < TOTAL_BUTTONS; z++) {
Buttons button = buttons.get(z);
panel.add(button);
button.setBorder(BorderFactory.createLineBorder(Color.black));
button.addActionListener(new Click());
}
JFrame frame = new JFrame("Tile Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Game());
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
private BufferedImage loadImg() throws IOException {
BufferedImage bufferedImage = ImageIO.read(new File("src/pond.jpg"));
return bufferedImage;
}
private BufferedImage resizeImg(BufferedImage natural, int width, int height, int type) {
BufferedImage adjustedImg = new BufferedImage(width, height, type);
Graphics2D graphics = adjustedImg.createGraphics();
// draw image to fit inside our window and return
graphics.drawImage(natural, 0, 0, width, height, null);
graphics.dispose();
return adjustedImg;
}
private int modifyHeight(int width, int height) {
double widthRatio = TOTAL_WIDTH / (double) width;
int modHeight = (int) (height * widthRatio);
return modHeight;
}
private class Click extends AbstractAction {
@Override
public void actionPerformed(ActionEvent event) {
check(event);
isCorrectConfiguration();
}
private void check(ActionEvent event) {
int emptyButtonIndex = 0;
for (Buttons btn: buttons) {
if (btn.getEmptyButton()) {
emptyButtonIndex = buttons.indexOf(btn);
}
}
JButton btn = (JButton) event.getSource();
int buttonIndex = buttons.indexOf(btn);
if ((buttonIndex + 1 == emptyButtonIndex) ||
(buttonIndex + 3 == emptyButtonIndex) ||
(buttonIndex - 1 == emptyButtonIndex) ||
(buttonIndex - 3 == emptyButtonIndex)) {
Collections.swap(buttons, emptyButtonIndex, buttonIndex);
update();
}
}
private void update() {
panel.removeAll();
for (JComponent btn: buttons) {
panel.add(btn);
}
panel.validate();
}
}
public void isCorrectConfiguration() {
ArrayList<Point> userState = new ArrayList<Point>();
for (JComponent btn: buttons) {
userState.add((Point) btn.getClientProperty("position"));
}
if (comparePuzzleState(answer, userState)) {
JOptionPane.showMessageDialog(panel, "You have completed the puzzle!");
}
}
public static boolean comparePuzzleState(ArrayList<Point> list1, ArrayList<Point> list2) {
return list1.toString().contentEquals(list2.toString());
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Game slidePuzzle = new Game();
slidePuzzle.setVisible(true);
});
}
}
public class Buttons extends JButton {
private boolean isEmptyButton = false;
public boolean getEmptyButton() {
return isEmptyButton;
}
public void setEmptyButton() {
isEmptyButton = true;
}
public Buttons() {
super();
initializeInterface();
}
public Buttons(Image img) {
super(new ImageIcon(img));
initializeInterface();
}
private void initializeInterface() {
BorderFactory.createLineBorder(Color.black);
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent event) {
setBorder(BorderFactory.createLineBorder(Color.green));
}
@Override
public void mouseExited(MouseEvent event) {
setBorder(BorderFactory.createLineBorder(Color.black));
}
});
}
}

More Related Content

Similar to Whenever I run my application my Game appears with the pict.pdf

Create xo game in android studio
Create xo game in android studioCreate xo game in android studio
Create xo game in android studioMahmoodGhaemMaghami
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfrajkumarm401
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonEric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonMatthew McCullough
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfapexcomputer54
 
Creating a Facebook Clone - Part XXXVI.pdf
Creating a Facebook Clone - Part XXXVI.pdfCreating a Facebook Clone - Part XXXVI.pdf
Creating a Facebook Clone - Part XXXVI.pdfShaiAlmog1
 
Tic Tac Toe game with GUI please dont copy and paste from google. .pdf
Tic Tac Toe game with GUI please dont copy and paste from google. .pdfTic Tac Toe game with GUI please dont copy and paste from google. .pdf
Tic Tac Toe game with GUI please dont copy and paste from google. .pdfformaxekochi
 
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfCreating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfShaiAlmog1
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfvenkt12345
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfcronkwurphyb44502
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdffashionscollect
 
I need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfI need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfezhilvizhiyan
 
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdfTic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdfinfomalad
 
Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfShaiAlmog1
 
I am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdfI am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdfseamusschwaabl99557
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdffashionfolionr
 
correct the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfcorrect the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfdevangmittal4
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfeyeonsecuritysystems
 

Similar to Whenever I run my application my Game appears with the pict.pdf (20)

Create xo game in android studio
Create xo game in android studioCreate xo game in android studio
Create xo game in android studio
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
Creating a Facebook Clone - Part XXXVI.pdf
Creating a Facebook Clone - Part XXXVI.pdfCreating a Facebook Clone - Part XXXVI.pdf
Creating a Facebook Clone - Part XXXVI.pdf
 
Tic Tac Toe game with GUI please dont copy and paste from google. .pdf
Tic Tac Toe game with GUI please dont copy and paste from google. .pdfTic Tac Toe game with GUI please dont copy and paste from google. .pdf
Tic Tac Toe game with GUI please dont copy and paste from google. .pdf
 
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfCreating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdf
 
I need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfI need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdf
 
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdfTic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
 
ch20.pptx
ch20.pptxch20.pptx
ch20.pptx
 
Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdf
 
Java awt
Java awtJava awt
Java awt
 
I am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdfI am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdf
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
 
correct the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfcorrect the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdf
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdf
 

More from aarthitimesgd

Por qu es necesaria la alfabetizacin intercultural en el .pdf
Por qu es necesaria la alfabetizacin intercultural en el .pdfPor qu es necesaria la alfabetizacin intercultural en el .pdf
Por qu es necesaria la alfabetizacin intercultural en el .pdfaarthitimesgd
 
Minerals Table This is pretty straightforward Fill in the b.pdf
Minerals Table This is pretty straightforward Fill in the b.pdfMinerals Table This is pretty straightforward Fill in the b.pdf
Minerals Table This is pretty straightforward Fill in the b.pdfaarthitimesgd
 
In the past Netflixs superior library of titles allowed th.pdf
In the past Netflixs superior library of titles allowed th.pdfIn the past Netflixs superior library of titles allowed th.pdf
In the past Netflixs superior library of titles allowed th.pdfaarthitimesgd
 
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdf
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdfLea atentamente el siguiente pasaje antes de elegir sus resp.pdf
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdfaarthitimesgd
 
Jada levant la vista del monitor de su computadora para obs.pdf
Jada levant la vista del monitor de su computadora para obs.pdfJada levant la vista del monitor de su computadora para obs.pdf
Jada levant la vista del monitor de su computadora para obs.pdfaarthitimesgd
 
In C++ Plz LAB Artwork label classesconstructors Given m.pdf
In C++ Plz LAB Artwork label classesconstructors Given m.pdfIn C++ Plz LAB Artwork label classesconstructors Given m.pdf
In C++ Plz LAB Artwork label classesconstructors Given m.pdfaarthitimesgd
 
Heres a menu Appetizer Roll V Spring egg V Sou.pdf
Heres a menu Appetizer   Roll V   Spring egg V   Sou.pdfHeres a menu Appetizer   Roll V   Spring egg V   Sou.pdf
Heres a menu Appetizer Roll V Spring egg V Sou.pdfaarthitimesgd
 
I am interesting in finding the volume wetness of a 809 m2 .pdf
I am interesting in finding the volume wetness of a 809 m2 .pdfI am interesting in finding the volume wetness of a 809 m2 .pdf
I am interesting in finding the volume wetness of a 809 m2 .pdfaarthitimesgd
 
George se registra previamente para los nuevos lanzamientos .pdf
George se registra previamente para los nuevos lanzamientos .pdfGeorge se registra previamente para los nuevos lanzamientos .pdf
George se registra previamente para los nuevos lanzamientos .pdfaarthitimesgd
 
In 2022 Mason is working with his attorney and his CFP pro.pdf
In 2022 Mason is working with his attorney and his CFP pro.pdfIn 2022 Mason is working with his attorney and his CFP pro.pdf
In 2022 Mason is working with his attorney and his CFP pro.pdfaarthitimesgd
 
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdf
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdfHola comunidad de Chegg Necesito ayuda con esta tarea par.pdf
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdfaarthitimesgd
 
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdf
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdfExxonMobil needs to analyze its Cash Flow from 2020 through .pdf
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdfaarthitimesgd
 
Estudio de caso empresas conjuntas de General Electric His.pdf
Estudio de caso empresas conjuntas de General Electric  His.pdfEstudio de caso empresas conjuntas de General Electric  His.pdf
Estudio de caso empresas conjuntas de General Electric His.pdfaarthitimesgd
 
Hay 19 miembros en una junta directiva Si deben formar un s.pdf
Hay 19 miembros en una junta directiva Si deben formar un s.pdfHay 19 miembros en una junta directiva Si deben formar un s.pdf
Hay 19 miembros en una junta directiva Si deben formar un s.pdfaarthitimesgd
 
Cul de los siguientes NO se considera un cambio en la pol.pdf
Cul de los siguientes NO se considera un cambio en la pol.pdfCul de los siguientes NO se considera un cambio en la pol.pdf
Cul de los siguientes NO se considera un cambio en la pol.pdfaarthitimesgd
 
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdf
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdfEn 2005 Irlanda comenz a cobrar impuestos a los residente.pdf
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdfaarthitimesgd
 
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdf
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdfEn su captulo sobre Nadouek Labelle afirma que la sociedad.pdf
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdfaarthitimesgd
 
Course HCA621Nursing Home Administration Case Study The a.pdf
Course HCA621Nursing Home Administration Case Study The a.pdfCourse HCA621Nursing Home Administration Case Study The a.pdf
Course HCA621Nursing Home Administration Case Study The a.pdfaarthitimesgd
 
Discuss the following topic in the forum with your classmate.pdf
Discuss the following topic in the forum with your classmate.pdfDiscuss the following topic in the forum with your classmate.pdf
Discuss the following topic in the forum with your classmate.pdfaarthitimesgd
 
2 Kasm 2010da Griffey Companynin maliyeti 3000 olan mal.pdf
2 Kasm 2010da Griffey Companynin maliyeti 3000  olan mal.pdf2 Kasm 2010da Griffey Companynin maliyeti 3000  olan mal.pdf
2 Kasm 2010da Griffey Companynin maliyeti 3000 olan mal.pdfaarthitimesgd
 

More from aarthitimesgd (20)

Por qu es necesaria la alfabetizacin intercultural en el .pdf
Por qu es necesaria la alfabetizacin intercultural en el .pdfPor qu es necesaria la alfabetizacin intercultural en el .pdf
Por qu es necesaria la alfabetizacin intercultural en el .pdf
 
Minerals Table This is pretty straightforward Fill in the b.pdf
Minerals Table This is pretty straightforward Fill in the b.pdfMinerals Table This is pretty straightforward Fill in the b.pdf
Minerals Table This is pretty straightforward Fill in the b.pdf
 
In the past Netflixs superior library of titles allowed th.pdf
In the past Netflixs superior library of titles allowed th.pdfIn the past Netflixs superior library of titles allowed th.pdf
In the past Netflixs superior library of titles allowed th.pdf
 
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdf
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdfLea atentamente el siguiente pasaje antes de elegir sus resp.pdf
Lea atentamente el siguiente pasaje antes de elegir sus resp.pdf
 
Jada levant la vista del monitor de su computadora para obs.pdf
Jada levant la vista del monitor de su computadora para obs.pdfJada levant la vista del monitor de su computadora para obs.pdf
Jada levant la vista del monitor de su computadora para obs.pdf
 
In C++ Plz LAB Artwork label classesconstructors Given m.pdf
In C++ Plz LAB Artwork label classesconstructors Given m.pdfIn C++ Plz LAB Artwork label classesconstructors Given m.pdf
In C++ Plz LAB Artwork label classesconstructors Given m.pdf
 
Heres a menu Appetizer Roll V Spring egg V Sou.pdf
Heres a menu Appetizer   Roll V   Spring egg V   Sou.pdfHeres a menu Appetizer   Roll V   Spring egg V   Sou.pdf
Heres a menu Appetizer Roll V Spring egg V Sou.pdf
 
I am interesting in finding the volume wetness of a 809 m2 .pdf
I am interesting in finding the volume wetness of a 809 m2 .pdfI am interesting in finding the volume wetness of a 809 m2 .pdf
I am interesting in finding the volume wetness of a 809 m2 .pdf
 
George se registra previamente para los nuevos lanzamientos .pdf
George se registra previamente para los nuevos lanzamientos .pdfGeorge se registra previamente para los nuevos lanzamientos .pdf
George se registra previamente para los nuevos lanzamientos .pdf
 
In 2022 Mason is working with his attorney and his CFP pro.pdf
In 2022 Mason is working with his attorney and his CFP pro.pdfIn 2022 Mason is working with his attorney and his CFP pro.pdf
In 2022 Mason is working with his attorney and his CFP pro.pdf
 
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdf
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdfHola comunidad de Chegg Necesito ayuda con esta tarea par.pdf
Hola comunidad de Chegg Necesito ayuda con esta tarea par.pdf
 
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdf
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdfExxonMobil needs to analyze its Cash Flow from 2020 through .pdf
ExxonMobil needs to analyze its Cash Flow from 2020 through .pdf
 
Estudio de caso empresas conjuntas de General Electric His.pdf
Estudio de caso empresas conjuntas de General Electric  His.pdfEstudio de caso empresas conjuntas de General Electric  His.pdf
Estudio de caso empresas conjuntas de General Electric His.pdf
 
Hay 19 miembros en una junta directiva Si deben formar un s.pdf
Hay 19 miembros en una junta directiva Si deben formar un s.pdfHay 19 miembros en una junta directiva Si deben formar un s.pdf
Hay 19 miembros en una junta directiva Si deben formar un s.pdf
 
Cul de los siguientes NO se considera un cambio en la pol.pdf
Cul de los siguientes NO se considera un cambio en la pol.pdfCul de los siguientes NO se considera un cambio en la pol.pdf
Cul de los siguientes NO se considera un cambio en la pol.pdf
 
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdf
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdfEn 2005 Irlanda comenz a cobrar impuestos a los residente.pdf
En 2005 Irlanda comenz a cobrar impuestos a los residente.pdf
 
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdf
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdfEn su captulo sobre Nadouek Labelle afirma que la sociedad.pdf
En su captulo sobre Nadouek Labelle afirma que la sociedad.pdf
 
Course HCA621Nursing Home Administration Case Study The a.pdf
Course HCA621Nursing Home Administration Case Study The a.pdfCourse HCA621Nursing Home Administration Case Study The a.pdf
Course HCA621Nursing Home Administration Case Study The a.pdf
 
Discuss the following topic in the forum with your classmate.pdf
Discuss the following topic in the forum with your classmate.pdfDiscuss the following topic in the forum with your classmate.pdf
Discuss the following topic in the forum with your classmate.pdf
 
2 Kasm 2010da Griffey Companynin maliyeti 3000 olan mal.pdf
2 Kasm 2010da Griffey Companynin maliyeti 3000  olan mal.pdf2 Kasm 2010da Griffey Companynin maliyeti 3000  olan mal.pdf
2 Kasm 2010da Griffey Companynin maliyeti 3000 olan mal.pdf
 

Recently uploaded

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

Whenever I run my application my Game appears with the pict.pdf

  • 1. Whenever I run my application, my Game appears with the picture cut up and shuffled. Whenever I click a button to move the tiles, I get this error: Cannot invoke "javax.swing.JPanel.removeAll()" because "this.this$0.panel" is null How do I fix this? code starts here: package slidePuzzle; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.util.ArrayList; import java.util.Collections; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Game extends JFrame { // declare variables, arrays, and constants private Buttons emptyButton; private ArrayList<Buttons> buttons; private ArrayList<Point> answer; private final int TOTAL_BUTTONS = 12; private final int TOTAL_WIDTH = 400; private JPanel panel; private Image img; private BufferedImage src; private BufferedImage mod; private int width; private int height; // default constructor public Game() { // call initializer initializeInterface(); } private void initializeInterface() { answer = new ArrayList<Point>(); buttons = new ArrayList<Buttons>(); JPanel panel = new JPanel();
  • 2. panel.setBorder(BorderFactory.createLineBorder(Color.black)); panel.setLayout(new GridLayout(4, 3, 0, 0)); for (int x = 0; x < 4; x++) { for (int y = 0; y < 3; y++) { answer.add(new Point(x, y)); } } try { src = loadImg(); int height = modifyHeight(src.getWidth(), src.getHeight()); mod = resizeImg(src, TOTAL_WIDTH, height, BufferedImage.TYPE_INT_ARGB); } catch (IOException exception) { JOptionPane.showMessageDialog(this, "Image is unable to be rendered.", "Error", JOptionPane.ERROR_MESSAGE); } height = mod.getHeight(null); width = mod.getWidth(null); for (int x = 0; x < 4; x++) { for (int y = 0; y <3; y++) { img = createImage(new FilteredImageSource(mod.getSource(), new CropImageFilter(x * width / 3, y * height / 4, (width / 3), height / 4))); Buttons button = new Buttons(img); button.putClientProperty("position", new Point(x, y)); if (x == 3 && y == 2) { emptyButton = new Buttons(); emptyButton.setBorderPainted(false); emptyButton.setContentAreaFilled(false); emptyButton.setEmptyButton(); emptyButton.putClientProperty("position", new Point(x, y)); } else { buttons.add(button); } } }
  • 3. Collections.shuffle(buttons); buttons.add(emptyButton); for (int z = 0; z < TOTAL_BUTTONS; z++) { Buttons button = buttons.get(z); panel.add(button); button.setBorder(BorderFactory.createLineBorder(Color.black)); button.addActionListener(new Click()); } JFrame frame = new JFrame("Tile Slider"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Game()); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setResizable(false); } private BufferedImage loadImg() throws IOException { BufferedImage bufferedImage = ImageIO.read(new File("src/pond.jpg")); return bufferedImage; } private BufferedImage resizeImg(BufferedImage natural, int width, int height, int type) { BufferedImage adjustedImg = new BufferedImage(width, height, type); Graphics2D graphics = adjustedImg.createGraphics(); // draw image to fit inside our window and return graphics.drawImage(natural, 0, 0, width, height, null); graphics.dispose(); return adjustedImg; } private int modifyHeight(int width, int height) { double widthRatio = TOTAL_WIDTH / (double) width; int modHeight = (int) (height * widthRatio); return modHeight; } private class Click extends AbstractAction { @Override
  • 4. public void actionPerformed(ActionEvent event) { check(event); isCorrectConfiguration(); } private void check(ActionEvent event) { int emptyButtonIndex = 0; for (Buttons btn: buttons) { if (btn.getEmptyButton()) { emptyButtonIndex = buttons.indexOf(btn); } } JButton btn = (JButton) event.getSource(); int buttonIndex = buttons.indexOf(btn); if ((buttonIndex + 1 == emptyButtonIndex) || (buttonIndex + 3 == emptyButtonIndex) || (buttonIndex - 1 == emptyButtonIndex) || (buttonIndex - 3 == emptyButtonIndex)) { Collections.swap(buttons, emptyButtonIndex, buttonIndex); update(); } } private void update() { panel.removeAll(); for (JComponent btn: buttons) { panel.add(btn); } panel.validate(); } } public void isCorrectConfiguration() { ArrayList<Point> userState = new ArrayList<Point>(); for (JComponent btn: buttons) { userState.add((Point) btn.getClientProperty("position")); }
  • 5. if (comparePuzzleState(answer, userState)) { JOptionPane.showMessageDialog(panel, "You have completed the puzzle!"); } } public static boolean comparePuzzleState(ArrayList<Point> list1, ArrayList<Point> list2) { return list1.toString().contentEquals(list2.toString()); } public static void main(String[] args) { EventQueue.invokeLater(() -> { Game slidePuzzle = new Game(); slidePuzzle.setVisible(true); }); } } public class Buttons extends JButton { private boolean isEmptyButton = false; public boolean getEmptyButton() { return isEmptyButton; } public void setEmptyButton() { isEmptyButton = true; } public Buttons() { super(); initializeInterface(); } public Buttons(Image img) { super(new ImageIcon(img)); initializeInterface(); } private void initializeInterface() { BorderFactory.createLineBorder(Color.black); addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent event) { setBorder(BorderFactory.createLineBorder(Color.green)); } @Override public void mouseExited(MouseEvent event) {