Why am I getting local variable 'timer' not initialized? [duplicate] - java

I'm trying to stop a timer inside the the ActionListener. Below is the code of what i'm trying to do. I'm tring to stop the timer i created when a certain condition is met inside the actionPerformed method. timer.stop() does not work , the compiler does not let me do that.
Any help . suggestion , advice would be really helpful.
public class ToggleAnnotationsAction extends IdentifiedMultiAction {
//This status indicates if the Toggle action has been completed
/**
* Defines the toggling direction of a <code>ToggleAnnotationAction</code> instance.
*/
public static enum Direction {FORWARD, BACKWARD};
private Direction myDir;
/**
* Create an action with the direction presets given by the provided <code>Enum</code>.
*
* #param dir An <code>Enum</code> defined in this class which maps to the correct direction of toggling
* #see behaviors.multiact.IdentifiedMultiAction#IdentifiedMultiAction(Enum)
*/
public ToggleAnnotationsAction(Direction dir) {
super(dir);
this.myDir = dir;
}
/**
* Performs the toggling, moving the audio position to the next/previous annotation.
*
* Afterward sends an update to all <code>UpdatingActions<code>.
*
* Since the waveform display autonomously decides when to paint itself, this action may not result in an instant visual change.
*
* <p>Prints warnings if an appropriate Annotation could not be found, despite the action being enabled.
*
* #param e The <code>ActionEvent</code> provided by the trigger
*/
public void actionPerformed(ActionEvent e) {
//Reset Status to 0
status =0;
Annotation ann = findAnnotation(myDir, CurAudio.getMaster().framesToMillis(CurAudio.getAudioProgress()));
if(ann == null) {
System.err.println("It should not have been possible to call " + getClass().getName() + ". Could not find matching annotation");
}
else {
final long approxFrame = CurAudio.getMaster().millisToFrames(ann.getTime());
final long curFrame = CurAudio.getAudioProgress();
if(approxFrame < 0 || approxFrame > CurAudio.getMaster().durationInFrames() - 1) {
GiveMessage.errorMessage("The annotation I am toggling to isn't in range.\nPlease check annotation file for errors.");
return;
}
Timer timer = new Timer(10, new ActionListener() {
private long panFrame = curFrame;
private long endFrame = approxFrame;
public void actionPerformed(ActionEvent evt) {
if(myDir == Direction.FORWARD){
if (panFrame >= endFrame) {
//How do i Stop my timer here ?
return;
}
CurAudio.setAudioProgressWithoutUpdatingActions(panFrame);
panFrame += 4000;
}
else if(myDir == Direction.BACKWARD){
if (panFrame <= endFrame) {
// How do i Stop my timer here ?
return;
}
CurAudio.setAudioProgressWithoutUpdatingActions(panFrame);
panFrame -= 4000;
}
}
}
);
timer.start();
}
MyFrame.getInstance().requestFocusInWindow();
}
/**
* A forward (backward) <code>ToggleAnnotationsAction</code> should be enabled only when audio is open, not playing, and when there is an annotation following (preceding) the current position.
*/
#Override
public void update() {
if(CurAudio.audioOpen()) {
if(CurAudio.getPlayer().getStatus() == PrecisionPlayer.Status.PLAYING) {
setEnabled(false);
}
else {
double curTimeMillis = CurAudio.getMaster().framesToMillis(CurAudio.getAudioProgress());
if(findAnnotation(myDir, curTimeMillis) != null) {
setEnabled(true);
}
else {
setEnabled(false);
}
}
}
else {
setEnabled(false);
}
}
/**
* Finds the next/previous <code>Annotation</code> relative to a certain audio position in milliseconds.
*
* #param dir The direction of movement
* #param curTimeMillis The present time in milliseconds
*
* #return In principle, the <code>Annotation</code> after/before <code>curTimeMillis</code>
*/
private Annotation findAnnotation(Direction dir, double curTimeMillis) {
Annotation[] anns = AnnotationDisplay.getAnnotationsInOrder();
if(myDir == Direction.FORWARD) {
for(int i = 0; i < anns.length; i++) {
if(anns[i].getTime() - curTimeMillis > 1) {
return anns[i];
}
}
}
else {
for(int i = anns.length - 1; i >= 0; i--) {
if(curTimeMillis - anns[i].getTime() > 1) {
return anns[i];
}
}
}
return null;
}
}
Thanks in Advance
Krishnan

Also possible:
final Timer timer = new Timer(10, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
(as in the question, except that you can refer to timer here)
}
});
Or, use the event object to get the source (and cast it, boo):
final Timer timer = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
((Timer)evt.getSource()).stop();
}
});
Or, keep the timer in an instance variable and you can reference it from your handler or have the handler call a method on your class which could stop/start it.

Fun problem.
You have to make Timer final to access it in the anonymous ActionListener you want to stop it in. But the anonymous ActionListener still won't compile because the Timer hasn't been initialized yet.
Note that a Timer may not be your best choice here. But to make this work as is, I'd wrap the Timer in an inner class.
In your ToggleAnnotationsAction.actionPerformed() method add a line like:
MyTimer timer = new MyTimer();
timer.start();
Then a class like this could be used, replacing my simple ActionListener code with your Timer's ActionListener code:
private class MyTimer implements ActionListener{
private Timer timer;
private MyTimer(){
timer = new Timer(10, this);
}
public void start(){
timer.start();
}
public void stop(){
timer.stop();
}
public void actionPerformed(ActionEvent e){
if(isTimeToStop()){
stop();
}
}
public boolean isTimeToStop(){
return true;
}
}

Related

How to trigger a second event handler after a delay

I'm trying to make a 'delete' button that deletes either a) a single character in a text-area if pressed and released in quick succession, or b) all of the text if pressed and held down for more than 2 seconds without release.
Is this possible in Java?
In order to be able to detect long key presses from the keyboard input, you need to understand and use 2 concepts:
1. KeyListener.
2. How to get current time.
Once you understand both, just compare the times between keyPressed and keyReleased and call the proper delete action.
Alternativly to a Swing-Timer (watch here for example) you could use a simple SwingWorker to realize the delay. In general you should not execute a delay, i.e. by Thread.sleep(1000), on the Swing EDT, since this would block the gui (for further information ...). Furthermore you should use a MouseListener to capture other informations that you need (stop the timer when mouse is released or exits the buttona area). Here is a very short example:
public class JButtonTest extends JFrame {
public static void main(String[] args) {
JButtonTest x = new JButtonTest();
JButton button = new JButton("Delete");
button.addMouseListener(new MouseAdapter() {
private static final long DELTA = 2000;
private SwingWorker<Void, Void> waitingWorker;
private Long timer;
#Override
public void mousePressed(MouseEvent e) {
timer = System.currentTimeMillis();
System.out.println("delete single char");//DO single delete here
if (waitingWorker != null && !waitingWorker.isDone())
waitingWorker.cancel(true);
waitingWorker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
Thread.sleep(DELTA);
return null;
}
#Override
protected void done() {
if (timer != null && System.currentTimeMillis() >= timer + DELTA)
System.out.println("delete all text");//DO text delete here
}
};
waitingWorker.execute();
}
#Override
public void mouseReleased(MouseEvent e) {
timer = null;
}
#Override
public void mouseExited(MouseEvent e) {
timer = null;
}
});
x.add(button);
x.setSize(100, 100);
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setVisible(true);
}
}

How to make a 3 minutes countdown timer

I want to use the method javax.swing.Timer to create a timer that will start at 3:00, then go down to 0:00. I have no idea how to use this method and how to proceed. From now on, I have this code, but I have no idea if it is good. One thing sure is that it doesn't work.
private javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
int minute = 3;
int seconde = 60;
do {
lblTimer.setText(Integer.toString(seconde));
seconde--;
} while (seconde != 0);
}
});
In this example, a TimerButton responds after the delay passed to the constructor, e.g.
new TimerButton("Back in three minutes", 3 * 60 * 1000));
Your StopListener would take the desired action when the Timer expires, e.g.
private class StopListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
timer.stop();
// further actions here
}
}
See How to Use Swing Timers for additional details.

java keylistener on linux

I'm trying to write a game in java3d on Linux and for that I need a proper KeyListener.
Did anyone of you know how to do it? I'm currently using following code, I found somewhere on the net. It's working pretty good, holding down just one key, but as soon, as I press more than one (like space and w) it will do unexpected things...
public class RepeatingReleasedEventsFixer implements AWTEventListener {
private final HashMap<Integer, ReleasedAction> _map = new HashMap<Integer, ReleasedAction>();
public void install() {
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
}
public void remove() {
Toolkit.getDefaultToolkit().removeAWTEventListener(this);
}
#Override
public void eventDispatched(AWTEvent event) {
assert event instanceof KeyEvent : "Shall only listen to KeyEvents, so no other events shall come here";
assert assertEDT(); // REMEMBER THAT THIS IS SINGLE THREADED, so no need for synch.
// ?: Is this one of our synthetic RELEASED events?
if (event instanceof Reposted) {
// -> Yes, so we shalln't process it again.
return;
}
// ?: KEY_TYPED event? (We're only interested in KEY_PRESSED and KEY_RELEASED).
if (event.getID() == KeyEvent.KEY_TYPED) {
// -> Yes, TYPED, don't process.
return;
}
final KeyEvent keyEvent = (KeyEvent) event;
// ?: Is this already consumed?
// (Note how events are passed on to all AWTEventListeners even though a previous one consumed it)
if (keyEvent.isConsumed()) {
return;
}
// ?: Is this RELEASED? (the problem we're trying to fix!)
if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
// -> Yes, so stick in wait
/**
* Really just wait until "immediately", as the point is that the subsequent PRESSED shall already have been
* posted on the event queue, and shall thus be the direct next event no matter which events are posted
* afterwards. The code with the ReleasedAction handles if the Timer thread actually fires the action due to
* lags, by cancelling the action itself upon the PRESSED.
*/
final Timer timer = new Timer(2, null);
ReleasedAction action = new ReleasedAction(keyEvent, timer);
timer.addActionListener(action);
timer.start();
_map.put(Integer.valueOf(keyEvent.getKeyCode()), action);
// Consume the original
keyEvent.consume();
}
else if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
// Remember that this is single threaded (EDT), so we can't have races.
ReleasedAction action = _map.remove(Integer.valueOf(keyEvent.getKeyCode()));
// ?: Do we have a corresponding RELEASED waiting?
if (action != null) {
// -> Yes, so dump it
action.cancel();
}
// System.out.println("PRESSED: [" + keyEvent + "]");
}
else {
throw new AssertionError("All IDs should be covered.");
}
}
/**
* The ActionListener that posts the RELEASED {#link RepostedKeyEvent} if the {#link Timer} times out (and hence the
* repeat-action was over).
*/
private class ReleasedAction implements ActionListener {
private final KeyEvent _originalKeyEvent;
private Timer _timer;
ReleasedAction(KeyEvent originalReleased, Timer timer) {
_timer = timer;
_originalKeyEvent = originalReleased;
}
void cancel() {
assert assertEDT();
_timer.stop();
_timer = null;
_map.remove(Integer.valueOf(_originalKeyEvent.getKeyCode()));
}
#Override
public void actionPerformed(#SuppressWarnings ("unused") ActionEvent e) {
assert assertEDT();
// ?: Are we already cancelled?
// (Judging by Timer and TimerQueue code, we can theoretically be raced to be posted onto EDT by TimerQueue,
// due to some lag, unfair scheduling)
if (_timer == null) {
// -> Yes, so don't post the new RELEASED event.
return;
}
// Stop Timer and clean.
cancel();
// Creating new KeyEvent (we've consumed the original).
KeyEvent newEvent = new RepostedKeyEvent((Component) _originalKeyEvent.getSource(),
_originalKeyEvent.getID(), _originalKeyEvent.getWhen(), _originalKeyEvent.getModifiers(),
_originalKeyEvent.getKeyCode(), _originalKeyEvent.getKeyChar(), _originalKeyEvent.getKeyLocation());
// Posting to EventQueue.
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(newEvent);
// System.out.println("Posted synthetic RELEASED [" + newEvent + "].");
}
}
/**
* Marker interface that denotes that the {#link KeyEvent} in question is reposted from some
* {#link AWTEventListener}, including this. It denotes that the event shall not be "hack processed" by this class
* again. (The problem is that it is not possible to state "inject this event from this point in the pipeline" - one
* have to inject it to the event queue directly, thus it will come through this {#link AWTEventListener} too.
*/
public interface Reposted {
// marker
}
/**
* Dead simple extension of {#link KeyEvent} that implements {#link Reposted}.
*/
public static class RepostedKeyEvent extends KeyEvent implements Reposted {
public RepostedKeyEvent(#SuppressWarnings ("hiding") Component source, #SuppressWarnings ("hiding") int id,
long when, int modifiers, int keyCode, char keyChar, int keyLocation) {
super(source, id, when, modifiers, keyCode, keyChar, keyLocation);
}
}
private static boolean assertEDT() {
if (!EventQueue.isDispatchThread()) {
throw new AssertionError("Not EDT, but [" + Thread.currentThread() + "].");
}
return true;
}
}
I can't be the only one who still runs into this - meanwhile 15 y.o. - problem and don't want to use timers...
EDIT: What this code is doing is fix the known problem on any Linux distri, where you add a simple KeyListener, which handles keyDowns, but invokes keyReleased Event repeatedly. To clearify my problem here a simple example
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Test5 extends JFrame{
public Test5() {
addKeyListener(new KeyListener() {
boolean keydown = false;
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent arg0) {
keydown = false;
System.out.println("keyup");
}
#Override
public void keyPressed(KeyEvent arg0) {
if (keydown){
System.out.println("key is down");
} else {
System.out.println("key not down");
}
keydown = true;
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
//new RepeatingReleasedEventsFixer().install(); // This line will fix it for one key pressed
}
public static void main(String[] args) {
new Test5();
}
}
The output without the line being commented out:
key not down
keyup
key not down
keyup
key not down
keyup
key not down
keyup
key not down
keyup
otherwise:
key not down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
keyup
Btw. How come, that it's not beeing fixed by now?
EDIT:
I tried the KeyBindings, as suggested, where it comes to these problems:
public class Test5 extends JFrame{
long timestamp = 0;
public Test5() {
((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('a'), "a");
((JComponent)getComponent(0)).getActionMap().put("a", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("time: "+(System.currentTimeMillis()-timestamp));
timestamp = System.currentTimeMillis();
}
});
((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('s'), "s");
((JComponent)getComponent(0)).getActionMap().put("s", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("s");
}
});
((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('d'), "d");
((JComponent)getComponent(0)).getActionMap().put("d", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("d");
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new RepeatingReleasedEventsFixer().install(); // This line will fix it for one key pressed
}
/**
* #param args
*/
public static void main(String[] args) {
new Test5();
}
Holding down "a" will give me following output:
time: 4171
time: 501
time: 30
time: 30
time: 30
Where the second time is the actual problem. It takes about 470ms too long.
Holding down "s" and then somewhne pressing "d" will give me that output:
s
s
s
s
d
d
d
d
d
So I can't process two actions as the same time, so I can't use KeyBindings
This is not an answer, it is a long comment with a picture and some explanations.
I used your Test5 (without RepeatingReleasedEventsFixer) to hold down a and measure the time responses. The output is of the form
time: t1
time: t2
time: t3
time: t3
time: t3
...
t1 is meaningless since it depends on the current time and has nothing to do with response time (you also seem to ignore it).
t2 is the time it takes for the OS to realize that you're holding the key for repeated input.
t3 is the "sample time" of the held key, or a discretization of the input.
I'm using Windows where I have the following control panel options:
Repeat delay allows me to set t2 between ~257 (short) and ~1050 (long).
Repeat rate allows me to set t3 between ~407 (slow) and ~37 (fast).
For Linux, you'll have to consult someone / somewhere on how to change these values if you don't already know how to.
As for using multiple keys, see this question and answer and the excellent link within (especially the "Motion With Multiple Keys Pressed" section). It's a short tutorial and analysis of key bindings and key listeners, similar to the one I sent you to on this site.
Key bindings will always be preferred over key listeners unless maybe there is some very low level thing you want to do.
After days of researching and putting stuff together, I ended up writing my own Listener combined with a KeyEventDispatcher, here is the code for someone running into the same problem. It can and should be optimized, but is working for now:
Klass to test if a specific key is pressed:
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.HashMap;
public class IsKeyPressed {
private static boolean wPressed = false;
private HashMap<Integer, Boolean> keys = new HashMap<Integer, Boolean>();
public IsKeyPressed() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (IsKeyPressed.class) {
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
keys.put(ke.getKeyCode(), true);
break;
case KeyEvent.KEY_RELEASED:
keys.put(ke.getKeyCode(), false);
break;
}
return false;
}
}
});
}
public static boolean isWPressed() {
synchronized (IsKeyPressed.class) {
return wPressed;
}
}
public boolean isPressed(int keyCode){
synchronized (IsKeyPressed.class) {
if (keys == null)
return false;
if (keys.get(keyCode) == null)
return false;
return keys.get(keyCode);
}
}
}
Abstract class, thats beeing used for the actions.
public abstract class KeyActionListener {
protected int keyCode;
public KeyActionListener(int keyCode) {
this.keyCode = keyCode;
}
public void setKeyCode(int keyCode){
this.keyCode = keyCode;
}
public int getKeyCode(){
return this.keyCode;
}
public abstract void onKeyDown();
public abstract void onKeyUp();
public abstract void onKeyHolding();
}
Start listening to the keys and run the actions.
import java.util.ArrayList;
import java.util.HashMap;
public class KeyThread extends Thread{
private int sleep = 3;
ArrayList<KeyActionListener> listener = new ArrayList<KeyActionListener>();
IsKeyPressed isPressed = new IsKeyPressed();
HashMap<KeyActionListener, Boolean> pressed = new HashMap<KeyActionListener, Boolean>();
public KeyThread() {
this.start();
}
public void run() {
while (true){
for (int i = 0; i < listener.size(); i++) {
KeyActionListener curListener = listener.get(i);
if (isPressed.isPressed(curListener.getKeyCode()) && !pressed.get(curListener)){
curListener.onKeyDown();
pressed.put(curListener, true);
} else if(!isPressed.isPressed(curListener.getKeyCode()) && pressed.get(curListener)) {
curListener.onKeyUp();
pressed.put(curListener, false);
}
if(isPressed.isPressed(curListener.getKeyCode())){
curListener.onKeyHolding();
}
try{
Thread.sleep(sleep);
} catch(InterruptedException e){
}
}
}
}
public void addKeyActionListener(KeyActionListener l){
listener.add(l);
pressed.put(l, false);
}
}

Problems with single-tap and press-and-hold with LWJGL

I've been researching a way to use LWJGL for my input system. I'm having problems detecting if it is a single-press or a press-and-hold. The event fires twice when I tap, instead of just once.
while(Keyboard.next())
{
if(Keyboard.getEventKeyState())
{
if(Keyboard.isRepeatEvent())
{
//Key held.
doAction(Keyboard.getEventKey(), true, false);
}
else
{
//Key pressed
doAction(Keyboard.getEventKey(), false, false);
}
}
else
{
//Fired when key is released.
doAction(Keyboard.getEventKey(), false, true);
}
}
Edit: I've both resolved the issue and modified this. Here you go, a modified version. (Dammit, Teamviewer..)
/**
* Updates all mouse info, keys bound, and performs actions.
*/
public static void tick()
{
mouseButtons[0] = Mouse.isButtonDown(0);
mouseButtons[1] = Mouse.isButtonDown(1);
mousePos[0] = Mouse.getX();
mousePos[1] = Mouse.getY();
while(Keyboard.next())
{
doAction(0, false);
if(Keyboard.getEventKeyState())
{
if(!Keyboard.isRepeatEvent())
{
doAction(Keyboard.getEventKey(), false);
}
}
else
{
doAction(Keyboard.getEventKey(), true);
}
}
while(Mouse.next())
{
}
}
/**
* Does the associated action for each key. Called automatically from tick.
* #param key The key to check & perform associated action
*/
public static void doAction(int key, boolean ifReleased)
{
if(mouseButtons[0])
{
}
if(mouseButtons[1])
{
}
if(key == 2 & !ifReleased)
{
System.out.println("a");
}
if(Keyboard.isKeyDown(3))
{
System.out.println("b");
}
}
I know it's been awhile since this question was asked, but I came up with a solution myself. My InputHelper lets you determine if a key or mouse button is pressed, released, or held down, and can be accessed from anyother class without initializing and sharing the same instance of it.
It works by having 2 arrays, 1 for mouse events, 1 for keyboard events, that each store one enum value for each key. If there is a button or key event, when updated, the update function sets the value in the appropriate array for that button/key to a certain enum. Then, the next time it is updated, it sets all the key and button events to no event, and repeats the process, handling any new events.
/*
* Handles mouse and keyboard input and stores values for keys
* down, released, or pressed, that can be accessed from anywhere.
*
* To update the input helper, add this line into the main draw loop:
* InputHelper.update();
*
* Use as so (can be used from anywhere):
* InputHelper.isKeyDown(Keyboard.KEY_SPACE);
*/
import java.util.ArrayList;
import org.lwjgl.input.*;
/**
*
* #author Jocopa3
*/
public class InputHelper {
private static InputHelper input = new InputHelper(); //Singleton class instance
private enum EventState {
NONE,PRESSED,DOWN,RELEASED;
}
private ArrayList<EventState> mouseEvents;
private ArrayList<EventState> keyboardEvents;
public InputHelper(){
//Mouse initialization
mouseEvents = new ArrayList<EventState>();
//Add mouse events to Array list
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.add(EventState.NONE);
}
//Keyboard initialization
keyboardEvents = new ArrayList<EventState>();
//Add keyboard events to Array list
for(int i = 0; i < Keyboard.KEYBOARD_SIZE; i++) {
keyboardEvents.add(EventState.NONE);
}
}
private void Update(){
resetKeys(); //clear Keyboard events
//Set Key down events (more accurate than using repeat-event method)
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++){
if(Keyboard.isKeyDown(i))
keyboardEvents.set(i, EventState.DOWN);
}
while(Keyboard.next()){ //Handle all Keyboard events
int key = Keyboard.getEventKey();
if(key<0) continue; //Ignore no events
if(Keyboard.getEventKeyState()){
if(!Keyboard.isRepeatEvent()){
keyboardEvents.set(key, EventState.PRESSED);
}
}else{
keyboardEvents.set(key, EventState.RELEASED);
}
}
resetMouse(); //clear Mouse events
//Set Mouse down events
for(int i = 0; i < Mouse.getButtonCount(); i++){
if(Mouse.isButtonDown(i))
mouseEvents.set(i, EventState.DOWN);
}
while (Mouse.next()){ //Handle all Mouse events
int button = Mouse.getEventButton();
if(button<0) continue; //Ignore no events
if (Mouse.getEventButtonState()) {
mouseEvents.set(button, EventState.PRESSED);
}else {
mouseEvents.set(button, EventState.RELEASED);
}
}
}
//Set all Keyboard events to false
private void resetKeys(){
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++) {
keyboardEvents.set(i, EventState.NONE);
}
}
//Set all Mouse events to false
private void resetMouse(){
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.set(i, EventState.NONE);
}
}
//Non-static version of methods (Only used in the singleton instance)
private boolean KeyDown(int key){
return keyboardEvents.get(key)==EventState.DOWN;
}
private boolean KeyPressed(int key){
return keyboardEvents.get(key)==EventState.PRESSED;
}
private boolean KeyReleased(int key){
return keyboardEvents.get(key)==EventState.RELEASED;
}
private boolean MouseButtonDown(int key){
return mouseEvents.get(key)==EventState.DOWN;
}
private boolean MouseButtonPressed(int key){
return mouseEvents.get(key)==EventState.PRESSED;
}
private boolean MouseButtonReleased(int key){
return mouseEvents.get(key)==EventState.RELEASED;
}
//Static version of methods (called from anywhere, return singleton instance value)
public static boolean isKeyDown(int key){
return input.KeyDown(key);
}
public static boolean isKeyPressed(int key){
return input.KeyPressed(key);
}
public static boolean isKeyReleased(int key){
return input.KeyReleased(key);
}
public static boolean isButtonDown(int key){
return input.MouseButtonDown(key);
}
public static boolean isButtonPressed(int key){
return input.MouseButtonPressed(key);
}
public static boolean isButtonReleased(int key){
return input.MouseButtonReleased(key);
}
public static void update(){
input.Update();
}
}
It has to be updated every frame manually, so your main draw loop you should add the line InputHelper.update(); like this:
while(!Display.isCloseRequested()) {
InputHelper.update(); //Should go before other code that uses the inputs
//Rest of code here
}
Once you have it setup to update every frame, you can use it anywhere you need to determine input states for Mouse or Key buttons as so:
//Mouse test
if(InputHelper.isButtonPressed(0))
System.out.println("Left Mouse button pressed");
if(InputHelper.isButtonDown(0))
System.out.println("Left Mouse button down");
if(InputHelper.isButtonReleased(0))
System.out.println("Left Mouse button released");
//Keyboard Test
if(InputHelper.isKeyPressed(Keyboard.KEY_SPACE))
System.out.println("Space key pressed");
if(InputHelper.isKeyDown(Keyboard.KEY_SPACE))
System.out.println("Space key down");
if(InputHelper.isKeyReleased(Keyboard.KEY_SPACE))
System.out.println("Space key released");

How to Make a Stopwatch for a quiz system using java

I'm really stuck, thanks to my college.
I need code in Java to have a Stopwatch which shows time in 00:00:00(mm:ss:msms) format. I want to use Key events to run and pause and reset the timer. Like if I press S the stopwatch starts and P pauses and R resets.
the thing is the I also want to add key events on numbers for teams, like if I press 1, the "team 1" flashes, preferably with a beep, and so on with 2 3 4 5. im not able to understand how to do this.
i wrote this to print time in second only just to try...
import java.awt.event.*;
import javax.swing.*;
public class StopWatch2 extends JLabel
implements KeyListener, ActionListener {
private long startTime;
private boolean running;
private Timer timer;
public StopWatch2() {
super(" Press S ", JLabel.CENTER);
addKeyListener(this);
}
public void actionPerformed(ActionEvent evt) {
long time = (System.currentTimeMillis() - startTime) / 1000;
setText(Long.toString(time));
}
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_S) {
running = true;
startTime = e.getWhen();
setText("Running: 0 seconds");
if (timer == null) {
timer = new Timer(100,this);
timer.start();
}
else
timer.restart();
}
if(keyCode==KeyEvent.VK_P)
{
timer.stop();
running = false;
long endTime = e.getWhen();
double seconds = (endTime - startTime) / 1000.0;
setText("Time: " + seconds + " sec.");
}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
}
import java.awt.*;
import javax.swing.*;
public class Test2 extends JApplet {
public void init() {
StopWatch2 watch = new StopWatch2();
watch.setFont( new Font("SansSerif", Font.BOLD, 24) );
watch.setBackground(Color.white);
watch.setForeground( new Color(180,0,0) );
watch.setOpaque(true);
getContentPane().add(watch, BorderLayout.CENTER);
}
}
im trying stuff on my own n m pretty much self taught so im not able to understand whats going wrong
Do you mean something like:
/**
* Stopwatch is a simple timer.
*/
public class Stopwatch {
/**
* Stopwatch() Initialises a stopwatch.
*/
public Stopwatch() {
// Your code here.
}
/**
* elapsed() The elapsed time in milliseconds shown on the stopwatch.
*
* #return double The elapsed time in milliseconds as a double. Returns -1.0 if no meaningful
* value is available, i.e. if the watch is reset or has been started and not stopped.
*/
public double elapsed() {
// Your code here.
}
/**
* start() Starts the stopwatch and clears the previous elapsed time.
*/
public void start() {
// Your code here.
}
/**
* stop() If the stopwatch has been started this stops the stopwatch and calculates the
* elapsed time. Otherwise it does nothing.
*/
public void stop() {
// Your code here.
}
/**
* reset() Resets the stopwatch and clears the elapsed time.
*/
public void reset() {
// Your code here.
}
#Override
public String toString() {
// Your code here.
}
} // end class Stopwatch

Categories