Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.70 KB | None | 0 0
  1.  
  2. import java.awt.Toolkit;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.Statement;
  6. import java.util.TimerTask;
  7. import javax.swing.JFrame;
  8. import javax.swing.JOptionPane;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15.  
  16. /**
  17. *
  18. * @author acer
  19. */
  20. public class JFrameHardQ3 extends javax.swing.JFrame {
  21. java.util.Timer timer;
  22. int countdown=20;
  23. Toolkit toolkit;
  24. private static String player_game;
  25.  
  26. /**
  27. * Creates new form JFrameHardQ3
  28. */
  29. public JFrameHardQ3() {
  30. initComponents();
  31.  
  32. jLabelScore.setText("Score: "+Integer.toString(JFrameLowQ1.score1));
  33. this.setLayout(null); //THIS WILL ENABLE COMPONENTS TO BE MOVED DYNAMICALLY/RANDOMLY
  34. this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH); //THIS IS TO MAXIMIZE THE WINDOW
  35. toolkit = Toolkit.getDefaultToolkit();
  36.  
  37. TimerTask task;
  38. task = new TimerTask(){
  39. public void run(){
  40. jLabel3.setText(Integer.toString(countdown));
  41. if (countdown==0){
  42. jLabel3.setVisible(false);
  43. jButtonSend.setVisible(true);
  44. timer.cancel();
  45.  
  46. String query = "'UPDATE `user` SET `score`='"+tfName.score1+"' WHERE name='"+player_game.playername+"';
  47. }
  48. countdown--;
  49. }
  50.  
  51. };
  52. jLabelScore.setText("Score: "+JFrameLowQ1.score1);
  53. countdown =20;
  54. jLabel3.setVisible(true);
  55. timer = new java.util.Timer();
  56. timer.scheduleAtFixedRate(task, 0, 800);
  57. }
  58. public Connection getConnection(){
  59. Connection con;
  60. try{
  61. con=DriverManager.getConnection("jdbc:mysql://localhost/databasegame","root","");
  62. return con;
  63. }
  64. catch (Exception e){
  65. e.printStackTrace();
  66. return null;
  67. }
  68. }
  69.  
  70. //execute the sql query
  71. public void executeQuery(String query)
  72. {
  73. Connection con =getConnection();
  74. Statement st;
  75. try{
  76. st=con.createStatement();
  77. if(st.executeUpdate(query)==1)
  78. {
  79. JOptionPane.showMessageDialog(null,"data inserted successfully");
  80. }else{
  81. JOptionPane.showMessageDialog(null,"data is not inserted successfully");
  82. }
  83. }catch(Exception ex){
  84. }
  85. }
  86.  
  87. /**
  88. * This method is called from within the constructor to initialize the form.
  89. * WARNING: Do NOT modify this code. The content of this method is always
  90. * regenerated by the Form Editor.
  91. */
  92. @SuppressWarnings("unchecked")
  93. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  94. private void initComponents() {
  95.  
  96. jScrollPane1 = new javax.swing.JScrollPane();
  97. jTextPaneQ3 = new javax.swing.JTextPane();
  98. jLabel1 = new javax.swing.JLabel();
  99. jLabel2 = new javax.swing.JLabel();
  100. jButtonSend = new javax.swing.JButton();
  101. jLabel3 = new javax.swing.JLabel();
  102. jLabel4 = new javax.swing.JLabel();
  103. jTextField2 = new javax.swing.JTextField();
  104. tfAnswer = new javax.swing.JTextField();
  105. jLabelScore = new javax.swing.JLabel();
  106.  
  107. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  108.  
  109. jTextPaneQ3.setEditable(false);
  110. jTextPaneQ3.setBackground(new java.awt.Color(0, 153, 153));
  111. jTextPaneQ3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
  112. jTextPaneQ3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  113. jTextPaneQ3.setText(" HARD QUESTION : QUESTION 3");
  114. jScrollPane1.setViewportView(jTextPaneQ3);
  115.  
  116. jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/air.png"))); // NOI18N
  117.  
  118. jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/pisau.png"))); // NOI18N
  119.  
  120. jButtonSend.setBackground(new java.awt.Color(0, 153, 153));
  121. jButtonSend.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  122. jButtonSend.setText("HANTAR!");
  123. jButtonSend.addActionListener(new java.awt.event.ActionListener() {
  124. public void actionPerformed(java.awt.event.ActionEvent evt) {
  125. jButtonSendActionPerformed(evt);
  126. }
  127. });
  128.  
  129. jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/orang2.png"))); // NOI18N
  130.  
  131. jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tali.jpg"))); // NOI18N
  132.  
  133. jTextField2.setEditable(false);
  134. jTextField2.setBackground(new java.awt.Color(0, 153, 51));
  135. jTextField2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  136. jTextField2.setText("HINT : 5 PERKATAAN");
  137.  
  138. jLabelScore.setText("Score :");
  139.  
  140. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  141. getContentPane().setLayout(layout);
  142. layout.setHorizontalGroup(
  143. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  144. .addGroup(layout.createSequentialGroup()
  145. .addComponent(jLabel1)
  146. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  147. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
  148. .addGap(2, 2, 2)
  149. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
  150. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  151. .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
  152. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  153. .addGap(122, 122, 122)
  154. .addComponent(tfAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
  155. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  156. .addComponent(jButtonSend, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
  157. .addGap(195, 195, 195))
  158. .addGroup(layout.createSequentialGroup()
  159. .addContainerGap()
  160. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  161. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
  162. .addGroup(layout.createSequentialGroup()
  163. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
  164. .addGap(183, 183, 183)
  165. .addComponent(jLabelScore, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
  166. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  167. );
  168. layout.setVerticalGroup(
  169. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  170. .addGroup(layout.createSequentialGroup()
  171. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  172. .addGroup(layout.createSequentialGroup()
  173. .addGap(21, 21, 21)
  174. .addComponent(jLabelScore)
  175. .addGap(87, 87, 87)
  176. .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
  177. .addGroup(layout.createSequentialGroup()
  178. .addContainerGap()
  179. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  180. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  181. .addGroup(layout.createSequentialGroup()
  182. .addGap(82, 82, 82)
  183. .addComponent(jLabel1))
  184. .addGroup(layout.createSequentialGroup()
  185. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
  186. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  187. .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
  188. .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))))
  189. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  190. .addGroup(layout.createSequentialGroup()
  191. .addGap(78, 78, 78)
  192. .addComponent(jButtonSend, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
  193. .addGroup(layout.createSequentialGroup()
  194. .addGap(55, 55, 55)
  195. .addComponent(tfAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
  196. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
  197. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
  198. .addContainerGap())
  199. );
  200.  
  201. pack();
  202. }// </editor-fold>
  203.  
  204. private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {
  205. // TODO add your handling code here:
  206. jButtonSend.setVisible(false);
  207. if(tfAnswer.getText().equals("air dicincang tak akan putus")){
  208. JOptionPane.showMessageDialog(null,"\n---------------------------"
  209. +"\nCongratulation "+player_game+"you have win!");
  210.  
  211.  
  212. String name = tfAnswer.getText();
  213. String JFrameLowQ1;
  214. String query = "'UPDATE `user` SET `score`='" '" +JFrameLowQ1.score1+"' WHERE name="'+player_game.playername+'";
  215.  
  216. executeQuery(query);
  217. // JOptionPane.showMessageDialog(null,"This is your final score!!!"+JFrameLowQ1.score1+=6);
  218. JOptionPane.showMessageDialog(null, "Congrateee you are done and you are the winner!!!!!!!!!!");
  219. JOptionPane.showMessageDialog(null, "Thank you ^.^ ");
  220. JOptionPane.showMessageDialog(null, "THANK YOU FOR PLAYING THIS GAME...^,^"
  221. + "SEE YOU NEXT TIME.... "
  222. +"\n\nCredit to the members:"
  223. +"\n- King"
  224. +"\n- KakYa"
  225. +"\n- Fiy"
  226. +"\n- Yaya"
  227. +"\n- Raihan"
  228. +"\n- Aira"
  229. +"\n- Nini");
  230.  
  231.  
  232. // JOptionPane.showMessageDialog(null," CLICK BUTTON SETERUSNYA FOR NEXT QUESTION!!!");
  233. JFrameLowQ1.score1+=6;
  234. new player_game().setVisible(true);
  235. this.setVisible(false);
  236. countdown=20;
  237.  
  238.  
  239. }
  240. else
  241. {
  242. JOptionPane.showMessageDialog(null,"\n---------------------------"
  243. +"\nI'm sorry "+player_game+"you have lost!");
  244.  
  245. String name = tfAnswer.getText();
  246. String query = "'UPDATE 'user' SET 'score'='"+JFrameLowQ1.score1+'" WHERE name='"+player_game.playername+";
  247. executeQuery(query);
  248.  
  249. new player_game().setVisible(true);
  250. this.setVisible(false);
  251. }
  252. }
  253.  
  254. /**
  255. * @param args the command line arguments
  256. */
  257. public static void main(String args[]) {
  258. /* Set the Nimbus look and feel */
  259. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  260. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  261. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  262. */
  263. try {
  264. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  265. if ("Nimbus".equals(info.getName())) {
  266. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  267. break;
  268. }
  269. }
  270. } catch (ClassNotFoundException ex) {
  271. java.util.logging.Logger.getLogger(JFrameHardQ3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  272. } catch (InstantiationException ex) {
  273. java.util.logging.Logger.getLogger(JFrameHardQ3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  274. } catch (IllegalAccessException ex) {
  275. java.util.logging.Logger.getLogger(JFrameHardQ3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  276. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  277. java.util.logging.Logger.getLogger(JFrameHardQ3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  278. }
  279. //</editor-fold>
  280.  
  281. /* Create and display the form */
  282. java.awt.EventQueue.invokeLater(new Runnable() {
  283. public void run() {
  284. new JFrameHardQ3().setVisible(true);
  285. }
  286. });
  287. }
  288.  
  289. // Variables declaration - do not modify
  290. private javax.swing.JButton jButtonSend;
  291. private javax.swing.JLabel jLabel1;
  292. private javax.swing.JLabel jLabel2;
  293. private javax.swing.JLabel jLabel3;
  294. private javax.swing.JLabel jLabel4;
  295. private javax.swing.JLabel jLabelScore;
  296. private javax.swing.JScrollPane jScrollPane1;
  297. private javax.swing.JTextField jTextField2;
  298. private javax.swing.JTextPane jTextPaneQ3;
  299. private javax.swing.JTextField tfAnswer;
  300. // End of variables declaration
  301.  
  302. // private void printwin(String player_game) {
  303. // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  304. //}
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement