5/24/2010

Current Program

Hey guys, Shaun here, here's the current build of the program, it doesn't do everything yet but-

What it does do: Counts clicks when the timer is going, times 20 seconds, displays player images, plays a sound after 20 seconds and randomly assigns a player as the Hunter.

What it doesn't do: Runs through more than once, stops the score from being added to the Hunter's total score (for when their hat thing gets knocked off), has the right images, has a restart button.

So, that should all be pretty easy except for the restart. I don;t think there's a built-in function to do it, so I'll either have another application that opens the main one or reset all variables.

You'll notice that the score isn't displayed, if I have enough time tonight I'll add that (won't take more than 10 minutes to add it and get it in the right position).

The only other thing's the final images which I'm waiting for from Lex, so could you get them to me sometime tomorrow please Alex?

Code below, sorry it's so long:



import javax.imageio.ImageIO;
import javax.swing.*;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Container;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public class MouseClickEventDemo extends Applet implements ActionListener {

BufferedImage image;
Random randomGenerator = new Random();
int playerNum=randomGenerator.nextInt(2);
int clickCount=0;
public Container getContentPane;
String timerSeconds = "ss";
boolean trueClicked=false;
final static JTextArea textArea = new JTextArea();
Button okButton;
static Panel panel = new MouseClickEventDemo();
int timeInt = 0;
int secondTimeInt=0;

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton) {
if (timeInt!=secondTimeInt){
clickCount++;
}
//System.out.println(clickCount);
}
repaint();
}

public void displayText() {
textArea.setText("Number of hit " + clickCount);
}

private void initComponents() {
textArea.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){

if (e.getButton() == MouseEvent.NOBUTTON){
}
else if (e.getButton() == MouseEvent.BUTTON1) {
clickCount++;
}
}
});
}

//SwingUtilities.invokeLater(new Runnable() {
public void run() {

okButton = new Button("Action!");
panel.add(okButton);
okButton.addActionListener(this);

//int timeInt = 0;
//int secondTimeInt=0;
new MouseClickEventDemo().setVisible(true);
timeInt = Integer.parseInt(DateUtils.now());
System.out.println("Now : " + DateUtils.now());
secondTimeInt=timeInt+20;
System.out.println("Then before : " + secondTimeInt);
if (secondTimeInt>60){
secondTimeInt=secondTimeInt-60;
}
System.out.println("Then after : " + secondTimeInt);
while (timeInt!=secondTimeInt){
initComponents();
timeInt = Integer.parseInt(DateUtils.now());
}

try {
PlayDing.playding();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(clickCount);

}

public void mouseClicked (MouseEvent e) {

}

public void mousePressed (MouseEvent e) {
clickCount++;
}


public String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(timerSeconds);
return sdf.format(cal.getTime());
}


public MouseClickEventDemo() {
try {
playerNum=playerNum+1;
Image playerImage;
//initComponents();
//getContentPane.add(textArea);

String imageName = "C:/Users/Shaun/Pictures/player"+playerNum+".jpg";
File input = new File(imageName);
image = ImageIO.read(input);
} catch (IOException ie) {
System.out.println("Error:"+ie.getMessage());
}
}

public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);
}

static public void main(String arg[]) throws
Exception {
JFrame frame = new JFrame("Display image");
panel.add(textArea);
frame.getContentPane().add(panel);
frame.setSize(500, 500);
frame.setVisible(true);

int timeInt = Integer.parseInt(DateUtils.now());
System.out.println(timeInt);

MouseClickEventDemo someString = new MouseClickEventDemo();
someString.run();
}

}


Different class


import sun.audio.*;
import java.io.*;

public class PlayDing {


public static void playding() throws IOException{

InputStream in = new FileInputStream("C:/Users/Shaun/Pictures/ding.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);

}

}

Different class

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class DateUtils {
public static final String timerSeconds = "ss";

public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(timerSeconds);
return sdf.format(cal.getTime());
}

//public static void main(String arg[]) {
public void getDate(){
int timeInt = Integer.parseInt(DateUtils.now());
System.out.println("Now : " + DateUtils.now());
int secondTimeInt=timeInt+20;
System.out.println("Then before : " + secondTimeInt);
if (secondTimeInt>60){
secondTimeInt=secondTimeInt-60;
}
System.out.println("Then after : " + secondTimeInt);
while (timeInt!=secondTimeInt){
timeInt = Integer.parseInt(DateUtils.now());
}
System.out.println("It has been 20 seconds");
}
}


-Shaun

3 comments:

  1. That's Terrific Shaun :D Good job, I'm working on the images now, sorry that it's taken me awhile, Imaging is really beating he crap out of me :(

    ReplyDelete
  2. Thanks Lex :D Lol yeah I know the feeling, it's ok take your time, I'd hate it if I was stressing you out or something.

    ReplyDelete
  3. Very nice.
    So, once we have the pictures,
    our game is complete!

    ReplyDelete

Followers