d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Quick Programming Help
Add Reply New Topic New Poll
Member
Posts: 15,917
Joined: Sep 14 2009
Gold: 0.00
Dec 28 2016 11:32pm
So I want to write or have written for me what i assume is a simple program.

I need it to display random letters q,w,e,a,s,d,z,x,c, wait until said letter pressed, then move on the next random letter on a loop until something like ESC is pressed. While all thats going on id like it to time it and display the total number of keys pressed?

how hard is this to program, and what hopefully freeware is available to do so?
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Dec 29 2016 05:10am
this would not be hard at all in java. You could just pick a letter at random and have an event listener for keystrokes. If the key pressed matches the letter then increment the count and refresh the letter.

As far as freeware, I don't know.
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Dec 31 2016 01:42am
very easy to code but your instructions are a little vague
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jan 1 2017 07:47am
Alright so... I think I did what you asked for... I have no idea really though lol
Source bellow followed by screenshots and download link for .jar if you just want the executable (will need java of course, saved it so that if you have a decent ide you can very easily load it as a package and view source code)
e. if i actually did what you wanted you'll have to share the purpose of this program lol
editedit... now that i think about it some nice summary average time, high low info is all pretty easy... but well i'm bored and i already posted it

Code
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* Created by user on 1/1/2017.
* stupid little hotkey timer programI have no idea the purpose of
*/

public class Main extends Application {
final private char[] chars = {'Q', 'W', 'E', 'A', 'S', 'D', 'Z', 'X', 'C'};

private Stage window;
private Scene scene;
private Map<Character, something> myMap;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("JSP Request");
window.setOnCloseRequest(e -> {
e.consume();
closeProgram();
});
myMap = new HashMap<>();
for (char x : chars) {
myMap.put(x, new something(x));
}

paneMaker();

window.show();
}

private class formattedLabel extends Label {
formattedLabel(String s) {
setText(s);
setFont(new Font("Monospaced Regular", 12));
}
}

private void newMethodIhaveYetToName() {
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
int x = 0, y = 0;
for (char c : chars) {
VBox box = new VBox();

box.getChildren().add(new formattedLabel("Letter: " + Character.toString(c)));
box.getChildren().add(new formattedLabel("Count: " + Integer.toString(myMap.get(c).count)));
box.getChildren().add(new formattedLabel("ErrorCount: " + Integer.toString(myMap.get(c).errorCount)));
box.getChildren().add(new formattedLabel("Times: "));
for (String s : myMap.get(c).times) {
box.getChildren().add(new formattedLabel(" " + s));
}
gridPane.add(box, x, y);
if (x == 2) {
y++;
x = 0;
} else x++;
}
Button button = new Button("play again");
button.setOnAction(e-> paneMaker());

gridPane.add(button, 3, 3);
scene = new Scene(gridPane);
window.setScene(scene);

}


private void closeProgram() {
window.close();
}

private void paneMaker() {
VBox labelPane = new VBox();
labelPane.setAlignment(Pos.CENTER);

labelPane.getChildren().add(new Label("Enter The following key"));

char answer = chars[(int) (Math.random() * chars.length)];
Label label = new Label((Character.toString(answer)));
label.setTextFill(Color.color(0, Math.random(), Math.random()));
labelPane.getChildren().add(label);

Button exitButton = new Button("End (Press Escape or Enter)");
exitButton.setOnAction(e -> newMethodIhaveYetToName());
labelPane.getChildren().add(exitButton);

FXTimer timer = new FXTimer();
labelPane.getChildren().add(timer);

labelPane.setAlignment(Pos.CENTER);

scene = new Scene(labelPane, 300, 250);
scene.setOnKeyPressed(event -> {
if (event.getCode().toString().equalsIgnoreCase(Character.toString(answer))) {
myMap.get(answer).count++;
myMap.get(answer).times.add(timer.timerLabel.getText()+"s");
paneMaker();
} else if (event.getCode().equals(KeyCode.ESCAPE) || event.getCode().equals(KeyCode.ENTER)) {
newMethodIhaveYetToName();
} else {
myMap.get(answer).errorCount++;
}
});
window.setScene(scene);
}

private class something {
char charCode;
int count;
int errorCount;
ArrayList<String> times;

something(char x) {
charCode = x;
count = 0;
errorCount = 0;
times = new ArrayList<>();
}
}

private class FXTimer extends StackPane {

private Timeline timeline;
Label timerLabel = new Label();
private DoubleProperty splitTimeSeconds = new SimpleDoubleProperty();
private Duration time = Duration.ZERO, splitTime = Duration.ZERO;

FXTimer() {
timerLabel.textProperty().bind(splitTimeSeconds.asString());
timerLabel.setTextFill(Color.BLUE);
timerLabel.setStyle("-fx-font-size: 2em;");
if (timeline != null) {
splitTime = Duration.ZERO;
splitTimeSeconds.set(splitTime.toSeconds());
} else {
timeline = new Timeline(
new KeyFrame(Duration.millis(100),
t -> {
Duration duration = ((KeyFrame) t.getSource()).getTime();
time = time.add(duration);
splitTime = splitTime.add(duration);
splitTimeSeconds.set(splitTime.toSeconds());
})
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}

VBox vb = new VBox(20);
vb.setAlignment(Pos.CENTER);
vb.setLayoutY(30);
vb.getChildren().add(timerLabel);
getChildren().add(vb);
}
}
}





Download Link
http://webservices.missouriwestern.edu/users/dnelson12/asdf/jsprequest2.jar

This post was edited by Ideophobe on Jan 1 2017 07:54am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll