Hi all,

I'm making a applet, about a users reaction time for when a coloured square changes colour...

Ive seemed to get something off to a bit of a start, but its very basic at the moment, and i would just like to ask for a bit of assistance or guidance as how i can achieve a few things...

Firstsly, check out the code that i've attached so you know what im talking about...When the user clicks to start the game, i want to change the message and colour of square from green to say orange...and maybe have the message change to say 'click when turns red' or something like that...Ive fiddled around with it a lot, but can only get the message to appear AFTER the square has turned red.

Also, you'll see if you try the test, after turning red, it turns green straight away, and then it doesnt change colour until it turns red again.

The reason i want to change this is it is very confusing for users to know if the test has started or not...

Another question i have (sorry, i have lots of issues with the applet) is that i've made a variable to determine if the user has cheated, you will see that it only tells you this after the square has gone red..instead i want it to stop straight away.

Sorry for the long post and many questions!

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ExerciseFiveReactionTimes extends Applet implements MouseListener {
    long start, end, randomTime;
    boolean started = false;
    boolean cheat;
    String info;
    String reactionTime = "Please Click To Start The Reaction Speed Test";
    
    public void init() {
        addMouseListener(this);
    }
    
    public void timer() {
        started = true;        
        start = System.currentTimeMillis();                                       // Start timer
        randomTime = Math.round(Math.random() * 5000) + 1000;                     // Set random time
        try {
            Thread.sleep(randomTime);            
        }
        catch(InterruptedException e) {}                                          // Stop at random times
        
    }
    public void paint(Graphics g)  {
        g.drawString("" + reactionTime, 120, 60);
        g.drawRect(71, 35, 37, 37);
        if (started) {
            g.setColor(Color.red);
            g.fillRect(72, 36, 36, 36);
        } else {            
            g.setColor(Color.green);
            g.fillRect(72, 36, 36, 36);              
        }        
    }
    
    public void mousePressed(MouseEvent e) {
        notFirst = true;
        if (started) {              
            started = false;                                                    //end clock method
            end = System.currentTimeMillis();                                       //end reactiontimer
            if (end < (start + randomTime)) {
                reactionTime = "You Cheated. Click to start again";
                cheat = true;
            } else {
                reactionTime = "Your Reaction Speed Is " + (end - (start + randomTime)) + " Milliseconds. Click to start again";
            }               
        } else {
            cheat = false;
            timer();
        }
        repaint();
    }    
    
    public void mouseMoved(MouseEvent e) {}
    public void mouseDragged(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

Thanks for looking at this, and thanks to anyone who has any suggestions about this! :)

-Crawf

Well, seeing as though no one has had much interest in this post, i wanted to clarify i dont want to ask people how to solve my problem, rather i just want to talk through it with someone with greater knowledge of java than me...

I hope no one has gotten the wrong idea, and thinking im looking for a hand out lol.

But anyway, everyone is probably busy, but i just wanted to make sure! :)

Thanks!

-Crawf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.