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