user2171472
user2171472

Reputation: 13

How do I run a (pseudo)main method with an applet?

I'm a beginner/intermediate java programmer that is attempting to code something that is "out-of-my-league". The program is supposed to judge a boxing/MMA match in real time by pressing keys that correspond to different scoring values. I've figured out that I need a KeyListener, and the only way I've found to use that is with an applet.

The problem I've run into is the only cues I have to print out a score come from keyPresses and keyReleases. I want the score to print EVERY second, along with the time. I'm made a clock function and can print every second using another class with a main method, but I don't know how to do this in the applet.

Here's what I have so far:

import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.*;

public class KeyPressTwo
extends Applet
implements KeyListener{

private long t;
private ArrayList<Integer> keysDown = new ArrayList<Integer>();
private double controlOnlyValue = 1; //Stores the score per second for control only
private double threateningValue = 2.5; //Score for threatening with strikes, i.e. landing sig strikes, or sub attempts
private double damagingValue = 4;   //Score for doing significant damage and dominating hea
private static double redTotal = 0; //Stores fighter score
private static double blueTotal = 0;
private static boolean firstRun = true;
private static boolean start = false;
private static boolean releasePressed = false; //Tells KeysReleased method when to wipe keysDown list
private static long roundBeganAt = 0; //System time when the round began    5
private static String redName;
private static String blueName;


public void init(){     
    this.addKeyListener(this);  
    //If names aren't hardcoded in, get them when the program is run
    if (redName == null){
        redName = JOptionPane.showInputDialog("Enter the red corner fighter's name.");
        blueName = JOptionPane.showInputDialog("Enter the blue corner fighter's name.");
    }

}

public void paint(){        
    setSize(500,500);
}


@Override
public void keyPressed(KeyEvent e) {            

    if(!keysDown.contains(e.getKeyCode()))
        keysDown.add(e.getKeyCode());

    //Starts the timer, don't print anything until started
    if(keysDown.contains(KeyEvent.VK_SPACE)){
        start = true;
        roundBeganAt = System.currentTimeMillis();
    }

    //If it's been more than 1s
    if(nextStep()){     
        //If space has been pushed
        if(start){
            if(keysDown.contains(KeyEvent.VK_Z) || keysDown.contains(KeyEvent.VK_NUMPAD1)){
                redTotal += controlOnlyValue;
            }
            if(keysDown.contains(KeyEvent.VK_X) || keysDown.contains(KeyEvent.VK_NUMPAD4)){
                redTotal += threateningValue;
            }
            if(keysDown.contains(KeyEvent.VK_C) || keysDown.contains(KeyEvent.VK_NUMPAD7)){
                redTotal += damagingValue;
            }
            if(keysDown.contains(KeyEvent.VK_COMMA) || keysDown.contains(KeyEvent.VK_NUMPAD3)){
                blueTotal += controlOnlyValue;
            }
            if(keysDown.contains(KeyEvent.VK_M) || keysDown.contains(KeyEvent.VK_NUMPAD6)){
                blueTotal += threateningValue;
            }
            if(keysDown.contains(KeyEvent.VK_N) || keysDown.contains(KeyEvent.VK_NUMPAD9)){
                blueTotal += damagingValue;
            }
            System.out.print("\n" +redName +": " +redTotal +"  \t" +blueName +": " +blueTotal +"\t\t" +time());
            releasePressed = true;
        }
    }
}

//Prints time since start (e.g. 2:05)
private static String time() {      
    String minutes = "";
    String seconds = "";
    int sRaw; //Gets time directly from system, will go above 60
    int s; //Gets time from sRaw, (0 - 59)  

    sRaw = (int)((System.currentTimeMillis() - roundBeganAt))/1000; 
    s = sRaw%60;

    minutes = Integer.toString(sRaw/60);
    if(s < 10)
        seconds = "0" +Integer.toString(s);
    else seconds = Integer.toString(s);


    return minutes +":" +seconds;
}

//Returns true if it's been more than1s since the last time it ran
public boolean nextStep() {
    if(firstRun){
        t = System.currentTimeMillis();
        firstRun = false;
        return true;
    }
    if(System.currentTimeMillis() > t + 1000){
        t = System.currentTimeMillis();
        return true;            
    }else
        return false;
}


public void printList(){
    for(int i : keysDown)
        System.out.print(i +" ");
    System.out.println();
}

@Override
public void keyReleased(KeyEvent e) {

    if(releasePressed){
        keysDown.clear();
        releasePressed = false;
    }

}

@Override
public void keyTyped(KeyEvent e) {

}
}

Upvotes: 1

Views: 300

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

I've figured out that I need a KeyListener,.."

Or preferably key bindings.

..and the only way I've found to use that is with an applet.

Where on Earth did you hear that?!? It is definitely wrong. Start using a JFrame for this app. and it will work better because focus will be more reliable.

Upvotes: 0

user1497579
user1497579

Reputation:

Maybe something along these lines would work for you:

Thread timerOutputThread = new Thread(new Runnable(){
    public boolean running = true;
    public void run(){
        output();
    }
    private void output(){
        try {
            Thread.sleep(1000);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("PRINT THE SCORE HERE");
        if(running){
            output();
        }
    }
});
timerOutputThread.start();

Stick that code wherever you want the Thread timer to be started, and then fill in that spot where it says "PRINT THE SCORE HERE".

Upvotes: 1

Related Questions