[Pemrograman]Membuat game catur sederhana dengan java



Hello world,lama sudah tidak posting lagi di newbieilmu karena kesibukan kuliah ditambah kerjaan yang menumpuk nah untuk itu kali ini kita akan belajar membuat game catur sederhana di java..
wah bagaimana sudah tidak sabar lagi kan?silakan download code nya di github
https://gist.github.com/1771476

package DemoChees;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import com.jtattoo.plaf.mcwin.McWinLookAndFeel;
 /*
  * @Anugrah Bagus Susilo
 */
public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public ChessGameDemo(){
        Dimension boardSize = new Dimension(600, 600);
        setTitle("Newbie Chees");
        setIconImage(getToolkit().getImage("src/DemoChartBar/images/chessboard.jpeg"));

        //  Gunakan Layered Pane untuk Aplikasi ini

        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);
        layeredPane.addMouseListener(this);
        layeredPane.addMouseMotionListener(this);

        // Tambahkan papan catur ke layer pane 

         chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.black : Color.white );
            else
                square.setBackground( i % 2 == 0 ? Color.white : Color.black );
        }

        // Tambahkan gambar pion ke board

        JLabel piece = new JLabel( new ImageIcon("src/DemoChartBar/images/bentengputih.jpeg") );
        JPanel panel = (JPanel)chessBoard.getComponent(0);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/kuda.jpeg"));
        panel = (JPanel)chessBoard.getComponent(1);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/assraja.jpeg"));
        panel = (JPanel)chessBoard.getComponent(2);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/raja.jpeg"));
        panel = (JPanel)chessBoard.getComponent(3);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/raja2.jpeg"));
        panel = (JPanel)chessBoard.getComponent(4);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/assraja.jpeg"));
        panel = (JPanel)chessBoard.getComponent(5);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/kuda.jpeg"));
        panel = (JPanel)chessBoard.getComponent(6);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/bentengputih.jpeg"));
        panel = (JPanel)chessBoard.getComponent(7);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(8);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(9);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(10);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(11);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(12);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(13);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(14);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pion.jpeg"));
        panel = (JPanel)chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(48);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(49);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(50);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(51);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(52);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(53);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(54);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/pionhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(55);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/benteng.jpeg"));
        panel = (JPanel)chessBoard.getComponent(56);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/kudahitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(57);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/sterhitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(59);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/kudahitam.jpeg"));
        panel = (JPanel)chessBoard.getComponent(62);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/DemoChartBar/images/benteng.jpeg"));
        panel = (JPanel)chessBoard.getComponent(63);
        panel.add(piece);

    }

    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel)
	return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }

    //gerakan pion ke sekitar 

    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
     }

  //Letakkan pion ke papan catur

    public void mouseReleased(MouseEvent e) {
        if(chessPiece == null) return;

        chessPiece.setVisible(false);
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JLabel){
            Container parent = c.getParent();
            parent.remove(0);
            parent.add( chessPiece );
        }
        else {
            Container parent = (Container)c;
            parent.add( chessPiece );
        }

        chessPiece.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
    }
    public void mouseMoved(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e){
    }
    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) throws UnsupportedLookAndFeelException {
    	UIManager.setLookAndFeel(new McWinLookAndFeel());
        JFrame frame = new ChessGameDemo();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}

ini sample screenshootnya yang nampak di dekstop saya

notes :
game ini masih banyak sekali kekurangan karena masih versi alpha🙂 di versi alpha ini lebih mengedepankan aspek interface nya,mungkin di versi yang akan datang akan ada penambahan fitur yang lebih kompleks
dan mungkin bagi yang ingin ikut mengembangkan silakan hubungi saya lewat email newbie.ilmu@yahoo.com

Happy coding^^

Anugrah Bagus Susilo

9 pemikiran pada “[Pemrograman]Membuat game catur sederhana dengan java

  1. kok ga bisa ya..kok eror di UIManager.setLookAndFeel(new McWinLookAndFeel());

  2. Terima kasih sharing ilmunya yaa

  3. gan bisa buat game catur jawa ga?? yg papan permainannya mirip bendera inggris ada 6 objek yg digeser-geser,,yg satu baris dy yg menang😀

    • klo di tempatku namanya dam-dam an gan :p ,blum pernah nyoba gan tapi sebenernya prinsipnya sama saja haha

      • wew,,ternyata namanya ga sama yah??😀
        sama?? catur banyak objek dan banyak kolom,,,maennya,,yaa,,kaya catur,,kalo catur jawa/dam-dam an,,,2 player ada 6 objek dengan 9 titik,,mirip tictactoe,bedanya tictactoe ga bisa digeser-geser trus objeknya lebih dari 6 kalo blum menang,,kalo permainan draw,,yaa,,9 objek hmm😀
        ayo gan coba buat,,kalo konsepnya kayanya antara tictactoe dan catur yah,,,kalo tictactoe sih aq ada,,mau project nya gan buat nyoba2 edit jd dam-dam an???😀
        aq dikasi temen,,tapi ga bisa edit program2 hehehe,,sekedar seneng maen game😀

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s