Any help is appreciated thanks a lot . At my wits end =/
public class GameFrame extends JFrame {
private JButton[] button = new JButton[9];
private JPanel puzpiece;
private JFrame frame;
public GameFrame() {
try {
initialize();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialize() throws IOException {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Puzzle Game");
frame.setVisible(true);
// run setImage()
setImage();
// creates a new panel for the splitted puzzle pieces
puzpiece = new JPanel();
// set layout of puzpiece panel
puzpiece.setLayout(new GridLayout(3,3));
// set size of puzpiece panel
puzpiece.setPreferredSize(new Dimension(500,200));
// adds 9 buttons to puzpiece panel
for(int a=0; a<9; a++){
button[a] = new JButton();
puzpiece.add(button[a]);
}
// add puzpiece panel to JFrame
frame.add(puzpiece,BorderLayout.WEST);
// set frame size to follow the maximum sizes of all contained components
frame.pack();
frame.setLocationRelativeTo(null);
}
public void setImage() throws IOException{
URL img= GameFrame.class.getResource("image/Penguins3.jpg");
BufferedImage bi=ImageIO.read(img);
int w=bi.getWidth();
int h=bi.getHeight();
int count=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
BufferedImage wi=bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
Image sc=bi.getScaledInstance(puzpiece.getWidth()/3,
puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
setupImage(count++,sc);
}
}
}
private void setupImage(int a,Image wi) {
button[a]=new JButton(new ImageIcon(wi));
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GameFrame gf = new GameFrame();
}
});
}
}