You are on page 1of 6

NAMA : ANAS TAHER

NIM : 18112444
MATA KULIAH : Game Developer
Tugas : Puzzle Gambar Dengan Java Netbeans

Membuat Game Puzzle Berbasis Java Dengan Ordo 4x4

-------Code------

SlidePuzzle.java

package slidepuzzle;
import javax.swing.JFrame;

public class SlidePuzzle {

public static void main(String[] args) {


JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new SlidePuzzleGUI());
window.pack(); // finalize layout
window.show(); // make window visible
window.setResizable(false);
}

SlidePuzzleGUI.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package slidepuzzle;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SlidePuzzleGUI extends javax.swing.JPanel {

private GraphicsPanel _puzzleGraphics;


private SlideModelPuzzle _puzzleModel = new SlideModelPuzzle();
/**
* Creates new form SlidePuzzleGUI
*/
public SlidePuzzleGUI() {
initComponents();
JButton newGameButton = new JButton("New Game");
newGameButton.addActionListener(new NewGameAction());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
_puzzleGraphics = new GraphicsPanel();
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}

class GraphicsPanel extends JPanel implements MouseListener {


private static final int ROWS = 4; //edit row menjadi 4
private static final int COLS = 4; //edit column menjadi 4 , untuk ordo 4x4
private static final int CELL_SIZE = 80; // Pixels
private Font _biggerFont;
public GraphicsPanel() {
_biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.black);
this.addMouseListener(this);
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);

g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);


g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}

public void mousePressed(MouseEvent e) {

int col = e.getX()/CELL_SIZE;


int row = e.getY()/CELL_SIZE;

if (!_puzzleModel.moveTile(row, col)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}

public void mouseClicked (MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}//end class GraphicsPanel
public class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}//end inner class NewGameAction

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);


this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}

SlideModelPuzzle.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package slidepuzzle;

public class SlideModelPuzzle {


private static final int ROWS = 4; //edit row menjadi 4
private static final int COLS = 4; //edit column menjadi 4 , untuk ordo 4x4
private Tile[][] _contents; // All tiles.
private Tile _emptyTile; // The empty space.
public SlideModelPuzzle() {
_contents = new Tile[ROWS][COLS];
reset(); // Initialize and shuffle tiles.
}//end constructor
String getFace(int row, int col) {
return _contents[row][col].getFace();
}//end getFace
public void reset() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
_contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1));
}
}
//--- Set last tile face to null to mark empty space
_emptyTile = _contents[ROWS-1][COLS-1];
_emptyTile.setFace(null);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
exchangeTiles(r, c, (int)(Math.random()*ROWS)
, (int)(Math.random()*COLS));
}
}
}//end reset

public boolean moveTile(int r, int c) {


return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}//end moveTile
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
//--- Check to see if this neighbor is on board and is empty.
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}//end checkEmpty
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<ROWS && c>=0 && c<COLS;
}//end isLegalRowCol
private void exchangeTiles(int r1, int c1, int r2, int c2) {
Tile temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}//end exchangeTiles
public boolean isGameOver() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
Tile trc = _contents[r][c];
return trc.isInFinalPosition(r, c);
}
}
return true;
}//end isGameOver
}//end class SlidePuzzleModel
class Tile {
private int _row; // row of final position
private int _col; // col of final position
private String _face; // string to display
//end instance variables
public Tile(int row, int col, String face) {
_row = row;
_col = col;

_face = face;
}//end constructor
public void setFace(String newFace) {
_face = newFace;
}//end getFace
public String getFace() {
return _face;
}
public boolean isInFinalPosition(int r, int c) {
return r==_row && c==_col;
}
}

------- Hasil Jadi ------------

You might also like