Commit 9a22fc37 authored by jackw's avatar jackw

New

parent 32034e88
...@@ -56,3 +56,5 @@ class ComplexTest { ...@@ -56,3 +56,5 @@ class ComplexTest {
assertEquals("1.2 - j3.4", d.toString()); assertEquals("1.2 - j3.4", d.toString());
} }
} }
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class NoughtsAndCrosses extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage s) throws Exception {
Pane root = new Pane();
Label label = new Label("Left Click For O, Right Click for X");
label.setLayoutX(220);
label.setLayoutY(40);
// Top Row
Button a = new Button();
a.setLayoutX(0);
a.setLayoutY(100);
a.setPrefSize(200, 200);
Button b = new Button();
b.setLayoutX(200);
b.setLayoutY(100);
b.setPrefSize(200, 200);
Button c = new Button();
c.setLayoutX(400);
c.setLayoutY(100);
c.setPrefSize(200, 200);
// Middle Row
Button d = new Button();
d.setLayoutX(0);
d.setLayoutY(300);
d.setPrefSize(200, 200);
Button ee = new Button();
ee.setLayoutX(200);
ee.setLayoutY(300);
ee.setPrefSize(200, 200);
Button f = new Button();
f.setLayoutX(400);
f.setLayoutY(300);
f.setPrefSize(200, 200);
// Bottom Row
Button g = new Button();
g.setPrefSize(200, 200);
g.setLayoutX(0);
g.setLayoutY(500);
Button h = new Button();
h.setLayoutX(200);
h.setLayoutY(500);
h.setPrefSize(200, 200);
Button i = new Button();
i.setLayoutX(400);
i.setLayoutY(500);
i.setPrefSize(200, 200);
// Game Buttons
Button Exit = new Button();
Exit.setLayoutX(450);
Exit.setLayoutY(25);
Exit.setPrefSize(100, 50);
Exit.setText("Exit");
Button New = new Button();
New.setLayoutX(50);
New.setLayoutY(25);
New.setPrefSize(100, 50);
New.setText("New Game");
a.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
a.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
a.setText("O");
}
}
});
b.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
b.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
b.setText("O");
}
}
});
c.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
c.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
c.setText("O");
}
}
});
d.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
d.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
d.setText("O");
}
}
});
ee.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
ee.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
ee.setText("O");
}
}
});
f.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
f.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
f.setText("O");
}
}
});
g.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
g.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
g.setText("O");
}
}
});
h.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
h.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
h.setText("O");
}
}
});
i.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
i.setText("X");
}
else
if(event.getButton() == MouseButton.PRIMARY) {
i.setText("O");
}
}
});
Exit.setOnAction(e -> Platform.exit());
New.setOnAction( __ ->
{
s.close();
s.setScene( new Scene( new BorderPane( root ) ) );
s.show();
} );
root.getChildren().addAll(a, b, c, d, ee, f, g, h, i, Exit, New, label);
s.setScene(new Scene(root, 600, 700));
s.setTitle("Noughts And Crosses");
s.show();
}
}
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.LineUnavailableException;
import java.util.*;
/** Class to play audio generated in real time */
public class Play {
/** sample rate in Hz */
private final int sampleRate = 44100;
private SourceDataLine line;
private double time;
private double get() {
return Math . sin (2 * Math .PI * 440 * time );
}
private void tick() {
time = 1.0 / sampleRate ;
}
public Play() throws LineUnavailableException {
/* Set up to play audio */
AudioFormat format = new AudioFormat((float) sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, 1024 * 2);
}
/** Create audio for a given time.
* @param seconds Number of seconds to run for.
*/
public void run(int seconds) {
byte[] buffer = new byte[1024 * 2];
line.start();
/* Run a loop to collect audio samples and give them to the audio interface */
int b = 0;
for (int i = 0; i < sampleRate * seconds; ++i) {
/* We scale samples down to a quarter of full scale */
short s = (short) (get() * 32768 / 4);
buffer[b] = (byte) s;
buffer[b + 1] = (byte) (s >> 8);
b += 2;
if (b >= buffer.length) {
line.write(buffer, 0, buffer.length);
b = 0;
}
tick();
}
}
/** Tidy up */
public void close() {
line.drain();
line.stop();
}
public static void main(String[] args) throws LineUnavailableException {
Play p = new Play();
p.run(5);
p.close();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment