Changing to network
This commit is contained in:
@@ -2,6 +2,10 @@ package control;
|
|||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
|
|
||||||
@@ -26,20 +30,8 @@ public class GameControl implements JoystickListener, ButtonListener, ActionList
|
|||||||
this.view = view;
|
this.view = view;
|
||||||
this.gsm = gsm;
|
this.gsm = gsm;
|
||||||
view.setIgnoreRepaint(true);
|
view.setIgnoreRepaint(true);
|
||||||
update = new Timer(0,this);
|
update = new Timer(1000/60,this);
|
||||||
update.start();
|
update.start();
|
||||||
// Timer update = new Timer();
|
|
||||||
// update.schedule(new TimerTask() {
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void run() {
|
|
||||||
// long currentTime = System.currentTimeMillis();
|
|
||||||
// model.update(currentTime - lastTime);
|
|
||||||
// lastTime = currentTime;
|
|
||||||
// view.repaint();
|
|
||||||
// System.out.println("Test");
|
|
||||||
// }
|
|
||||||
// }, 0,1000/120);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package control;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import control.button.ButtonHandler;
|
||||||
|
import control.joystick.JoystickHandler;
|
||||||
|
|
||||||
|
public class NetworkHandler implements Runnable{
|
||||||
|
|
||||||
|
DatagramSocket udp;
|
||||||
|
|
||||||
|
String host;
|
||||||
|
int port;
|
||||||
|
|
||||||
|
boolean running;
|
||||||
|
Thread t;
|
||||||
|
|
||||||
|
byte[] send;
|
||||||
|
byte[] receive;
|
||||||
|
|
||||||
|
ButtonHandler bth;
|
||||||
|
JoystickHandler jth;
|
||||||
|
|
||||||
|
public NetworkHandler(String host, int port, ButtonHandler bth, JoystickHandler jth)
|
||||||
|
{
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
|
||||||
|
this.bth = bth;
|
||||||
|
this.jth = jth;
|
||||||
|
|
||||||
|
udp = null;
|
||||||
|
|
||||||
|
send = new byte[1024];
|
||||||
|
receive = new byte[1024];
|
||||||
|
|
||||||
|
try {
|
||||||
|
udp = new DatagramSocket();
|
||||||
|
udp.connect(InetAddress.getByName(host), port);
|
||||||
|
} catch (SocketException | UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
t = new Thread(this);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLed(int led, int r, int g , int b)
|
||||||
|
{
|
||||||
|
String cmd = "1|" + led + "|" + r + "|" + g + "|" + b + "\n";
|
||||||
|
send(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show(){
|
||||||
|
String cmd = "0\n";
|
||||||
|
send(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void send(String str)
|
||||||
|
{
|
||||||
|
send = str.getBytes();
|
||||||
|
try {
|
||||||
|
udp.send(new DatagramPacket(send, send.length));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
udp.disconnect();
|
||||||
|
udp.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while(running)
|
||||||
|
{
|
||||||
|
DatagramPacket pckg = new DatagramPacket(receive, receive.length);
|
||||||
|
try {
|
||||||
|
udp.receive(pckg);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = new String(pckg.getData());
|
||||||
|
System.out.println(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-2
@@ -18,6 +18,7 @@ import view.GameView;
|
|||||||
import control.GameControl;
|
import control.GameControl;
|
||||||
import control.GameStateManager;
|
import control.GameStateManager;
|
||||||
import control.LedHandler;
|
import control.LedHandler;
|
||||||
|
import control.NetworkHandler;
|
||||||
import control.button.ButtonHandler;
|
import control.button.ButtonHandler;
|
||||||
import control.joystick.JoystickHandler;
|
import control.joystick.JoystickHandler;
|
||||||
|
|
||||||
@@ -35,9 +36,8 @@ public class Window extends JFrame {
|
|||||||
setSize(WIDTH, HEIGHT);
|
setSize(WIDTH, HEIGHT);
|
||||||
|
|
||||||
//Create Events
|
//Create Events
|
||||||
LedHandler led = null;
|
|
||||||
Window.ON_RASP = ON_RASP;
|
Window.ON_RASP = ON_RASP;
|
||||||
|
LedHandler led = null;
|
||||||
if(ON_RASP){ //Only on the raspberry pi
|
if(ON_RASP){ //Only on the raspberry pi
|
||||||
led = new LedHandler();
|
led = new LedHandler();
|
||||||
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
@@ -57,6 +57,8 @@ public class Window extends JFrame {
|
|||||||
ButtonHandler bth = new ButtonHandler(led);
|
ButtonHandler bth = new ButtonHandler(led);
|
||||||
JoystickHandler jsh = new JoystickHandler();
|
JoystickHandler jsh = new JoystickHandler();
|
||||||
|
|
||||||
|
NetworkHandler ntw = new NetworkHandler("192.168.1.6", 1113, bth, jsh);
|
||||||
|
|
||||||
//Create Instances
|
//Create Instances
|
||||||
final SongHandler sh = new SongHandler();
|
final SongHandler sh = new SongHandler();
|
||||||
GameStateManager gsm = new GameStateManager(sh);
|
GameStateManager gsm = new GameStateManager(sh);
|
||||||
|
|||||||
Reference in New Issue
Block a user