GUNADARMA OF UNIVERSITY

TEKNIK INFORMATIKA'09

Kamis, 30 September 2010

Game acak gambar

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.*;
import java.sql.*;

public class Games extends JFrame {
public String namaanda;
private static final int TOTAL_BUTTONS = 40;
private static final int TOTAL_PAIRS = TOTAL_BUTTONS>>1;
private int buttonsLeft;
private int totalRetries;
private int waktubagi=1;
private JMenuBar menuBar=new JMenuBar();
private JMenu mnuGame = new JMenu("Game"),
         mnuAbout = new JMenu("About");
private JMenuItem mnuItemNew=new JMenuItem("New Game"),
 mnuItemScore=new JMenuItem("Score"),
 mnuItemExit=new JMenuItem("Exit");
private JMenuItem mnuItemHelp=new JMenuItem("Help"),
 mnuItemAboutMe=new JMenuItem("About Me");
private JPanel northPanel = new JPanel(new GridLayout(1,5,10,10));
private JLabel lblButtonsLeft = new JLabel(""+ TOTAL_BUTTONS);
private JLabel lblTotalRetries = new JLabel("0");
private JLabel lblnamaanda = new JLabel("");
private JDesktopPane southPanel = new JDesktopPane();
private GamesButton[] buttons = new GamesButton[TOTAL_BUTTONS];
private Container contentPane = getContentPane();
private GamesButtonHandler eventHandler = new GamesButtonHandler(this);
private Dimension Dimensi=Toolkit.getDefaultToolkit().getScreenSize();

public Games() {
initDisplay();
initGame();
setJam();
}

private void initDisplay() {
setTitle("Games");
mnuGame.add(mnuItemNew);
mnuItemNew.setActionCommand("reset");
mnuItemNew.addActionListener(eventHandler);
mnuGame.add(mnuItemScore);
mnuItemScore.setActionCommand("score");
mnuItemScore.addActionListener(eventHandler);
mnuGame.addSeparator();
mnuGame.add(mnuItemExit);
mnuItemExit.setActionCommand("exit");
mnuItemExit.addActionListener(eventHandler);
mnuAbout.add(mnuItemHelp);
mnuItemHelp.setActionCommand("help");
mnuItemHelp.addActionListener(eventHandler);
mnuAbout.addSeparator();
mnuAbout.add(mnuItemAboutMe);
mnuItemAboutMe.setActionCommand("aboutme");
mnuItemAboutMe.addActionListener(eventHandler);
menuBar.add(mnuGame);
menuBar.add(mnuAbout);
mnuGame.setMnemonic ('G');
mnuAbout.setMnemonic ('A');
mnuItemNew.setMnemonic ('N');
mnuItemScore.setMnemonic ('S');
mnuItemExit.setMnemonic ('X');
mnuItemHelp.setMnemonic ('H');
mnuItemAboutMe.setMnemonic ('A');
northPanel.add(new JLabel("Jumlah Percobaan : "));
northPanel.add(lblTotalRetries);
northPanel.add(new JLabel("Sisa Button : "));
northPanel.add(lblButtonsLeft);
northPanel.add(new JLabel("Nama Anda : "));
northPanel.add(lblnamaanda);

int baris=1,kolom=1;

for(int i=0;i<TOTAL_BUTTONS;i++) {
buttons[i] = new GamesButton();
buttons[i].addActionListener(eventHandler);
if(kolom>8){
baris++;
kolom=1;
}

buttons[i].setBounds(100*kolom-50,100*baris-50,100,100);

kolom++;
southPanel.add(buttons[i]);
}
setJMenuBar (menuBar);
contentPane.add(northPanel,BorderLayout.NORTH);
contentPane.add(southPanel,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(910,650);
setLocation((Dimensi.width-getWidth())/2,(Dimensi.height-getHeight())/2);
show();
namaanda=JOptionPane.showInputDialog(this,"Masukan Nama","Nama anda ?",JOptionPane.QUESTION_MESSAGE);
lblnamaanda.setText(namaanda);
}

private void initGame() {
buttonsLeft = TOTAL_BUTTONS;
totalRetries = 0;
lblButtonsLeft.setText("" + buttonsLeft);
lblTotalRetries.setText("" + totalRetries);
ArrayList list = new ArrayList(TOTAL_BUTTONS);
Random rd = new Random();

for(int i=0;i<TOTAL_PAIRS;i++) {
list.add(new Integer(rd.nextInt(20)));
}

ArrayList tmpList = (ArrayList)list.clone();

for(int i=TOTAL_PAIRS;i>0;i--) {
int pos = rd.nextInt(i);
list.add(tmpList.get(pos));
tmpList.remove(pos);
}

for(int i=0;i< TOTAL_BUTTONS;i++) {
buttons[i].setVal((Integer)list.get(i));
}

list = null;
tmpList = null;
show();
}

public void updateTotalButtonsLeft() {
buttonsLeft-=2;

if(buttonsLeft==0) {
int nilaitotal=0;
if(totalRetries<=20){
nilaitotal=10000;
}else if(totalRetries<=23){
nilaitotal=9900;
}else if(totalRetries<=26){
nilaitotal=9800;
}else if(totalRetries<=29){
nilaitotal=9500;
}else if(totalRetries<=32){
nilaitotal=9000;
}else if(totalRetries<=35){
nilaitotal=8500;
}else if(totalRetries<=38){
nilaitotal=8000;
}else if(totalRetries<=41){
nilaitotal=7500;
}else if(totalRetries<=44){
nilaitotal=7000;
}else if(totalRetries<=47){
nilaitotal=6500;
}else if(totalRetries<=50){
nilaitotal=6000;
}else if(totalRetries<=55){
nilaitotal=5500;
}else if(totalRetries<=60){
nilaitotal=5000;
}else if(totalRetries<=65){
nilaitotal=4500;
}else if(totalRetries<=70){
nilaitotal=4000;
}else if(totalRetries<=75){
nilaitotal=3500;
}else if(totalRetries<=80){
nilaitotal=3000;
}else if(totalRetries<=85){
nilaitotal=2500;
}else if(totalRetries<=90){
nilaitotal=2000;
}else if(totalRetries<=95){
nilaitotal=1500;
}else if(totalRetries<=100){
nilaitotal=1000;
}else if(totalRetries<=110){
nilaitotal=500;
}else if(totalRetries<=120){
nilaitotal=400;
}else if(totalRetries<=130){
nilaitotal=300;
}else if(totalRetries<=140){
nilaitotal=200;
}else if(totalRetries<=150){
nilaitotal=100;
}else if(totalRetries>150){
nilaitotal=0;
}
System.out.println("Total Retries = "+totalRetries);
System.out.println("Nama : " + namaanda);
System.out.println("Waktu anda : " + waktubagi);
System.out.println("Nilai : " + nilaitotal);
System.out.println("Nilai Total : " + nilaitotal/waktubagi);
int result = JOptionPane.showConfirmDialog(this,"Score anda adalah "+nilaitotal+"\nPermainan sudah selesai, Anda ingin mengulang ? " ,"Game Over",JOptionPane.YES_NO_OPTION);
/*
try{
Connect aa=new Connect();
Connection con=aa.OpenConnect();
Statement st=con.createStatement();
String sql="select * from score where Nama='" + namaanda + "'" ;
ResultSet rows=st.executeQuery(sql);
if(rows.next()){
String nilaiawaldatabase=rows.getString("Nilai");
if(Integer.parseInt(nilaiawaldatabase)<=nilaitotal){
String sql1="update score set Nilai='"+nilaitotal+"' where Nama='" + namaanda + "'";
int rows1=st.executeUpdate(sql1);
}
}else{
String sql2="insert into score(Nama,Nilai) values ('"+namaanda+"','"+nilaitotal+"')";
int rows2=st.executeUpdate(sql2);
}
con.close();
}catch(SQLException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(this,"ERROR","ERROR",JOptionPane.ERROR_MESSAGE);
}
*/
if(result==JOptionPane.YES_OPTION)resetGame();
}
lblButtonsLeft.setText("" + buttonsLeft);
}

public void updateTotalRetries() {
totalRetries++;
lblTotalRetries.setText("" + totalRetries);
}
public void resetGame() {
for(int i=0;i<TOTAL_BUTTONS;i++) {
buttons[i].hideValue();
buttons[i].setEnabled(true);
}
namaanda=JOptionPane.showInputDialog(this,"Masukan Nama","Nama anda ?",JOptionPane.QUESTION_MESSAGE);
lblnamaanda.setText(namaanda);
waktubagi=1;
initGame();
}

public static void main(String[] args) {
Games game = new Games();
}
public void setJam() {
ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {      
waktubagi++;
}
   };
    new Timer(10000, taskPerformer).start();
   }
}

class GamesButton extends JButton {
private static final String HIDDEN_CHAR = "XXX";
private Integer val;
public GamesButton() {
hideValue();
}
public void displayValue(){
setText(val.toString());
setIcon(new ImageIcon("gambar/gambar ("+val.toString()+").png"));
}
public void hideValue() {
setText(HIDDEN_CHAR);
setIcon(new ImageIcon("gambar/bg.JPG"));
}
public void setVal(Integer val) {
this.val = val;
}
public Integer getVal() {
return val;
}
}

class GamesButtonHandler implements ActionListener,Runnable{
// private formScore formscoreall=new formScore();
private GamesButton prevButton = null;
private GamesButton currButton = null;
private boolean inAction = false;
private Games parentFrame = null;
public GamesButtonHandler(Games parentFrame) {
this.parentFrame = parentFrame;
}

public void run() {
currButton.displayValue();
try{
Thread.sleep(500);
}catch(Exception err) {}
if(currButton.getVal().intValue()==prevButton.getVal().intValue()){
prevButton.setEnabled(false);
prevButton = null;
currButton.setEnabled (false);
parentFrame.updateTotalButtonsLeft();
}else {
prevButton.hideValue();
prevButton = null;
currButton.hideValue();
}
parentFrame.updateTotalRetries();
inAction = false;
}
public void actionPerformed(ActionEvent e) {
if(inAction)return;

if(e.getActionCommand().equals("reset")) {
parentFrame.resetGame();
return;
}
if(e.getActionCommand().equals("score")) {
// formscoreall.show();
// formscoreall.tampilketabel1();
return;
}
if(e.getActionCommand().equals("exit")) {
System.exit(0);
return;
}
if(e.getActionCommand().equals("help")) {
JOptionPane.showMessageDialog(this.parentFrame,"Peraturan Permainan\n1. Peraturan 1\n2. Peraturan 2\n3. Peraturan 3","Help",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(e.getActionCommand().equals("aboutme")) {
JOptionPane.showMessageDialog(this.parentFrame,"Created By : Nick Alexander \nGame Tebak Gambar...\n@Copyrigth 2009-2010","About Me",JOptionPane.INFORMATION_MESSAGE);
return;
}
currButton = (GamesButton)e.getSource();
if(prevButton==null){
currButton.displayValue();
prevButton = currButton;
} else if(prevButton!=currButton){
inAction = true;
Thread workerThread = new Thread(this);
workerThread.start();
}
}
}

NB : bisa menggunakan JCreator atau Edit Plus+

Tidak ada komentar:

Posting Komentar