Results 1 to 7 of 7
Thread: java game problem
- 02-04-2012, 06:06 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 48
- Rep Power
- 0
java game problem
I am trying to have this quiz like game where user have 3 buttons to choose from. If they chose the wrong answer, an image bomb wil appear and immediately it wil automatically change to the next question and if they chose the corect one, it will also change to the next question. However, my problem here is that i do not know if i wrote my if...else statements correctly. When i run my program, it doesn't change to the next question and there is no bomb image appearing too. I tried many methods to solve this but i stil could'nt. I really need help.
Here is my code and the if... else statements is from line 244 to 388
Java Code:package HealthOK.ui; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import javax.swing.JLabel; import java.awt.Rectangle; import javax.swing.ImageIcon; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingConstants; import javax.swing.JButton; import java.awt.Point; import javax.swing.Timer; import HealthOK.ui.TimerModel; import HealthOK.entity.GameQuestion; import OOPJ.database.DBController; import java.sql.ResultSet; import java.util.ArrayList; public class GamesQuestionPanel extends JPanel{ private static final long serialVersionUID = 1L; private JLabel jLabelQuestion = null; private JLabel jLabelScore = null; private JLabel jLabelHeart1 = null; private JLabel jLabelHeart2 = null; private JLabel jLabelHeart3 = null; private JButton jButtonAnswer1; private JButton jButtonAnswer2; private JButton jButtonAnswer3; private JLabel jLabelDescription1; private JLabel jLabelDescription2; private JLabel jLabelDescription3; private JButton jButtonAdd = null; private JFrame myFrame = null; private JLabel jLabelTimer = null; private Timer t; private TimerModel tm; private int qn_num = 0; private JLabel jLabelScore1 = null; private JLabel jLabelQn; private JButton jButtonEdit = null; private String answer; private int count=0; int Score = 0; public void Randomise() { ArrayList<Integer> random = new ArrayList<Integer>(); try{ DBController db = new DBController(); //passing data source name db.setUp("CFDatabase"); String g_question =" "; String dbQuery = "SELECT * FROM GameQuestion WHERE g_question ='" + g_question + "'"; //for retrieve SQL use readRequest method ResultSet rs = db.readRequest(dbQuery); if (rs.next()){ int g_questionNo =rs.getInt("g_questionNo"); random.add(g_questionNo); System.out.println(random.get(g_questionNo)); } }catch (Exception e) { e.printStackTrace(); } ;} /** * This is the default constructor */ public GamesQuestionPanel() { super(); initialize(); } public GamesQuestionPanel(JFrame f) { this(); myFrame = f; } public GamesQuestionPanel(JFrame f, String s ){ this(); myFrame = f; jLabelTimer.setText(s); } /** * This method initialises this * * @return void */ private void initialize() { jLabelQn = new JLabel(); jLabelQn.setBounds(new Rectangle(13, 96, 55, 50)); jLabelQn.setFont(new Font("Dialog", Font.BOLD, 18)); jLabelQn.setHorizontalAlignment(SwingConstants.CENTER); jLabelQn.setHorizontalTextPosition(SwingConstants.CENTER); jLabelQn.setText(""); jLabelScore1 = new JLabel(); jLabelScore1.setText(""); jLabelScore1.setSize(new Dimension(83, 29)); jLabelScore1.setLocation(new Point(119, 438)); jLabelTimer = new JLabel(); jLabelTimer.setBounds(new Rectangle(17, 23, 199, 53)); jLabelTimer.setText("00:01:00"); jLabelTimer.setHorizontalTextPosition(SwingConstants.CENTER); jLabelTimer.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/timerbg.png"))); jLabelTimer.setFont(new Font("Dialog", Font.BOLD, 18)); jLabelTimer.setHorizontalAlignment(SwingConstants.CENTER); t = new Timer(1000, new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent e) { tm.timeTick(); jLabelTimer.setText(tm.getTime()); if(tm.isTimeUp()){ t.stop(); JOptionPane.showMessageDialog(null, "HighScore: " + Score, "GameOver", JOptionPane.INFORMATION_MESSAGE); int finalScore = Score ; String display = Integer.toString(finalScore); jLabelScore.setText(display); tm.setTime("00:10:00"); JPanel panel = new ProgrammesEventsPanelEnq(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } } }); tm = new TimerModel(); tm.setTime("00:01:00"); t.start(); jLabelDescription3 = new JLabel(); jLabelDescription3.setFont(new Font("Dialog", Font.BOLD, 20)); jLabelDescription3.setLocation(new Point(483, 385)); jLabelDescription3.setSize(new Dimension(218, 33)); jLabelDescription3.setHorizontalAlignment(SwingConstants.CENTER); jLabelDescription3.setHorizontalTextPosition(SwingConstants.CENTER); jLabelDescription3.setForeground(Color.black); jLabelDescription2 = new JLabel(); jLabelDescription2.setFont(new Font("Dialog", Font.BOLD, 20)); jLabelDescription2.setLocation(new Point(244, 386)); jLabelDescription2.setSize(new Dimension(226, 33)); jLabelDescription2.setHorizontalAlignment(SwingConstants.CENTER); jLabelDescription2.setHorizontalTextPosition(SwingConstants.CENTER); jLabelDescription2.setForeground(Color.black); jLabelDescription1 = new JLabel(); jLabelDescription1.setFont(new Font("Dialog", Font.BOLD, 20)); jLabelDescription1.setBackground(new Color(238, 238, 238)); jLabelDescription1.setLocation(new Point(9, 386)); jLabelDescription1.setSize(new Dimension(225, 33)); jLabelDescription1.setHorizontalAlignment(SwingConstants.CENTER); jLabelDescription1.setHorizontalTextPosition(SwingConstants.CENTER); jLabelDescription1.setForeground(Color.black); jLabelHeart3 = new JLabel(); jLabelHeart3.setBounds(new Rectangle(611, 61, 45, 39)); jLabelHeart3.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); jLabelHeart3.setText(""); jLabelHeart2 = new JLabel(); jLabelHeart2.setBounds(new Rectangle(546, 61, 43, 40)); jLabelHeart2.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); jLabelHeart2.setText(""); jLabelHeart1 = new JLabel(); jLabelHeart1.setBounds(new Rectangle(482, 60, 44, 41)); jLabelHeart1.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); jLabelHeart1.setText(""); jLabelScore = new JLabel(); jLabelScore.setBounds(new Rectangle(25, 438, 83, 29)); jLabelScore.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); jLabelScore.setForeground(Color.black); jLabelScore.setText(" Score:"); jLabelQuestion = new JLabel(); jLabelQuestion.setBounds(new Rectangle(71, 95, 618, 52)); jLabelQuestion.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); jLabelQuestion.setForeground(Color.black); this.setSize(712, 500); this.setLayout(null); this.setBackground(Color.white); this.add(jLabelQuestion, null); this.add(jLabelScore, null); this.add(jLabelHeart1, null); this.add(jLabelHeart2, null); this.add(jLabelHeart3, null); this.add(getJButtonAnswer1(), null); this.add(getJButtonAnswer2(), null); this.add(getJButtonAnswer3(), null); this.add(jLabelDescription1, null); this.add(jLabelDescription2, null); this.add(jLabelDescription3, null); this.add(getJButtonAdd(), null); this.add(jLabelTimer, null); this.add(jLabelScore1, null); this.add(jLabelQn, null); this.add(getJButtonEdit(), null); qn_num = 1; GameQuestion g1 = new GameQuestion(qn_num); g1.retrieveGameQuestion(); jLabelQn.setText("Q1."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } /** * This method initialises jButtonAnswer1 * * @return javax.swing.JButton */ private JButton getJButtonAnswer1() { if (jButtonAnswer1 == null) { jButtonAnswer1 = new JButton(); jButtonAnswer1.setBackground(Color.white); jButtonAnswer1.setSize(new Dimension(228, 214)); jButtonAnswer1.setLocation(new Point(5, 164)); jButtonAnswer1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //answer = "1"; qn_num = 1; GameQuestion g1 = new GameQuestion(qn_num); g1.retrieveGameQuestion(); if((jButtonAnswer1.isSelected()) && (g1.getG_correctAnswer() == 1)) { jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } else if((jButtonAnswer1.isSelected()) && (g1.getG_correctAnswer() != 1)) { jButtonAnswer1.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); int delay = 100; try { Thread.sleep(delay); } catch (InterruptedException e1) { e1.printStackTrace(); } jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } } }); } return jButtonAnswer1; } /** * This method initialises jButtonAnswer2 * * @return javax.swing.JButton */ private JButton getJButtonAnswer2() { if (jButtonAnswer2 == null) { jButtonAnswer2 = new JButton(); jButtonAnswer2.setBackground(Color.white); jButtonAnswer2.setSize(new Dimension(228, 214)); jButtonAnswer2.setLocation(new Point(240, 164)); jButtonAnswer2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { // answer = "2"; qn_num = 1; GameQuestion g1 = new GameQuestion(qn_num); g1.retrieveGameQuestion(); if((jButtonAnswer2.isSelected()) && (g1.getG_correctAnswer() == 2)) { jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } else if((jButtonAnswer2.isSelected()) && (g1.getG_correctAnswer() != 2)) { jButtonAnswer2.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); int delay = 100; try { Thread.sleep(delay); } catch (InterruptedException e1) { e1.printStackTrace(); } jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } } }); } return jButtonAnswer2; } /** * This method initialises jButtonAnswer3 * * @return javax.swing.JButton */ private JButton getJButtonAnswer3() { if (jButtonAnswer3 == null) { jButtonAnswer3 = new JButton(); jButtonAnswer3.setBackground(Color.white); jButtonAnswer3.setSize(new Dimension(228, 214)); jButtonAnswer3.setLocation(new Point(474, 163)); jButtonAnswer3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { // answer = "3"; qn_num = 1; GameQuestion g1 = new GameQuestion(qn_num); g1.retrieveGameQuestion(); if((jButtonAnswer3.isSelected()) && (g1.getG_correctAnswer() == 3)) { jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } else if((jButtonAnswer3.isSelected()) && (g1.getG_correctAnswer() != 3)) { jButtonAnswer3.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); int delay = 100; try { Thread.sleep(delay); } catch (InterruptedException e1) { e1.printStackTrace(); } jLabelQn.setText("Q" + "."); jLabelQuestion.setText(g1.getG_question()); jLabelDescription1.setText(g1.getG_imageDescription1()); jLabelDescription2.setText(g1.getG_imageDescription2()); jLabelDescription3.setText(g1.getG_imageDescription3()); jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); } } }); } return jButtonAnswer3; } /** * This method initializes jButtonEdit * * @return javax.swing.JButton */ private JButton getJButtonAdd() { if (jButtonAdd == null) { jButtonAdd = new JButton(); jButtonAdd.setBounds(new Rectangle(591, 9, 90, 39)); jButtonAdd.setFont(new Font("Dialog", Font.BOLD, 16)); jButtonAdd.setText("Add"); jButtonAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { t.stop(); JPanel panel = new GamesCreatePanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); Object admin = null; if(getName() == admin) { jButtonAdd.setVisible(true); } else { jButtonAdd.setVisible(false); } } }); } return jButtonAdd; } /** * This method initializes jButtonEdit * * @return javax.swing.JButton */ private JButton getJButtonEdit() { if (jButtonEdit == null) { jButtonEdit = new JButton(); jButtonEdit.setPreferredSize(new Dimension(65, 31)); jButtonEdit.setSize(new Dimension(90, 39)); jButtonEdit.setText("Edit"); jButtonEdit.setFont(new Font("Dialog", Font.BOLD, 16)); jButtonEdit.setLocation(new Point(492, 9)); jButtonEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { t.stop(); String a = jLabelQuestion.getText(); String b = jButtonAnswer1.getText(); String c = jButtonAnswer2.getText(); String d = jButtonAnswer3.getText(); String f = jLabelDescription1.getText(); String g = jLabelDescription2.getText(); String h = jLabelDescription3.getText(); GameQuestion game = new GameQuestion(); game.setG_question(a); game.setG_image1(b); game.setG_image2(c); game.setG_image3(d); game.setG_imageDescription1(f); game.setG_imageDescription2(g); game.setG_imageDescription3(h); JPanel panel = new GamesEditPanel(myFrame, a, game); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButtonEdit; } }
- 02-04-2012, 06:37 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 48
- Rep Power
- 0
Re: java game problem
I'm sorry about that, a big ThankYou for your help. As for the above threads that you and other people have helped, i solved it! ThankYou very much! Sorry about not replying as i was very happy that i solved the problems. so sorry..
- 02-04-2012, 06:49 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 48
- Rep Power
- 0
Re: java game problem
Sorry if I'm asking too much, but i am experiencing the above problem.. can you help me?
- 02-04-2012, 06:55 PM #4
Re: java game problem
Have you tried debugging your code to see where the execution flow is going and to understand why the program is executing the way it is?
Add some printlns to print out the value of variables as they are changed and as they are tested to see what the program is doing. The output should help you understand what the program is doing.
-
Re: java game problem
You've got uncompilable code present and too much code unrelated to your problem at hand, and it's a beautiful Saturday here. If you don't get a decent solution forthcoming, I suggest that you take the time and effort to create and post an SSCCE. Check the link as it will explain why this is important and what the requirements are.
Thanks for replying to your other threads by the way. I'll delete my hijack post above now.
-
Re: java game problem
Hm, I was trying to go through your code and create an SSCCE myself, but I can't as there are too many dependencies that I have no access to, and the code is too long. :( Looks like if Norm's suggestion doesn't lead to a solution, it's up to you to create the SSCCE.
Luck!
- 02-04-2012, 07:41 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Java Applet (game) Problem
By ninjaturtlez in forum Java AppletsReplies: 16Last Post: 01-07-2012, 10:22 AM -
Java Game Launching Problem
By PaulMoretti in forum Java AppletsReplies: 8Last Post: 08-18-2011, 08:17 PM -
Some problem with game
By kirayamato in forum NetBeansReplies: 13Last Post: 12-01-2010, 06:06 AM -
My java game problem!? Help!!!
By Jcbconway in forum AWT / SwingReplies: 32Last Post: 09-26-2010, 09:33 PM -
Need smart solution on java game lag problem..
By Addez in forum New To JavaReplies: 0Last Post: 11-13-2009, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks