Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here are the requirements of this assignment: Develop a class, called JEightPuzz

ID: 673293 • Letter: H

Question

Here are the requirements of this assignment:

Develop a class, called JEightPuzzleFrame, which extends JFrame. Its constructor has

two parameters: 1) title of window, 2) the path to an image. (Your program does not

need to verify the image is really square.)

The size of the window is decided by the image size. You should at least test your

program with two images with different sizes. When I test your program, I may use

different images. So your program should not rely on a certain image.

Each button is loaded with a part of the image. When the puzzle is solved, these parts

should make up the original image, missing the right-bottom corner.

When I test your program using the given “fgcu_logo.png”, your initial setting of the

game must be the same as shown in the left figure above. (This is easy for you and me

to test the program when the puzzle is solved.)

Please ensure that your program runs fine as long as the image file “fgcu_logo.png” is in

the same folder where the .class files are stored.

When a game is solved, pop up a window to congratulate the user. That pop-up window

will not go away until the user click “ok”.

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class JEightPuzzleFrame extends JFrame implements ActionListener
{
private int width;
private int height;   
private int position[][];
private int count[] = {1,2,3,4,5,6,7,8,9};   
private Image image1;
private JPanel centerPanel;
private JButton[][] listOfBtn = new JButton[3][3];
private String Source;
private String Title;
private int ShuffleCount;
public JEightPuzzleFrame(String Title, String Source)
{
this.Source = Source;
this.Title = Title;
ShuffleCount = 0;
if (ShuffleCount == 0)
{
ShuffleCount++;
set();
}
else
{
shuffle();
}
}
private Image getIcon(int i, int j)
{
BufferedImage image=null;
try
{
image = ImageIO.read(new File(Source));
}
catch(IOException e)
{
System.err.println("Image not found");
System.exit(1);
}   
width = image.getWidth();
height = image.getHeight();
Image source = (Image) image;
image1 = createImage(new FilteredImageSource(source.getSource(),new CropImageFilter(j*width/3, i*height/3, (width/3)+1, height/3)));
return image1;
}
private void set()
{
position = new int[][] {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 3, 0, 0));
add(centerPanel, BorderLayout.CENTER);
BufferedImage image=null;
try
{
image = ImageIO.read(new File(Source));
}
catch(IOException e)
{
System.err.println("Image not found");
System.exit(1);
}   
width = image.getWidth();
height = image.getHeight();
/* for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 3; j++)
{
if ( i == 2 && j == 1)
{
listOfBtn[2][1] = new JButton();
centerPanel.add(listOfBtn[2][1]);
}
else
{
listOfBtn[i][j] = new JButton();
listOfBtn[i][j].addActionListener(this);
centerPanel.add(listOfBtn[i][j]);
Image source = (Image) image;
image1 = createImage(new FilteredImageSource(source.getSource(),new CropImageFilter(j*width/3, i*height/3, (width/3)+1, height/3)));
listOfBtn[i][j].setIcon(new ImageIcon(image1));
listOfBtn[i][j].setVisible(true);
}
}
} */
listOfBtn[0][0] = new JButton();
centerPanel.add(listOfBtn[0][0]);
count [0] = 9;
listOfBtn[0][1] = new JButton();
listOfBtn[0][1].addActionListener(this);
centerPanel.add(listOfBtn[0][1]);
listOfBtn[0][1].setIcon(new ImageIcon(getIcon(0,0)));
listOfBtn[0][1].setVisible(true);
count[1] = 1;
listOfBtn[0][2] = new JButton();
listOfBtn[0][2].addActionListener(this);
centerPanel.add(listOfBtn[0][2]);
listOfBtn[0][2].setIcon(new ImageIcon(getIcon(0,1)));
listOfBtn[0][2].setVisible(true);
count[2] = 2;
listOfBtn[1][0] = new JButton();
listOfBtn[1][0].addActionListener(this);
centerPanel.add(listOfBtn[1][0]);
listOfBtn[1][0].setIcon(new ImageIcon(getIcon(1,1)));
listOfBtn[1][0].setVisible(true);
count[3] = 5;
listOfBtn[1][1] = new JButton();
listOfBtn[1][1].addActionListener(this);
centerPanel.add(listOfBtn[1][1]);
listOfBtn[1][1].setIcon(new ImageIcon(getIcon(1,2)));
listOfBtn[1][1].setVisible(true);
count[4] = 6;
listOfBtn[1][2] = new JButton();
listOfBtn[1][2].addActionListener(this);
centerPanel.add(listOfBtn[1][2]);
listOfBtn[1][2].setIcon(new ImageIcon(getIcon(0,2)));
listOfBtn[1][2].setVisible(true);
count[5] = 3;
listOfBtn[2][0] = new JButton();
listOfBtn[2][0].addActionListener(this);
centerPanel.add(listOfBtn[2][0]);
listOfBtn[2][0].setIcon(new ImageIcon(getIcon(1,0)));
listOfBtn[2][0].setVisible(true);
count[6] = 4;
listOfBtn[2][1] = new JButton();
listOfBtn[2][1].addActionListener(this);
centerPanel.add(listOfBtn[2][1]);
listOfBtn[2][1].setIcon(new ImageIcon(getIcon(2,0)));
listOfBtn[2][1].setVisible(true);
count[7] = 7;
listOfBtn[2][2] = new JButton();
listOfBtn[2][2].addActionListener(this);
centerPanel.add(listOfBtn[2][2]);
listOfBtn[2][2].setIcon(new ImageIcon(getIcon(2,1)));
listOfBtn[2][2].setVisible(true);
count[8] = 8;
validate();
setSize(width, height);
setTitle(Title);
setResizable(false);
setLocationRelativeTo(null); // center the image
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void shuffle()
{
set();
}
public static void main(String[] args)
{
new JEightPuzzleFrame("Puzzle", "image.png");   
System.out.println();
}
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton) e.getSource();
Dimension size = button.getSize();
int emptyX = listOfBtn[0][0].getX();
int emptyY = listOfBtn[0][0].getY();
int buttonX = button.getX();
int buttonY = button.getY();
int buttonPosX = buttonX / size.width;
int buttonPosY = buttonY / size.height;
int buttonIndex = position[buttonPosY][buttonPosX];
if (emptyX == buttonX && (emptyY - buttonY) == size.height )
{
int labelIndex = buttonIndex + 3;
centerPanel.remove(buttonIndex);
centerPanel.add(listOfBtn[0][0], buttonIndex);
centerPanel.add(button,labelIndex);
centerPanel.validate();
int a = count[buttonIndex];
count[buttonIndex] = count[labelIndex];
count[labelIndex] = a;
}
if (emptyX == buttonX && (emptyY - buttonY) == -size.height )
{
int labelIndex = buttonIndex - 3;
centerPanel.remove(labelIndex);
centerPanel.add(button,labelIndex);
centerPanel.add(listOfBtn[0][0], buttonIndex);
centerPanel.validate();
int a = count[buttonIndex];
count[buttonIndex] = count[labelIndex];
count[labelIndex] = a;
}
if (emptyY == buttonY && (emptyX - buttonX) == size.width )
{
int labelIndex = buttonIndex + 1;
centerPanel.remove(buttonIndex);
centerPanel.add(listOfBtn[0][0], buttonIndex);
centerPanel.add(button,labelIndex);   
centerPanel.validate();
int a = count[buttonIndex];
count[buttonIndex] = count[labelIndex];
count[labelIndex] = a;
}
if (emptyY == buttonY && (emptyX - buttonX) == -size.width )
{
int labelIndex = buttonIndex - 1;
centerPanel.remove(buttonIndex);
centerPanel.add(listOfBtn[0][0], labelIndex);
centerPanel.add(button,labelIndex);
centerPanel.validate();
int a = count[buttonIndex];
count[buttonIndex] = count[labelIndex];
count[labelIndex] = a;
}
if(count[0] == 1 &&count[1] == 2 &&count[2] == 3 &&count[3] == 4 &&count[4] == 5 &&count[5] == 6 &&count[6] == 7 &&count[7] == 8 &&count[8] == 9)
{
JOptionPane.showMessageDialog(centerPanel,"Good job! Hit ok to scramble");
shuffle();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote