The first design was made in advance:
game design
game score
then the input source for the form games :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* game.java
*
* Created on 10 Jul 11, 17:10:37
*/
package gameuas_523;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Unda
*/
public class game extends javax.swing.JFrame {
CariSuara cs;
Connection connection;
Statement statement;
ResultSet resultSet;
String query;
String url = "jdbc:mysql://localhost:3306/dbVisual";
String username = "root";
String password = "";
DefaultTableModel model;
/** Creates new form game */
public game() {
initComponents();
this.nama = nama;
lbNama.setText(this.nama);
posisiPlayerX = lbPlayer.getX();
posisiPlayerY = lbPlayer.getY();
posisiEnemyX = lbEnemy.getX();
posisiEnemyY = lbEnemy.getY();
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
kolomEnemy =lbEnemy.getY()-10;
if(lbEnemy.getY()<=0){
lbEnemy.setLocation(lbEnemy.getX(), pDasar.getHeight());
}else{
lbEnemy.setLocation(lbEnemy.getX(), kolomEnemy);
}
int tinggiPlayer=lbPlayer.getY()+lbPlayer.getHeight();
int lebarPlayer=lbPlayer.getX()+lbPlayer.getWidth();
int tinggiEnemy=lbEnemy.getY()+lbEnemy.getHeight();
int lebarEnemy=lbEnemy.getX()+lbEnemy.getWidth();
int tinggiFinish=lbFinish.getY()+lbFinish.getHeight();
int lebarFinish=lbFinish.getX()+lbFinish.getWidth();
if (((tinggiPlayer>=lbEnemy.getY())&&(lebarPlayer>=lbEnemy.getX()))&&((lbPlayer.getY()<= tinggiEnemy)&&(lbPlayer.getX()<=lebarEnemy)))
{
cs = new CariSuara("file:///"+lbEnemy.getText());
cs.bunyi();
JOptionPane.showMessageDialog(null, "ANDA KALAH.....!!!! Score anda " + C);
timer.stop();
String nama = JOptionPane.showInputDialog(new javax.swing.ImageIcon(getClass().getResource("")));//
JOptionPane.showMessageDialog(null, "Score : "+lbScore.getText()+" Point");
new formDatabase (lbScore.getText(), nama).setVisible(true);
lbScore.setText(String.valueOf(D));
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
}else{
if (((tinggiPlayer>=lbFinish.getY())&&(lebarPlayer>=lbFinish.getX()))&&((lbPlayer.getY()<= tinggiFinish)&&(lbPlayer.getX()<=lebarFinish)))
{
cs = new CariSuara("file:///"+lbFinish.getText());
cs.bunyi();
JOptionPane.showMessageDialog(null, "ANDA MENANG.....!!!! Score anda " + C);
timer.stop();
String nama = JOptionPane.showInputDialog(new javax.swing.ImageIcon(getClass().getResource("")));//
JOptionPane.showMessageDialog(null, "Score : "+lbScore.getText()+" Point");
new formDatabase (lbScore.getText(), nama).setVisible(true);
lbScore.setText(String.valueOf(D));
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
}
}
}
});
}
/** 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() {
jButton5 = new javax.swing.JButton();
pDasar = new javax.swing.JPanel();
bStop = new javax.swing.JButton();
bPlay = new javax.swing.JButton();
bPause = new javax.swing.JButton();
bKiri = new javax.swing.JButton();
bClose = new javax.swing.JButton();
bAtas = new javax.swing.JButton();
bKanan = new javax.swing.JButton();
bBawah = new javax.swing.JButton();
lbPlayer = new javax.swing.JLabel();
lbEnemy = new javax.swing.JLabel();
lbNama = new javax.swing.JLabel();
lbNama1 = new javax.swing.JLabel();
lbScore = new javax.swing.JLabel();
lbScore1 = new javax.swing.JLabel();
lbFinish = new javax.swing.JLabel();
kecepatan = new javax.swing.JComboBox();
lbMusic = new javax.swing.JLabel();
bSkor = new javax.swing.JButton();
jButton5.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pDasar.setBackground(new java.awt.Color(255, 102, 255));
pDasar.setLayout(null);
bStop.setText("STOP");
bStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bStopActionPerformed(evt);
}
});
pDasar.add(bStop);
bStop.setBounds(30, 360, 100, 23);
bPlay.setText("PLAY");
bPlay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPlayActionPerformed(evt);
}
});
pDasar.add(bPlay);
bPlay.setBounds(30, 310, 100, 23);
bPause.setText("PAUSE");
bPause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPauseActionPerformed(evt);
}
});
pDasar.add(bPause);
bPause.setBounds(140, 310, 100, 23);
bKiri.setText("KIRI");
bKiri.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKiriActionPerformed(evt);
}
});
pDasar.add(bKiri);
bKiri.setBounds(343, 340, 110, 23);
bClose.setText("CLOSE");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
pDasar.add(bClose);
bClose.setBounds(140, 360, 100, 23);
bAtas.setText("ATAS");
bAtas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bAtasActionPerformed(evt);
}
});
pDasar.add(bAtas);
bAtas.setBounds(419, 300, 120, 23);
bKanan.setText("KANAN");
bKanan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKananActionPerformed(evt);
}
});
pDasar.add(bKanan);
bKanan.setBounds(507, 340, 120, 23);
bBawah.setText("BAWAH");
bBawah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBawahActionPerformed(evt);
}
});
pDasar.add(bBawah);
bBawah.setBounds(419, 380, 130, 23);
lbPlayer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/Megaman-drill.gif"))); // NOI18N
pDasar.add(lbPlayer);
lbPlayer.setBounds(10, 220, 160, 71);
lbEnemy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/Megaman_anim-slashman.gif"))); // NOI18N
lbEnemy.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bird 05 flying.wav");
pDasar.add(lbEnemy);
lbEnemy.setBounds(269, 344, 50, 60);
pDasar.add(lbNama);
lbNama.setBounds(60, 10, 100, 20);
lbNama1.setText("Player :");
pDasar.add(lbNama1);
lbNama1.setBounds(10, 10, 50, 20);
lbScore.setText("0");
pDasar.add(lbScore);
lbScore.setBounds(540, 10, 90, 20);
lbScore1.setText("Score :");
pDasar.add(lbScore1);
lbScore1.setBounds(490, 10, 50, 20);
lbFinish.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/avatar46125_1.gif.png"))); // NOI18N
lbFinish.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bungee_scream.wav");
pDasar.add(lbFinish);
lbFinish.setBounds(570, 50, 60, 70);
kecepatan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "15" }));
pDasar.add(kecepatan);
kecepatan.setBounds(410, 10, 56, 20);
lbMusic.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bugspray.wav");
pDasar.add(lbMusic);
lbMusic.setBounds(20, 370, 0, 40);
bSkor.setText("SKOR");
bSkor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSkorActionPerformed(evt);
}
});
pDasar.add(bSkor);
bSkor.setBounds(10, 50, 120, 23);
getContentPane().add(pDasar, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-656)/2, (screenSize.height-447)/2, 656, 447);
}// </editor-fold>
private void bKiriActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
if(kecepatan.getSelectedIndex()==0)
{
barisPlayer=lbPlayer.getX()-20;
}
else
if(kecepatan.getSelectedIndex()==1)
{
barisPlayer=lbPlayer.getX()-30;
}
else
if(kecepatan.getSelectedIndex()==2)
{
barisPlayer=lbPlayer.getX()-40;
}
lbPlayer.setLocation(barisPlayer, lbPlayer.getY());
A = Integer.parseInt(lbScore.getText());
C = A - 5;
lbScore.setText(String.valueOf(C));
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void bAtasActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
kolomPlayer =lbPlayer.getY()-10;
lbPlayer.setLocation(lbPlayer.getX(), kolomPlayer);
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bKananActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
if(kecepatan.getSelectedIndex()==0)
{
barisPlayer=lbPlayer.getX()+20;
}
else
if(kecepatan.getSelectedIndex()==1)
{
barisPlayer=lbPlayer.getX()+30;
}
else
if(kecepatan.getSelectedIndex()==2)
{
barisPlayer=lbPlayer.getX()+40;
}
lbPlayer.setLocation(barisPlayer, lbPlayer.getY());
A = Integer.parseInt(lbScore.getText());
C = A + 10;
lbScore.setText(String.valueOf(C));
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bBawahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
kolomPlayer =lbPlayer.getY()+10;
lbPlayer.setLocation(lbPlayer.getX(), kolomPlayer);
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bPlayActionPerformed(java.awt.event.ActionEvent evt) {
timer.start();
}
private void bStopActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
JOptionPane.showMessageDialog(null, "GAME OVER....!!!! Score anda = "+lbScore.getText());
lbScore.setText(String.valueOf(D));
}
private void bPauseActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
}
private void bSkorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new formDatabase().setVisible(true);
query = "insert into dbScore values (' "+bSkor.getText()+")";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
model = new DefaultTableModel();
} catch(SQLException ex) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new game().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bAtas;
private javax.swing.JButton bBawah;
private javax.swing.JButton bClose;
private javax.swing.JButton bKanan;
private javax.swing.JButton bKiri;
private javax.swing.JButton bPause;
private javax.swing.JButton bPlay;
private javax.swing.JButton bSkor;
private javax.swing.JButton bStop;
private javax.swing.JButton jButton5;
private javax.swing.JComboBox kecepatan;
private javax.swing.JLabel lbEnemy;
private javax.swing.JLabel lbFinish;
private javax.swing.JLabel lbMusic;
private javax.swing.JLabel lbNama;
private javax.swing.JLabel lbNama1;
private javax.swing.JLabel lbPlayer;
private javax.swing.JLabel lbScore;
private javax.swing.JLabel lbScore1;
private javax.swing.JPanel pDasar;
// End of variables declaration
int kolomPlayer, barisPlayer, kolomEnemy, posisiPlayerX, posisiPlayerY, posisiEnemyX, posisiEnemyY, A, B, C, D, E;
int delay = 90;
Timer timer;
String nama;
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* game.java
*
* Created on 10 Jul 11, 17:10:37
*/
package gameuas_523;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Unda
*/
public class game extends javax.swing.JFrame {
CariSuara cs;
Connection connection;
Statement statement;
ResultSet resultSet;
String query;
String url = "jdbc:mysql://localhost:3306/dbVisual";
String username = "root";
String password = "";
DefaultTableModel model;
/** Creates new form game */
public game() {
initComponents();
this.nama = nama;
lbNama.setText(this.nama);
posisiPlayerX = lbPlayer.getX();
posisiPlayerY = lbPlayer.getY();
posisiEnemyX = lbEnemy.getX();
posisiEnemyY = lbEnemy.getY();
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
kolomEnemy =lbEnemy.getY()-10;
if(lbEnemy.getY()<=0){
lbEnemy.setLocation(lbEnemy.getX(), pDasar.getHeight());
}else{
lbEnemy.setLocation(lbEnemy.getX(), kolomEnemy);
}
int tinggiPlayer=lbPlayer.getY()+lbPlayer.getHeight();
int lebarPlayer=lbPlayer.getX()+lbPlayer.getWidth();
int tinggiEnemy=lbEnemy.getY()+lbEnemy.getHeight();
int lebarEnemy=lbEnemy.getX()+lbEnemy.getWidth();
int tinggiFinish=lbFinish.getY()+lbFinish.getHeight();
int lebarFinish=lbFinish.getX()+lbFinish.getWidth();
if (((tinggiPlayer>=lbEnemy.getY())&&(lebarPlayer>=lbEnemy.getX()))&&((lbPlayer.getY()<= tinggiEnemy)&&(lbPlayer.getX()<=lebarEnemy)))
{
cs = new CariSuara("file:///"+lbEnemy.getText());
cs.bunyi();
JOptionPane.showMessageDialog(null, "ANDA KALAH.....!!!! Score anda " + C);
timer.stop();
String nama = JOptionPane.showInputDialog(new javax.swing.ImageIcon(getClass().getResource("")));//
JOptionPane.showMessageDialog(null, "Score : "+lbScore.getText()+" Point");
new formDatabase (lbScore.getText(), nama).setVisible(true);
lbScore.setText(String.valueOf(D));
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
}else{
if (((tinggiPlayer>=lbFinish.getY())&&(lebarPlayer>=lbFinish.getX()))&&((lbPlayer.getY()<= tinggiFinish)&&(lbPlayer.getX()<=lebarFinish)))
{
cs = new CariSuara("file:///"+lbFinish.getText());
cs.bunyi();
JOptionPane.showMessageDialog(null, "ANDA MENANG.....!!!! Score anda " + C);
timer.stop();
String nama = JOptionPane.showInputDialog(new javax.swing.ImageIcon(getClass().getResource("")));//
JOptionPane.showMessageDialog(null, "Score : "+lbScore.getText()+" Point");
new formDatabase (lbScore.getText(), nama).setVisible(true);
lbScore.setText(String.valueOf(D));
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
}
}
}
});
}
/** 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() {
jButton5 = new javax.swing.JButton();
pDasar = new javax.swing.JPanel();
bStop = new javax.swing.JButton();
bPlay = new javax.swing.JButton();
bPause = new javax.swing.JButton();
bKiri = new javax.swing.JButton();
bClose = new javax.swing.JButton();
bAtas = new javax.swing.JButton();
bKanan = new javax.swing.JButton();
bBawah = new javax.swing.JButton();
lbPlayer = new javax.swing.JLabel();
lbEnemy = new javax.swing.JLabel();
lbNama = new javax.swing.JLabel();
lbNama1 = new javax.swing.JLabel();
lbScore = new javax.swing.JLabel();
lbScore1 = new javax.swing.JLabel();
lbFinish = new javax.swing.JLabel();
kecepatan = new javax.swing.JComboBox();
lbMusic = new javax.swing.JLabel();
bSkor = new javax.swing.JButton();
jButton5.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pDasar.setBackground(new java.awt.Color(255, 102, 255));
pDasar.setLayout(null);
bStop.setText("STOP");
bStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bStopActionPerformed(evt);
}
});
pDasar.add(bStop);
bStop.setBounds(30, 360, 100, 23);
bPlay.setText("PLAY");
bPlay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPlayActionPerformed(evt);
}
});
pDasar.add(bPlay);
bPlay.setBounds(30, 310, 100, 23);
bPause.setText("PAUSE");
bPause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPauseActionPerformed(evt);
}
});
pDasar.add(bPause);
bPause.setBounds(140, 310, 100, 23);
bKiri.setText("KIRI");
bKiri.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKiriActionPerformed(evt);
}
});
pDasar.add(bKiri);
bKiri.setBounds(343, 340, 110, 23);
bClose.setText("CLOSE");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
pDasar.add(bClose);
bClose.setBounds(140, 360, 100, 23);
bAtas.setText("ATAS");
bAtas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bAtasActionPerformed(evt);
}
});
pDasar.add(bAtas);
bAtas.setBounds(419, 300, 120, 23);
bKanan.setText("KANAN");
bKanan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKananActionPerformed(evt);
}
});
pDasar.add(bKanan);
bKanan.setBounds(507, 340, 120, 23);
bBawah.setText("BAWAH");
bBawah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBawahActionPerformed(evt);
}
});
pDasar.add(bBawah);
bBawah.setBounds(419, 380, 130, 23);
lbPlayer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/Megaman-drill.gif"))); // NOI18N
pDasar.add(lbPlayer);
lbPlayer.setBounds(10, 220, 160, 71);
lbEnemy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/Megaman_anim-slashman.gif"))); // NOI18N
lbEnemy.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bird 05 flying.wav");
pDasar.add(lbEnemy);
lbEnemy.setBounds(269, 344, 50, 60);
pDasar.add(lbNama);
lbNama.setBounds(60, 10, 100, 20);
lbNama1.setText("Player :");
pDasar.add(lbNama1);
lbNama1.setBounds(10, 10, 50, 20);
lbScore.setText("0");
pDasar.add(lbScore);
lbScore.setBounds(540, 10, 90, 20);
lbScore1.setText("Score :");
pDasar.add(lbScore1);
lbScore1.setBounds(490, 10, 50, 20);
lbFinish.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameuas_523/avatar46125_1.gif.png"))); // NOI18N
lbFinish.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bungee_scream.wav");
pDasar.add(lbFinish);
lbFinish.setBounds(570, 50, 60, 70);
kecepatan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "15" }));
pDasar.add(kecepatan);
kecepatan.setBounds(410, 10, 56, 20);
lbMusic.setText("C:\\Documents and Settings\\inn\\My Documents\\NetBeansProjects\\gameUAS_523\\src\\gameuas_523\\bugspray.wav");
pDasar.add(lbMusic);
lbMusic.setBounds(20, 370, 0, 40);
bSkor.setText("SKOR");
bSkor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSkorActionPerformed(evt);
}
});
pDasar.add(bSkor);
bSkor.setBounds(10, 50, 120, 23);
getContentPane().add(pDasar, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-656)/2, (screenSize.height-447)/2, 656, 447);
}// </editor-fold>
private void bKiriActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
if(kecepatan.getSelectedIndex()==0)
{
barisPlayer=lbPlayer.getX()-20;
}
else
if(kecepatan.getSelectedIndex()==1)
{
barisPlayer=lbPlayer.getX()-30;
}
else
if(kecepatan.getSelectedIndex()==2)
{
barisPlayer=lbPlayer.getX()-40;
}
lbPlayer.setLocation(barisPlayer, lbPlayer.getY());
A = Integer.parseInt(lbScore.getText());
C = A - 5;
lbScore.setText(String.valueOf(C));
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void bAtasActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
kolomPlayer =lbPlayer.getY()-10;
lbPlayer.setLocation(lbPlayer.getX(), kolomPlayer);
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bKananActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
if(kecepatan.getSelectedIndex()==0)
{
barisPlayer=lbPlayer.getX()+20;
}
else
if(kecepatan.getSelectedIndex()==1)
{
barisPlayer=lbPlayer.getX()+30;
}
else
if(kecepatan.getSelectedIndex()==2)
{
barisPlayer=lbPlayer.getX()+40;
}
lbPlayer.setLocation(barisPlayer, lbPlayer.getY());
A = Integer.parseInt(lbScore.getText());
C = A + 10;
lbScore.setText(String.valueOf(C));
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bBawahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cs = new CariSuara("file:///"+lbMusic.getText());
cs.bunyi();
if(timer.isRunning()){
kolomPlayer =lbPlayer.getY()+10;
lbPlayer.setLocation(lbPlayer.getX(), kolomPlayer);
}else{
JOptionPane.showMessageDialog(null, "please select PLAY");
}
}
private void bPlayActionPerformed(java.awt.event.ActionEvent evt) {
timer.start();
}
private void bStopActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
lbPlayer.setLocation(posisiPlayerX, posisiPlayerY);
lbEnemy.setLocation(posisiEnemyX, posisiEnemyY);
JOptionPane.showMessageDialog(null, "GAME OVER....!!!! Score anda = "+lbScore.getText());
lbScore.setText(String.valueOf(D));
}
private void bPauseActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
}
private void bSkorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new formDatabase().setVisible(true);
query = "insert into dbScore values (' "+bSkor.getText()+")";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
model = new DefaultTableModel();
} catch(SQLException ex) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new game().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bAtas;
private javax.swing.JButton bBawah;
private javax.swing.JButton bClose;
private javax.swing.JButton bKanan;
private javax.swing.JButton bKiri;
private javax.swing.JButton bPause;
private javax.swing.JButton bPlay;
private javax.swing.JButton bSkor;
private javax.swing.JButton bStop;
private javax.swing.JButton jButton5;
private javax.swing.JComboBox kecepatan;
private javax.swing.JLabel lbEnemy;
private javax.swing.JLabel lbFinish;
private javax.swing.JLabel lbMusic;
private javax.swing.JLabel lbNama;
private javax.swing.JLabel lbNama1;
private javax.swing.JLabel lbPlayer;
private javax.swing.JLabel lbScore;
private javax.swing.JLabel lbScore1;
private javax.swing.JPanel pDasar;
// End of variables declaration
int kolomPlayer, barisPlayer, kolomEnemy, posisiPlayerX, posisiPlayerY, posisiEnemyX, posisiEnemyY, A, B, C, D, E;
int delay = 90;
Timer timer;
String nama;
}
DETAILS: for that bold action is respectively on the image and buttons, to use it right click on the picture or button select EVENTS> ACTION
then the input source for the form score :
package gameuas_523;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* fromDatabase.java
*
* Created on 11 Jul 11, 15:06:08
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Unda
*/
public class formDatabase extends javax.swing.JFrame {
Connection connection;
Statement statement;
ResultSet resultSet;
String query;
String url = "jdbc:mysql://localhost:3306/db523";
String username = "root";
String password = "";
DefaultTableModel model;
/** Creates new form fromDatabase */
public formDatabase() {
initComponents();
statusKoneksi();
model = new DefaultTableModel();
initTabel();
refreshTabel();
}
public formDatabase(String skor, String nama ) {
initComponents();
this.skor = skor;
lbSkor.setText(this.skor);
this.nama = nama;
lbNama.setText(this.nama);
}
private void statusKoneksi(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
JOptionPane.showMessageDialog(null, "Koneksi Sukses");
}catch(Exception e){
// JOptionPane.showMessageDialog(null, "Koneksi Gagal, Karena "+e.getMessage()+
// " tidak ada!!!");
System.exit(0);
}
}
private void initTabel(){
tSkor.setModel(model);
model.addColumn("NAMA");
model.addColumn("SKOR");
}
private void Bersih(){
lbNama.setText("");
lbSkor.setText("");
}
private void refreshTabel(){
query = "select * from tgame order by SKOR desc";
try{
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while(resultSet.next()){
model.addRow(new Object[]{resultSet.getString(1),resultSet.getString(2)});
}
statement.close();
connection.close();
}catch(SQLException e){
// JOptionPane.showMessageDialog(null, "Lihat Data Error di "+e.getSQLState());
}
}
/** 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() {
jPanel1 = new javax.swing.JPanel();
bSkor = new javax.swing.JButton();
lbNama = new javax.swing.JLabel();
lbSkor = new javax.swing.JLabel();
bClose = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
bBersih = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tSkor = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 204, 255));
jPanel1.setLayout(null);
bSkor.setText("High Score");
bSkor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSkorActionPerformed(evt);
}
});
jPanel1.add(bSkor);
bSkor.setBounds(30, 210, 100, 23);
lbNama.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jPanel1.add(lbNama);
lbNama.setBounds(170, 10, 140, 30);
lbSkor.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jPanel1.add(lbSkor);
lbSkor.setBounds(170, 40, 140, 30);
bClose.setText("Exit");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
jPanel1.add(bClose);
bClose.setBounds(320, 20, 100, 23);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel2.setText("SKOR");
jPanel1.add(jLabel2);
jLabel2.setBounds(10, 40, 70, 30);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel3.setText(":");
jPanel1.add(jLabel3);
jLabel3.setBounds(130, 40, 20, 30);
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel4.setText("NAMA");
jPanel1.add(jLabel4);
jLabel4.setBounds(10, 10, 120, 30);
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel5.setText(":");
jPanel1.add(jLabel5);
jLabel5.setBounds(130, 10, 20, 30);
bBersih.setText("Clear");
bBersih.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBersihActionPerformed(evt);
}
});
jPanel1.add(bBersih);
bBersih.setBounds(260, 210, 100, 23);
tSkor.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
tSkor.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tSkorMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tSkor);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 80, 410, 120);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-443)/2, (screenSize.height-297)/2, 443, 297);
}// </editor-fold>
private void bSkorActionPerformed(java.awt.event.ActionEvent evt) {
query = "insert into tgame values ('"+lbNama.getText()+"', '"+lbSkor.getText()+"')";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
// JOptionPane.showMessageDialog(null,"Data Berhasil Dimasukkan.");
model = new DefaultTableModel();
initTabel();
refreshTabel();
Bersih();
} catch(SQLException ex) {
// JOptionPane.showMessageDialog(null,"Data Error di "+ex.getMessage());
}
}
private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void bBersihActionPerformed(java.awt.event.ActionEvent evt) {
query = "delete from tgame where NAMA ='"+lbNama.getText()+"'";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
// JOptionPane.showMessageDialog(null,"Data Berhasil Dihapus.");
model = new DefaultTableModel();
initTabel();
refreshTabel();
Bersih();
} catch(SQLException ex) {
// JOptionPane.showMessageDialog(null,"Data Error di "+ex.getMessage());
}
}
private void tSkorMouseClicked(java.awt.event.MouseEvent evt) {
lbNama.setText(tSkor.getValueAt(tSkor.getSelectedRow(), 0).toString());
lbSkor.setText(tSkor.getValueAt(tSkor.getSelectedRow(), 1).toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
new formDatabase().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bBersih;
private javax.swing.JButton bClose;
private javax.swing.JButton bSkor;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbNama;
private javax.swing.JLabel lbSkor;
private javax.swing.JTable tSkor;
// End of variables declaration
String skor,nama,status;
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* fromDatabase.java
*
* Created on 11 Jul 11, 15:06:08
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Unda
*/
public class formDatabase extends javax.swing.JFrame {
Connection connection;
Statement statement;
ResultSet resultSet;
String query;
String url = "jdbc:mysql://localhost:3306/db523";
String username = "root";
String password = "";
DefaultTableModel model;
/** Creates new form fromDatabase */
public formDatabase() {
initComponents();
statusKoneksi();
model = new DefaultTableModel();
initTabel();
refreshTabel();
}
public formDatabase(String skor, String nama ) {
initComponents();
this.skor = skor;
lbSkor.setText(this.skor);
this.nama = nama;
lbNama.setText(this.nama);
}
private void statusKoneksi(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
JOptionPane.showMessageDialog(null, "Koneksi Sukses");
}catch(Exception e){
// JOptionPane.showMessageDialog(null, "Koneksi Gagal, Karena "+e.getMessage()+
// " tidak ada!!!");
System.exit(0);
}
}
private void initTabel(){
tSkor.setModel(model);
model.addColumn("NAMA");
model.addColumn("SKOR");
}
private void Bersih(){
lbNama.setText("");
lbSkor.setText("");
}
private void refreshTabel(){
query = "select * from tgame order by SKOR desc";
try{
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while(resultSet.next()){
model.addRow(new Object[]{resultSet.getString(1),resultSet.getString(2)});
}
statement.close();
connection.close();
}catch(SQLException e){
// JOptionPane.showMessageDialog(null, "Lihat Data Error di "+e.getSQLState());
}
}
/** 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() {
jPanel1 = new javax.swing.JPanel();
bSkor = new javax.swing.JButton();
lbNama = new javax.swing.JLabel();
lbSkor = new javax.swing.JLabel();
bClose = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
bBersih = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tSkor = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 204, 255));
jPanel1.setLayout(null);
bSkor.setText("High Score");
bSkor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSkorActionPerformed(evt);
}
});
jPanel1.add(bSkor);
bSkor.setBounds(30, 210, 100, 23);
lbNama.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jPanel1.add(lbNama);
lbNama.setBounds(170, 10, 140, 30);
lbSkor.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jPanel1.add(lbSkor);
lbSkor.setBounds(170, 40, 140, 30);
bClose.setText("Exit");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
jPanel1.add(bClose);
bClose.setBounds(320, 20, 100, 23);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel2.setText("SKOR");
jPanel1.add(jLabel2);
jLabel2.setBounds(10, 40, 70, 30);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel3.setText(":");
jPanel1.add(jLabel3);
jLabel3.setBounds(130, 40, 20, 30);
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel4.setText("NAMA");
jPanel1.add(jLabel4);
jLabel4.setBounds(10, 10, 120, 30);
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel5.setText(":");
jPanel1.add(jLabel5);
jLabel5.setBounds(130, 10, 20, 30);
bBersih.setText("Clear");
bBersih.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBersihActionPerformed(evt);
}
});
jPanel1.add(bBersih);
bBersih.setBounds(260, 210, 100, 23);
tSkor.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
tSkor.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tSkorMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tSkor);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 80, 410, 120);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-443)/2, (screenSize.height-297)/2, 443, 297);
}// </editor-fold>
private void bSkorActionPerformed(java.awt.event.ActionEvent evt) {
query = "insert into tgame values ('"+lbNama.getText()+"', '"+lbSkor.getText()+"')";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
// JOptionPane.showMessageDialog(null,"Data Berhasil Dimasukkan.");
model = new DefaultTableModel();
initTabel();
refreshTabel();
Bersih();
} catch(SQLException ex) {
// JOptionPane.showMessageDialog(null,"Data Error di "+ex.getMessage());
}
}
private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void bBersihActionPerformed(java.awt.event.ActionEvent evt) {
query = "delete from tgame where NAMA ='"+lbNama.getText()+"'";
try {
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
// JOptionPane.showMessageDialog(null,"Data Berhasil Dihapus.");
model = new DefaultTableModel();
initTabel();
refreshTabel();
Bersih();
} catch(SQLException ex) {
// JOptionPane.showMessageDialog(null,"Data Error di "+ex.getMessage());
}
}
private void tSkorMouseClicked(java.awt.event.MouseEvent evt) {
lbNama.setText(tSkor.getValueAt(tSkor.getSelectedRow(), 0).toString());
lbSkor.setText(tSkor.getValueAt(tSkor.getSelectedRow(), 1).toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
new formDatabase().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bBersih;
private javax.swing.JButton bClose;
private javax.swing.JButton bSkor;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbNama;
private javax.swing.JLabel lbSkor;
private javax.swing.JTable tSkor;
// End of variables declaration
String skor,nama,status;
}
DETAILS: for that bold action is respectively on the image and buttons, to use it right click on the picture or button select EVENTS> ACTION
to private void tSkorMouseClicked (java.awt.event.MouseEvent evt) how to make EVENTS> MOUSE> MOUSECLIKED
to private void tSkorMouseClicked (java.awt.event.MouseEvent evt) how to make EVENTS> MOUSE> MOUSECLIKED
0 komentar:
Posting Komentar