Membuat Game Acak Gambar Dengan Java
Game acak gambar merupakan game yang mencocokkan gambar setelah kumpulan dari gambar - gambar tersebut diacak telebih dahulu setelah itu kita akan mencocokkan gambar - gambar yang sama.
kurang lebih tampilannya akan seperti ini.
setelah itu klik tombol acak gambar..
maka gambar akan teracak,gambar berkepala orang itu hanya sebagai penutup gambar yang diacak.
sebelum membuat game ini kita telebih dahulu kita harus mempunyai gambar sebanyak 13 gambar..
dimana 12 gambar yang akan diacak dan satu gambar yang akan menutupi gambar yang diacak tersebut..
misalnya gambar seperti ini:
setelah itu kita akan membuat source codenya.
kurang lebih seperti ini..
sebelumnnya nama kelas java ini ada lah CariGambar.java
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class CariGambar extends javax.swing.JFrame implements ActionListener
{
private javax.swing.JPanel cardPane;
private javax.swing.JButton exitButton;
private javax.swing.JButton replayButton;
private javax.swing.JLabel numTriesLabel;
int[] tilemap = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6,6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12};
JToggleButton[] cards = new JToggleButton[24];
Object card1Object = null;
Object card2Object = null;
String card1Image = "";
String card2Image = "";
int card1Num = 0;
int card2Num = 0;
int cardsSelected = 0;
int cardsLeft = 24;
int numTries = 0;
public CariGambar()
{
initComponents();
shuffleCards();
pack();
}
private void initComponents()
{
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// setSize(800,600);
setTitle("Cari Gambar");
JPanel buttonPane = new JPanel();
JPanel cardPane = new JPanel();
buttonPane.setLayout(new GridBagLayout());
GridBagConstraints b = new GridBagConstraints();
b.fill = GridBagConstraints.HORIZONTAL;
cardPane.setLayout(new GridLayout(6,6));
numTriesLabel = new JLabel();
b.gridx = 0;
b.gridy = 1;
buttonPane.add(numTriesLabel,b);
replayButton = new JButton();
b.gridx = 0;
b.gridy = 0;
buttonPane.add(replayButton,b);
exitButton = new JButton();
b.gridx = 1;
b.gridy = 0;
buttonPane.add(exitButton,b);
numTriesLabel.setText("Total Coba: "+ numTries +" x");
replayButton.setMnemonic('S');
replayButton.setText("Acak Gambar");
replayButton.setToolTipText("Acak Gambar");
replayButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
replayButtonActionPerformed(evt);
}
});
exitButton.setMnemonic('x');
exitButton.setText("Keluar");
exitButton.setToolTipText("Keluar");
exitButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
exitButtonActionPerformed(evt);
}
});
for (int i = 0; i < cards.length; i++)
{
cards[i] = new JToggleButton("Card: " + i);
cards[i].addActionListener(this);
cards[i].setSize(100,100);
cards[i].setIcon(new ImageIcon(".\\background.jpg"));
cards[i].setText("");
cardPane.add(cards[i]);
}
getContentPane().add(buttonPane,BorderLayout.SOUTH);
getContentPane().add(cardPane,BorderLayout.NORTH);
}
private void replayButtonActionPerformed(java.awt.event.ActionEvent evt)
{
shuffleCards();
}
private void shuffleCards()
{
int[] temparray = new int[3];
for (int shuffle = 0; shuffle < 5; shuffle++)
{
for (int x = 0; x < tilemap.length; x++)
{
temparray[0] = (int)Math.floor
(Math.random()*24);
temparray[1] = tilemap[temparray[0]];
temparray[2] = tilemap[x];
tilemap[x] = temparray[1];
tilemap[temparray[0]] = temparray[2];
}
}
for (int i = 0; i < cards.length; i++)
{
cards[i].setSelectedIcon(new ImageIcon(".\\image"+tilemap[i]+".jpg"));
cards[i].setText("" + i);
cards[i].setEnabled(true);
cards[i].setVisible(true);
cards[i].setSelected(false);
}
cardsLeft = 24;
numTries = 0;
numTriesLabel.setText("Total Coba: "+ numTries+" x");
}
public void actionPerformed(java.awt.event.ActionEvent e)
{
int iconMarker = 0;
int nameMarker = 0;
String card1String = "";
String card2String = "";
String nameText = "";
cardsSelected++;
if (cardsSelected == 3)
{
card1Object = null;
cards[card1Num].setSelected(false);
card2Object = null;
cards[card2Num].setSelected(false);
cardsSelected = 1;
}
if (card1Object == null)
{
card1Object = e.getSource();
card1String = card1Object.toString();
iconMarker = card1String.lastIndexOf(",selectedIcon=");
nameMarker = card1String.lastIndexOf(",text=");
card1Image = card1String.substring(iconMarker+14,nameMarker);
nameText = card1String.substring(nameMarker +6,card1String.length()-1);
card1Num = Integer.parseInt(nameText);
}
else if (card2Object == null)
{
card2Object = e.getSource();
card2String = card2Object.toString();
iconMarker = card2String.lastIndexOf(",selectedIcon=");
nameMarker = card2String.lastIndexOf(",text=");
card2Image = card2String.substring(iconMarker+14,nameMarker);
nameText = card2String.substring(nameMarker+6,card2String.length()-1);
card2Num = Integer.parseInt(nameText);
}
if (card1Object != null && card2Object != null)
{
numTries++;
numTriesLabel.setText("Total Coba: " + numTries+" x");
if (card1Image.equals(card2Image) &&(card1Num != card2Num))
{
cards[card1Num].setVisible(false);
cards[card2Num].setVisible(false);
card1Object = null;
card2Object = null;
cardsLeft -= 2;
cardsSelected = 0;
}
}
if (cardsLeft == 0)
{
getRootPane().setDefaultButton(replayButton);
numTriesLabel.setText("Selamat! Anda Selesai Dalam : "+ numTries+" x Mencoba");
}
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new CariGambar().setVisible(true);
}
});
}
}
setelah itu compiple dan run..
maka hasilnya kurang lebih seperti gambar pertama..
selamat mencoba ..
2 Komentar:
ga bisa gan, yang muncul cuma box2 berisi angka2 ga jelas...
tolong kasih tau cara masukin gambarnya dong gan...
hehehe
thanks ya gan........
bang,,cara simpen gambarnya dimana??ko mlah keluar angka doang,,,thanx ya
Posting Komentar
Berlangganan Posting Komentar [Atom]
<< Beranda