Artikel kali ini tentang pembuatan program atau game menggunakan java. Program ini menggunakan aplikasi JCreator. Anda Bisa Download Disini file lengkapnya.
Berikut Listing Programnya, simpan dengan extensi Gamepuzzle.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import javax.swing.*;
class Gamepuzzle extends JFrame implements ActionListener{
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample;
JButton rb;
JButton clickMe;
JLabel lcc;
JTextField cc;
JLabel cg;
Icon star;
int ccnt;
Icon ic0=new ImageIcon("Sample.jpg");
Icon ic1=new ImageIcon("1.jpg");
Icon ic2=new ImageIcon("2.jpg");
Icon ic3=new ImageIcon("3.jpg");
Icon ic4=new ImageIcon("4.jpg");
Icon ic5=new ImageIcon("5.jpg");
Icon ic6=new ImageIcon("6.jpg");
Icon ic7=new ImageIcon("7.jpg");
Icon ic8=new ImageIcon("8.jpg");
Icon ic9=new ImageIcon("*.jpg");
Gamepuzzle(){
JFrame f = new JFrame("Game Puzzle by Siman Mendrofa");
ImageIcon gambar = new ImageIcon("Taeyeon.gif");
f.setIconImage(gambar.getImage());
sample=new JButton(ic0);
sample.setBounds(500,30,300, 300);
b1=new JButton(ic1);
b1.setBounds(10,10,100, 100);
b2=new JButton(ic2);
b2.setBounds(110,10,100, 100);
b3=new JButton(ic3);
b3.setBounds(210,10,100, 100);
b4=new JButton(ic4);
b4.setBounds(10,110,100, 100);
b5=new JButton(ic5);
b5.setBounds(110,110,100, 100);
b6=new JButton(ic6);
b6.setBounds(210,110,100, 100);
b7=new JButton(ic7);
b7.setBounds(10,210,100, 100);
b8=new JButton(ic8);
b8.setBounds(110,210,100, 100);
b9=new JButton(ic9);
b9.setBounds(210,210,100, 100);
Random r = new Random();
rb=new JButton("Random");
clickMe = new JButton("Click Me");
cc = new JTextField("0");
//cc.enable(false);
lcc = new JLabel("Click Count: ");
cg = new JLabel("Gambar Asli");
rb.setBounds(310,410,100, 40);
cc.setBounds(210,420,100, 30);
lcc.setBounds(140,420,100, 40);
cg.setBounds(500,300,100, 100);
clickMe.setBounds(10,500,100,40);
star=b9.getIcon();
f.add(sample);f.add(cg);
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(b9);
f.add(rb);f.add(clickMe);f.add(cc);f.add(lcc);
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this);
b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this);
b9.addActionListener(this);
cc.addActionListener(this); rb.addActionListener(this); clickMe.addActionListener(this);
f.setLayout(null);
f.setSize(900,600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == clickMe) {
JOptionPane.showMessageDialog(null,"Program Game Java Sederhana : \n\n" +
"Game Puzzle\n" +
"Notes : " + " Program ini masih jauh dari sempurna,Terima Kasih."
);
}
if(e.getSource()==b1){
Icon s1=b1.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b1.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b4.getIcon()==star){
b4.setIcon(s1);
b1.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b2){
Icon s1=b2.getIcon();
ccnt++;
if(b1.getIcon()==star){
b1.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b3.getIcon()==star){
b3.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b3){
Icon s1=b3.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b3.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b6.getIcon()==star){
b6.setIcon(s1);
b3.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b4){
Icon s1=b4.getIcon();
ccnt++;
if(b1.getIcon()==star){
b1.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b7.getIcon()==star){
b7.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b5){
Icon s1=b5.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b6.getIcon()==star){
b6.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b4.getIcon()==star){
b4.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b8.getIcon()==star){
b8.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b6){
Icon s1=b6.getIcon();
ccnt++;
if(b3.getIcon()==star){
b3.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b9.getIcon()==star){
b9.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b7){
Icon s1=b7.getIcon();
ccnt++;
if(b4.getIcon()==star){
b4.setIcon(s1);
b7.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b8.getIcon()==star){
b8.setIcon(s1);
b7.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b8){
Icon s1=b8.getIcon();
ccnt++;
if(b5.getIcon()==star){
b5.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b7.getIcon()==star){
b7.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b9.getIcon()==star){
b9.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b9){
Icon s1=b9.getIcon();
ccnt++;
if(b6.getIcon()==star){
b6.setIcon(s1);
b9.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b8.getIcon()==star){
b8.setIcon(s1);
b9.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if((b1.getIcon()==ic1 && b2.getIcon()==ic2)&&
(b3.getIcon()==ic3 && b4.getIcon()==ic4)&&
(b5.getIcon()==ic5 && b6.getIcon()==ic6)&&
(b7.getIcon()==ic7 && b8.getIcon()==ic8)&&
(b9.getIcon()==ic9 )){
JOptionPane.showMessageDialog(null,"Selamat, Anda Menang dengan " + (ccnt) + " langkah.");
}
if (e.getSource() == rb) {
Icon s1=b9.getIcon();
if(b9.getIcon()==star){
b1.setIcon(ic2);
b2.setIcon(ic1);
b3.setIcon(ic4);
b4.setIcon(ic3);
b5.setIcon(ic8);
b6.setIcon(ic5);
b7.setIcon(ic7);
b8.setIcon(ic6);
b9.setIcon(ic9);
ccnt=0;
cc.setText("0");
ccnt=0;
}else if(b9.getIcon()==star){
}
}
}
public static void main(String args[]){
Gamepuzzle test = new Gamepuzzle();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Berikut Hasil Outputnya:
Berikut Listing Programnya, simpan dengan extensi Gamepuzzle.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import javax.swing.*;
class Gamepuzzle extends JFrame implements ActionListener{
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample;
JButton rb;
JButton clickMe;
JLabel lcc;
JTextField cc;
JLabel cg;
Icon star;
int ccnt;
Icon ic0=new ImageIcon("Sample.jpg");
Icon ic1=new ImageIcon("1.jpg");
Icon ic2=new ImageIcon("2.jpg");
Icon ic3=new ImageIcon("3.jpg");
Icon ic4=new ImageIcon("4.jpg");
Icon ic5=new ImageIcon("5.jpg");
Icon ic6=new ImageIcon("6.jpg");
Icon ic7=new ImageIcon("7.jpg");
Icon ic8=new ImageIcon("8.jpg");
Icon ic9=new ImageIcon("*.jpg");
Gamepuzzle(){
JFrame f = new JFrame("Game Puzzle by Siman Mendrofa");
ImageIcon gambar = new ImageIcon("Taeyeon.gif");
f.setIconImage(gambar.getImage());
sample=new JButton(ic0);
sample.setBounds(500,30,300, 300);
b1=new JButton(ic1);
b1.setBounds(10,10,100, 100);
b2=new JButton(ic2);
b2.setBounds(110,10,100, 100);
b3=new JButton(ic3);
b3.setBounds(210,10,100, 100);
b4=new JButton(ic4);
b4.setBounds(10,110,100, 100);
b5=new JButton(ic5);
b5.setBounds(110,110,100, 100);
b6=new JButton(ic6);
b6.setBounds(210,110,100, 100);
b7=new JButton(ic7);
b7.setBounds(10,210,100, 100);
b8=new JButton(ic8);
b8.setBounds(110,210,100, 100);
b9=new JButton(ic9);
b9.setBounds(210,210,100, 100);
Random r = new Random();
rb=new JButton("Random");
clickMe = new JButton("Click Me");
cc = new JTextField("0");
//cc.enable(false);
lcc = new JLabel("Click Count: ");
cg = new JLabel("Gambar Asli");
rb.setBounds(310,410,100, 40);
cc.setBounds(210,420,100, 30);
lcc.setBounds(140,420,100, 40);
cg.setBounds(500,300,100, 100);
clickMe.setBounds(10,500,100,40);
star=b9.getIcon();
f.add(sample);f.add(cg);
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(b9);
f.add(rb);f.add(clickMe);f.add(cc);f.add(lcc);
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this);
b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this);
b9.addActionListener(this);
cc.addActionListener(this); rb.addActionListener(this); clickMe.addActionListener(this);
f.setLayout(null);
f.setSize(900,600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == clickMe) {
JOptionPane.showMessageDialog(null,"Program Game Java Sederhana : \n\n" +
"Game Puzzle\n" +
"Notes : " + " Program ini masih jauh dari sempurna,Terima Kasih."
);
}
if(e.getSource()==b1){
Icon s1=b1.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b1.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b4.getIcon()==star){
b4.setIcon(s1);
b1.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b2){
Icon s1=b2.getIcon();
ccnt++;
if(b1.getIcon()==star){
b1.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b3.getIcon()==star){
b3.setIcon(s1);
b2.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b3){
Icon s1=b3.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b3.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b6.getIcon()==star){
b6.setIcon(s1);
b3.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b4){
Icon s1=b4.getIcon();
ccnt++;
if(b1.getIcon()==star){
b1.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b7.getIcon()==star){
b7.setIcon(s1);
b4.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b5){
Icon s1=b5.getIcon();
ccnt++;
if(b2.getIcon()==star){
b2.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b6.getIcon()==star){
b6.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b4.getIcon()==star){
b4.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b8.getIcon()==star){
b8.setIcon(s1);
b5.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b6){
Icon s1=b6.getIcon();
ccnt++;
if(b3.getIcon()==star){
b3.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b5.getIcon()==star){
b5.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b9.getIcon()==star){
b9.setIcon(s1);
b6.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b7){
Icon s1=b7.getIcon();
ccnt++;
if(b4.getIcon()==star){
b4.setIcon(s1);
b7.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b8.getIcon()==star){
b8.setIcon(s1);
b7.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b8){
Icon s1=b8.getIcon();
ccnt++;
if(b5.getIcon()==star){
b5.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b7.getIcon()==star){
b7.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
else if(b9.getIcon()==star){
b9.setIcon(s1);
b8.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if(e.getSource()==b9){
Icon s1=b9.getIcon();
ccnt++;
if(b6.getIcon()==star){
b6.setIcon(s1);
b9.setIcon(star);
cc.setText(Integer.toString(ccnt));
} else if(b8.getIcon()==star){
b8.setIcon(s1);
b9.setIcon(star);
cc.setText(Integer.toString(ccnt));
}
}
if((b1.getIcon()==ic1 && b2.getIcon()==ic2)&&
(b3.getIcon()==ic3 && b4.getIcon()==ic4)&&
(b5.getIcon()==ic5 && b6.getIcon()==ic6)&&
(b7.getIcon()==ic7 && b8.getIcon()==ic8)&&
(b9.getIcon()==ic9 )){
JOptionPane.showMessageDialog(null,"Selamat, Anda Menang dengan " + (ccnt) + " langkah.");
}
if (e.getSource() == rb) {
Icon s1=b9.getIcon();
if(b9.getIcon()==star){
b1.setIcon(ic2);
b2.setIcon(ic1);
b3.setIcon(ic4);
b4.setIcon(ic3);
b5.setIcon(ic8);
b6.setIcon(ic5);
b7.setIcon(ic7);
b8.setIcon(ic6);
b9.setIcon(ic9);
ccnt=0;
cc.setText("0");
ccnt=0;
}else if(b9.getIcon()==star){
}
}
}
public static void main(String args[]){
Gamepuzzle test = new Gamepuzzle();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Berikut Hasil Outputnya:
Artikel Lain:
- Cari Duit Sampingan Daftar Dapat $10 Modal Atau Check In
- Bisa Juga Jadi Agen Tiket Pesawat, Counter Pulsa, DLL
- Mau Liburan Ke Korea, Inilah Tempatnya
- Ada Juga Program Java yang mungkin berkaitan
- I Believe You, Tempatnya Link Download