Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

help with little program

Thread view: 
john abbas - 07 Apr 2005 19:57 GMT
Hi i was wondering if anyone could help me out with this little program i
made.. the program is a game. it has clock faces on it and you have to
guess the right time, according to the clock. im storing the answers and
stuff in arrays. im trying to make the program able to keep track of the
wrong problems. then i want it to post the clocks you got wrong and then
repost them on the clock until you get it right... if anyone thinks they
can help me email me civicish@hotmail.com so i can send the java files, for
you to look at. or post ur email and ill send them to you
richard.anderson970@gmail.com - 07 Apr 2005 21:56 GMT
How about posting some code you have attempted to write to the
newsgroup?  Maybe you'll get more help like that?
john abbas - 07 Apr 2005 23:42 GMT
import javax.swing.*;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class ClockGame extends JFrame implements ActionListener {
// private instance variables
JButton button;
int buttonCount;
String input;
JTextField inputHour;

AudioClip audio;
JLabel prompt;
TicToc questionClock, answerClock;

DecimalFormat df = new DecimalFormat("00");
boolean[] correct = new boolean[100];
int[] hour = new int[100];
int[] minute = new int[100];
int[] wrongArray = new int[100];
int[][] hourMinute = new int[100][2];

int dots = 0;
int wrongCount = 0;
int zeroCounter = 0;
private URL menuItemName;
private AbstractButton missedMessage;
private int arrayCounter;
public static void main(String[] args) {
 ClockGame mine = new ClockGame();
 mine.setVisible(true);
}
public ClockGame() {
 setTitle("Analog Clock Game");
 setSize(800, 600);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // get the content pane and set properties
 Container contentPane = getContentPane();
 contentPane.setBackground(Color.cyan);
 contentPane.setLayout(null);
 // button
 button = new JButton("Check Answer");
 button.setBounds(210, 380, 220, 140);
 button.addActionListener(this);
 contentPane.add(button);
 buttonCount = 0;
 // text field
 inputHour = new JTextField();
 inputHour.setBounds(270, 330, 100, 20);
 inputHour.addActionListener(this);
 contentPane.add(inputHour);
 
 //AudioClip audio = Applet.newAudioClip(new URL("file", "localhost",
"kids_klub_mono_8bit.wav"));
 prompt = new JLabel();
 prompt.setText("Enter the time ---> hh:mm");
 prompt.setForeground(Color.yellow);
 prompt.setFont(new Font("SansSerif", 0, 16));
 prompt.setBounds(225, 338, 250, 50);
 contentPane.add(prompt);
 
 //image = new ImageIcon("image3.jpg");
 //imageLabel = new JLabel(image);
 //imageLabel.setBounds(0, 0, 290, 200);
 //contentPane.add(imageLabel);

 questionClock = new TicToc();
 questionClock.setBounds(20, 20, 280, 280);
 questionClock.showCurrentTime();
 contentPane.add(questionClock);
 answerClock = new TicToc();
 answerClock.setBounds(350, 20, 280, 280);
 answerClock.setVisible(false);
 contentPane.add(answerClock);
 JMenuBar myMenuBar = new JMenuBar();
 setJMenuBar(myMenuBar);
 JMenu fileMenu = new JMenu("Menu");
 JMenu otherMenu = new JMenu("Other Stuff");
 JMenuItem clearItem = new JMenuItem("Clear");
 JMenuItem logItem = new JMenuItem("Show Log");
 JMenuItem analyses = new JMenuItem("Analyze");
 JMenuItem showMinDots = new JMenuItem("Show Tick Marks");
 JMenuItem fixThem = new JMenuItem("Redo Wrong Ones");
 JMenuItem fixMany = new JMenuItem("How Many Left to Redo");
 JMenuItem playMusic = new JMenuItem("Start music");
 JMenuItem stopMusic = new JMenuItem("Turn music off");
 JMenuItem exitItem = new JMenuItem("Exit");
 
 fileMenu.add(clearItem);
 fileMenu.add(logItem);
 fileMenu.add(exitItem);
 otherMenu.add(analyses);
 otherMenu.add(fixThem);
 otherMenu.add(fixMany);
 otherMenu.add(showMinDots);
 otherMenu.add(playMusic);
 otherMenu.add(stopMusic);
 clearItem.addActionListener(this);
 logItem.addActionListener(this);
 exitItem.addActionListener(this);
 analyses.addActionListener(this);
 fixThem.addActionListener(this);
 fixMany.addActionListener(this);
 showMinDots.addActionListener(this);
 playMusic.addActionListener(this);
 stopMusic.addActionListener(this);
 myMenuBar.add(fileMenu);
 myMenuBar.add(otherMenu);
 
}
public void actionPerformed(ActionEvent event) {
 Object source = event.getSource();
 if (source == inputHour){
  button.requestFocusInWindow();
  Toolkit.getDefaultToolkit().beep();
 }
 if (source == button)
  if(button.getText().equals("Check Answer")) {
   checkAnswer();
   buttonCount++;
   button.setText("Next Question");
  }
  else if (button.getText().equals("Next Question")) {
   
   
    questionClock.setClock((int) (Math.random() * 60), (int) (Math
      .random() * 12));
    button.setText("Check Answer");
    inputHour.setText("");
    inputHour.requestFocusInWindow();
    answerClock.setVisible(false);
    repaint();
   
   
  }
 
 if (source instanceof JMenuItem) {
  String menuItemName = event.getActionCommand();
  System.out.println(menuItemName);
  if (menuItemName.equals("Clear")) {
   inputHour.setText("");
   button.setText("Next Question");
  }if (menuItemName.equals("Exit")) {
   System.exit(0);
  }
  if (menuItemName.equals("Show Log")) {
   for(int i = 0; i < buttonCount; i++)
   {
   
   System.out.println("\nClock time #" + (i+ 1)+ "--->  " + hour[i] + ":"
+
     df.format(minute[i]));
   System.out.println("Your Quess #" + (i + 1) + "--->  " + hourMinute[i]
[0] +
     ":" + df.format(hourMinute[i][1]) + " " + (correct[i] ? "Correct" :
"Incorrect"));
   }
  }
 
  if(menuItemName.equals("Analyze"))
  {
   JOptionPane.showMessageDialog(null, "Analyses of Clock's" +
   "\n# of clocks guessed : " + buttonCount +
   "\n# of right guessesd : " + (buttonCount - zeroCounter) +
   "\n# of wrong guesses : " + zeroCounter);
   
  }
 
  if(menuItemName.equals("Start Music"))
  {
   audio.loop();
   
   
  }
 
  if(menuItemName.equals("Turn mucic off"))
  {
   audio.stop();
   
  }
 
 
  if(menuItemName.equals("Show Tick Marks"))
  {
   TicToc.toggleTickMarks();
   repaint();
  }
 
 
 
 
  if(menuItemName.equals("How Many Left to Redo"))
  {
      fixIt();
  }
 
  }
  if(menuItemName.equals("Redo Wrong Ones"))
  {
   int h;
   int m;
   int hold;
   
   if(wrongCount != 0){
    arrayCounter++;
    JOptionPane.showMessageDialog(null, "You got " +
    wrongCount + " incorrect and now you " +
    "\n must fix them one at a time ");
   
    hold = wrongArray[arrayCounter];
    h = hour[hold];
    m = minute[hold];
   
    questionClock.setClock(h, m);
   
    button.setText("CheckAnswer");
    inputHour.setText("");
    inputHour.requestFocusInWindow();
    answerClock.setVisible(false);
    wrongCount--;
    repaint();
   }
   
    else if(wrongCount == 0)
     JOptionPane.showMessageDialog(null, "There are " +
     wrongCount + " left to do. :)");
  }
 
    if(menuItemName.equals("How Many Left to Redo"))
    {
     JOptionPane.showMessageDialog(null, "You still have " +
       wrongCount + " clocks left to get right that were perviously
missed");
    }
  }

   

private void checkAnswer() {
 
 int hold = 0;
 
 input = inputHour.getText();
 //System.out.println(input);
 Pattern pattern = Pattern.compile("\\d+\\D+\\d+");
 Matcher matcher = pattern.matcher(input);
 String time = "10:12";// just a number
 if (matcher.find())
  time = matcher.group();
 //System.out.println("checkAnswer time: " + time);
 String[] hourMinuteStr = time.split("\\D+");
 hourMinute[buttonCount][0] = Integer.parseInt(hourMinuteStr[0]);
 hourMinute[buttonCount][1] = Integer.parseInt(hourMinuteStr[1]);
 
 
 hour[buttonCount] = questionClock.getHour() == 0 ? 12 :
questionClock.getHour();
 minute[buttonCount] = questionClock.getMinute();
 correct[buttonCount] = hourMinute[buttonCount][0] == hour[buttonCount]
   && hourMinute[buttonCount][1] == minute[buttonCount];
 
 answerClock.setClock(hourMinute[buttonCount][1], hourMinute[buttonCount]
[0]);
 answerClock.setSubTitle((correct[buttonCount] ? "Right" : "Wrong"));
 answerClock.setVisible(true);
 hold = (correct[buttonCount] ? 0 : 1);
 if(hold == 1)
 {
  wrongCount++;
  zeroCounter++;
  wrongArray[wrongCount]= buttonCount;
 }
 
}

public void fixIt(){
 
 ActionEvent event = null;
 boolean right = false;
 int h;
 int m;
 int hold;  
 
 if(wrongCount != 0){
 
  JOptionPane.showMessageDialog(null, "You got " +
    wrongCount + " wrong "+
    "\n fix them ");
 
 
 for(int i = 0; i < wrongCount; i++){
 
  hold = wrongArray[i];
  h = hour[hold];
  m = minute[hold];
 
  questionClock.setClock(h, m);
  while(right != true){
   
   Object here = event.getSource();
   
   if (here == inputHour){
    button.requestFocusInWindow();
    Toolkit.getDefaultToolkit().beep();
   }
   if (here == button)
    if(button.getText().equals("Check Answer")) {
     checkAnswer();
     right = (correct[hold] ? true : false);
     
     buttonCount++;
     button.setText("Next Clock to fix");
    }
    else if (button.getText().equals("Next Clock To Fix")) {
     
     
     button.setText("Check Answer");
     inputHour.setText("");
     inputHour.requestFocusInWindow();
     answerClock.setVisible(false);
     repaint();
    }
   
  }
 
 }
 
 }
 else
  JOptionPane.showMessageDialog(null, "No More are Wrong");
 
 
}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.