1

我是一名初学者/中级 Java 程序员,正在尝试编写“超出我的联盟”的东西。该程序应该通过按下对应于不同得分值的键来实时判断拳击/MMA比赛。我发现我需要一个 KeyListener,而我发现使用它的唯一方法是使用小程序。

我遇到的问题是我必须打印出分数的唯一线索来自 keyPresses 和 keyReleases。我希望分数与时间一起打印每一秒。我制作了一个时钟功能,可以使用另一个带有 main 方法的类每秒打印一次,但我不知道如何在小程序中执行此操作。

这是我到目前为止所拥有的:

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) {

}
}
4

2 回答 2

1

也许这些方面的东西对你有用:

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();

将该代码粘贴到您希望启动线程计时器的任何位置,然后填写“在此处打印分数”的位置。

于 2013-03-14T20:24:39.887 回答
0

我发现我需要一个 KeyListener,......”

或者最好是键绑定

..我发现使用它的唯一方法是使用小程序。

你到底从哪里听到的?!?这绝对是错误的。开始使用JFrame这个应用程序。它会更好地工作,因为焦点会更可靠。

于 2013-03-15T00:17:24.037 回答