Forráskód Böngészése

Added working buttons stuff

Kenneth van Ewijk 10 éve
szülő
commit
564f10af62

+ 37 - 0
src/control/GameControl.java

@@ -0,0 +1,37 @@
+package control;
+
+import model.GameModel;
+import view.GameView;
+import control.button.ButtonEvent;
+import control.button.ButtonListener;
+import control.joystick.JoystickEvent;
+import control.joystick.JoystickListener;
+
+public class GameControl implements JoystickListener, ButtonListener{
+	
+	GameModel model;
+	GameView view;
+	
+	public GameControl(GameModel model, GameView view)
+	{
+		this.model = model;
+		this.view = view;
+	}
+
+	@Override
+	public void buttonPressed(ButtonEvent e) {
+		view.setColor(e.getButton().getColor());
+	}
+
+	@Override
+	public void buttonReleased(ButtonEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void onJoyStickMoved(JoystickEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+}

+ 100 - 0
src/control/LedHandler.java

@@ -0,0 +1,100 @@
+package control;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+
+public class LedHandler {
+    private BufferedReader inp;
+    private BufferedWriter out;
+    private Process p;
+    
+    public LedHandler(){
+	    try {
+	        p = Runtime.getRuntime().exec("sudo python led.py");
+	        inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
+	        out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
+	        
+	        //setLed(15, 100, 100, 100);	        
+	        //strobo();        
+	    }
+	    catch (Exception err) {
+	        err.printStackTrace();
+	    }
+    }
+    
+    public void close(){
+        try {
+			inp.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+        try {
+			out.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+        p.destroy();
+    }
+    
+    
+    /**
+     * Set the color of a specific led
+     * @param Pixel number of the led
+     * @param Red value
+     * @param Green value
+     * @param Blue value
+     * Remember: Button leds are GRB, normal ledstrip is RGB
+     */
+    public void setLed(int led, int r, int g, int b) {
+	    if(led==-1)
+	    {
+	    	return;
+	    }
+    	try {
+    		System.out.println("Set the led with " + r);
+	        out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" );
+	        out.flush();
+	    }
+	    catch (Exception err) {
+	    	err.printStackTrace();
+	    }
+    }
+
+    /**
+     * Shows the (new) colors 
+     */
+    public void show(){
+	    try {
+	        out.write( "0\n" );
+	        out.flush();
+	    }
+	    catch (Exception err) {
+	    	err.printStackTrace();
+	    }
+    }
+    
+    public void strobo(){
+    	boolean on = true;
+    	while(true){
+    		if(on){
+				for(int i = 1; i < 66; i++){
+					setLed(i, 0, 0, 0);
+				}
+    		}else{
+				for(int i = 1; i < 66; i++){
+					setLed(i, 255, 255, 255);
+				}
+    		}
+    		on = !on;
+    		show();
+    		try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+    	}
+    }
+}

+ 44 - 0
src/control/button/Button.java

@@ -0,0 +1,44 @@
+package control.button;
+
+import java.awt.Color;
+
+import control.LedHandler;
+
+public class Button {
+
+	Color color;
+	int ledID;
+	int buttonID;
+	LedHandler led;
+	
+	public Button(int buttonID, int ledID, LedHandler led)
+	{
+		color = new Color(255,255,255);
+		this.ledID = ledID;
+		this.buttonID = buttonID;
+		this.led = led;
+		setLed();
+	}
+	
+	private void setLed()
+	{
+		led.setLed(ledID, color.getGreen(), color.getRed(), color.getBlue());
+		led.show();
+	}
+	
+	public void setColor(Color newColor)
+	{
+		color = newColor;
+		setLed();
+	}
+	
+	public Color getColor(){
+		return color;
+	}
+	public int getButtonID()
+	{
+		return buttonID;
+	}
+	
+	
+}

+ 19 - 0
src/control/button/ButtonEvent.java

@@ -0,0 +1,19 @@
+package control.button;
+
+public class ButtonEvent {
+	private Button button;
+	private Long now;
+	
+	public ButtonEvent(Button button, Long now){
+		this.button = button;
+		this.now = now;
+	}
+
+	public Button getButton() {
+		return button;
+	}
+
+	public Long getNow() {
+		return now;
+	}		
+}

+ 84 - 0
src/control/button/ButtonHandler.java

@@ -0,0 +1,84 @@
+package control.button;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.util.ArrayList;
+import java.util.List;
+
+import control.LedHandler;
+
+public class ButtonHandler implements KeyListener{
+
+	List<ButtonListener> listeners;
+	static List<Button> buttons;
+	LedHandler led;
+	
+	public ButtonHandler(LedHandler led)
+	{
+		this.led = led;
+		
+		listeners = new ArrayList<ButtonListener>();
+		buttons = new ArrayList<Button>();
+		
+		buttons.add(new Button(0, -1, led));
+		buttons.add(new Button(1, 5, led)); //TODO change to real ID's;
+		buttons.add(new Button(2, 4, led));
+		buttons.add(new Button(3, 3, led));
+		buttons.add(new Button(4, 2, led));
+		buttons.add(new Button(5, 1, led));
+		buttons.add(new Button(6, 0, led));
+	}
+
+	public void addButtonListener(ButtonListener toAdd) {
+	    listeners.add(toAdd);
+	}
+	
+	public void buttonPress(Button b) {
+		ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
+	    for (ButtonListener bt : listeners)
+	        bt.buttonPressed(e);
+	}
+	 
+	public void buttonRelease(Button b) {
+		ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
+	    for (ButtonListener bt : listeners)
+	        bt.buttonReleased(e);
+	}
+
+	
+	@Override
+	public void keyPressed(KeyEvent e) {
+		switch(e.getKeyCode())
+		{
+		case KeyEvent.VK_0:
+			buttonPress(buttons.get(0));
+			break;
+		case KeyEvent.VK_1:
+			buttonPress(buttons.get(1));
+			break;
+		case KeyEvent.VK_2:
+			buttonPress(buttons.get(2));
+			break;
+		case KeyEvent.VK_3:
+			buttonPress(buttons.get(3));
+			break;
+		case KeyEvent.VK_4:
+			buttonPress(buttons.get(4));
+			break;
+		case KeyEvent.VK_5:
+			buttonPress(buttons.get(5));
+			break;
+		case KeyEvent.VK_6:
+			buttonPress(buttons.get(6));
+			break;
+		}
+	}
+
+	public void keyReleased(KeyEvent arg0) {}
+	public void keyTyped(KeyEvent arg0) {}
+	
+	public static List<Button> getButtons()
+	{
+		return buttons;
+	}
+}

+ 6 - 0
src/control/button/ButtonListener.java

@@ -0,0 +1,6 @@
+package control.button;
+
+public interface ButtonListener {
+	public void buttonPressed(ButtonEvent e);
+	public void buttonReleased(ButtonEvent e);
+}

+ 8 - 0
src/control/joystick/Joystick.java

@@ -0,0 +1,8 @@
+package control.joystick;
+
+public class Joystick {
+	
+	public Joystick()
+	{
+	}
+}

+ 19 - 0
src/control/joystick/JoystickEvent.java

@@ -0,0 +1,19 @@
+package control.joystick;
+
+public class JoystickEvent {
+	private Joystick joystick;
+	private Long now;
+	
+	public JoystickEvent(Joystick joystick, Long now){
+		this.joystick = joystick;
+		this.now = now;
+	}
+
+	public Joystick getJoystick() {
+		return joystick;
+	}
+
+	public Long getNow() {
+		return now;
+	}	
+}

+ 48 - 0
src/control/joystick/JoystickHandler.java

@@ -0,0 +1,48 @@
+package control.joystick;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.util.ArrayList;
+import java.util.List;
+
+import control.LedHandler;
+
+public class JoystickHandler implements KeyListener{
+	
+	List<JoystickListener> listeners;
+	LedHandler led;
+	
+	public JoystickHandler(LedHandler led)
+	{
+		this.led = led;
+		listeners = new ArrayList<JoystickListener>();
+	}
+
+	public void addJoyStickListener(JoystickListener toAdd) {
+	    listeners.add(toAdd);
+	}
+	
+	public void onJoyStickMoved() {
+		JoystickEvent e = new JoystickEvent(new Joystick(), 1000L); //TODO edit
+	    for (JoystickListener yst : listeners)
+	        yst.onJoyStickMoved(e);
+	}
+
+	@Override
+	public void keyPressed(KeyEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void keyReleased(KeyEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void keyTyped(KeyEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+}

+ 5 - 0
src/control/joystick/JoystickListener.java

@@ -0,0 +1,5 @@
+package control.joystick;
+
+public interface JoystickListener {
+	public void onJoyStickMoved(JoystickEvent e);
+}

+ 1 - 4
src/main/Main.java

@@ -1,12 +1,9 @@
 package main;
 
-import control.Buttontest;
-
 public class Main {
 
 	public static void main(String[] args) {
-		//	new Window();
-		new Buttontest();
+		new Window();
 	}
 
 }

+ 28 - 5
src/main/Window.java

@@ -5,14 +5,19 @@ import java.awt.event.WindowEvent;
 
 import javax.swing.JFrame;
 
-import view.Panel;
+import model.GameModel;
+import view.GameView;
+import control.GameControl;
+import control.LedHandler;
+import control.button.ButtonHandler;
+import control.joystick.JoystickHandler;
 
 public class Window extends JFrame {
 	public Window()
 	{
 		//Create window
-		super("Arcade-controls");
-		setSize(500,600);
+		super("Arcade");
+		setSize(1280, 1024);
 		
 		//Set window close listener
 		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@@ -22,11 +27,29 @@ public class Window extends JFrame {
 			}
 		});
 		
+		//Set window to fullscreen
+		setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
+		setUndecorated(true);
+		
+		//Create Events
+		LedHandler led = new LedHandler();
+		ButtonHandler bth = new ButtonHandler(led);
+		JoystickHandler jsh = new JoystickHandler(led);
+		
 		//Create Instances
-		Panel panel = new Panel();
-		setContentPane(panel);
+		GameView view = new GameView(led);
+		GameModel model = new GameModel(view);
+		GameControl control = new GameControl(model, view);
+		setContentPane(view);
+		
+		//Create EventListeners
+		addKeyListener(bth);
+		addKeyListener(jsh);
+		bth.addButtonListener(control);
+		jsh.addJoyStickListener(control);
 		
 		//Display
+		pack();
 		setVisible(true);
 	}
 }

+ 32 - 0
src/model/GameModel.java

@@ -0,0 +1,32 @@
+package model;
+
+import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.Timer;
+
+import view.GameView;
+import control.button.Button;
+import control.button.ButtonHandler;
+
+public class GameModel implements ActionListener{
+	
+	GameView view;
+	Timer t;
+	
+	public GameModel(GameView view)
+	{
+		this.view = view;
+		t = new Timer(2000, this);
+		t.start();
+	}
+
+	@Override
+	public void actionPerformed(ActionEvent arg0) {
+		for(Button b : ButtonHandler.getButtons())
+		{
+			b.setColor(new Color((int)(Math.random()*254+1), (int)(Math.random()*254+1), (int)(Math.random()*254+1)));
+		}
+	}
+}

+ 49 - 0
src/view/GameView.java

@@ -0,0 +1,49 @@
+package view;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JPanel;
+import javax.swing.Timer;
+
+import control.LedHandler;
+
+public class GameView extends JPanel implements ActionListener{
+	
+	Timer t;
+	Color c;
+	LedHandler led;
+	public GameView(LedHandler led)
+	{
+		this.led=led;
+		t = new Timer(1000/30, this);
+		t.start();
+		c = new Color(100,100,100);
+	}
+
+	public void actionPerformed(ActionEvent arg0) {
+		repaint();
+	}
+	
+	@Override
+	public void paintComponent(Graphics g)
+	{
+		super.paintComponent(g);
+		Graphics2D g2d = (Graphics2D) g;
+		
+		g2d.setPaint(c);
+		g2d.fillRect(0, 0, getWidth(), getHeight());
+	}
+	
+	public void setColor(Color c)
+	{
+		this.c = c;
+		for(int i =6; i < 55; i++){
+			led.setLed(i, c.getRed(), c.getGreen(), c.getBlue());
+		}
+		led.show();
+	}
+}