From 0c6f8cff73bc5f57989091a03fba1331aa5bd2ff Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 8 Sep 2011 09:00:58 +0100 Subject: Improved the look of the buttons, added new effect noise, added serial debug (not finished or working yet) and made interface more flexible with scroll panels. --- PunchingBag/src/PunchingBag.java | 62 ++++++++++++++----- PunchingBag/src/PunchingBagGUI.java | 115 ++++++++++++++++++++++++++++++++++-- 2 files changed, 158 insertions(+), 19 deletions(-) diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 7f7b044..4bab465 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -1,5 +1,6 @@ import javax.swing.event.EventListenerList; +import java.awt.Rectangle; import java.awt.geom.Area; import java.io.IOException; @@ -15,7 +16,9 @@ public class PunchingBag implements Runnable { final public int ledHeight = 20; final public int ledWidth = 9; private byte[] rawLeds = new byte[6 * 8]; - private Colour[][] leds = new Colour[ledWidth][ledHeight]; + private Colour[][] leds = new Colour[ledWidth][ledHeight]; // NEVER ACCESS + // DIRECTLY (Use + // SetLedInternal) private byte[] ledGridIntensities = new byte[6]; final public int buttonHeight = 19; final public int buttonWidth = 8; @@ -87,6 +90,10 @@ public class PunchingBag implements Runnable { runningEffects.add(new CircleExpand(x, y, 100)); } + void noise(Rectangle rect, long time) { + runningEffects.add(new Noise(rect, System.currentTimeMillis() + time)); + } + abstract class Effect { long lastRefresh = 0; boolean stop = false; @@ -168,13 +175,44 @@ public class PunchingBag implements Runnable { // longhand: currentRadius = currentRadius + 0.1 + (currentRadius / // 10); - if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, - colour)) + if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, colour)) stop = true; } } + class Noise extends Effect { + final Rectangle area; + final long endTime; + + public Noise(Rectangle area, long endTime) { + this.area = area; + this.endTime = endTime; + } + + @Override + public void draw() { + if (endTime >= System.currentTimeMillis()) { + for (int y = area.y; y < (area.y + area.height); y++) { + for (int x = area.x; x < (area.x + area.width); x++) { + double random = Math.random(); + if (random < 0.25) { + setLEDInternal(x, y, Colour.Red); + } else if (random < 0.5) { + setLEDInternal(x, y, Colour.Yellow); + } else if (random < 0.75) { + setLEDInternal(x, y, Colour.Green); + } else { + setLEDInternal(x, y, Colour.None); + } + } + } + } else { + stop(); + } + } + } + public boolean drawEllipse(int x, int y, int radx, int rady, Colour colour) { if (radx == 0 && rady == 0) { return setLEDInternal(x, y, colour); @@ -226,15 +264,12 @@ public class PunchingBag implements Runnable { for (int y = 0; y <= 18; y++) { for (int x = 0; x <= 6; x++) { if ((y % 2) == 0) { - if (leds[x][y] == Colour.Green - || leds[x][y] == Colour.Yellow) { - rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math - .floor(y / 4)] | (1 << (7 - x))); + if (leds[x][y] == Colour.Green || leds[x][y] == Colour.Yellow) { + rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math.floor(y / 4)] | (1 << (7 - x))); } } else { if (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) { - rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math - .floor(y / 4)] | (1 << (7 - x))); + rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math.floor(y / 4)] | (1 << (7 - x))); } } } @@ -293,8 +328,7 @@ public class PunchingBag implements Runnable { // ConcurrentModificationException's // (havent managed to produce one // though - for (Iterator iter = runningEffects.iterator(); iter - .hasNext();) { + for (Iterator iter = runningEffects.iterator(); iter.hasNext();) { Effect ef = (Effect) iter.next(); if (ef.stop) { iter.remove(); @@ -320,16 +354,14 @@ public class PunchingBag implements Runnable { */ if (ledsChanged) { - LEDListener[] LEDListeners = ledListenerList - .getListeners(LEDListener.class); + LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class); for (int i = 0; i < ledListenerList.getListenerCount(); i++) { LEDListeners[i].LEDchanged(); } } if (buttonsChanged) { - ButtonListener[] buttonListeners = buttonListenerList - .getListeners(ButtonListener.class); + ButtonListener[] buttonListeners = buttonListenerList.getListeners(ButtonListener.class); for (int i = 0; i < buttonListenerList.getListenerCount(); i++) { for (int x = 0; x < buttonWidth; x++) { for (int y = 0; y < buttonHeight; y++) { diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 5f1b571..91d223a 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -6,6 +6,7 @@ import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; @@ -20,6 +21,10 @@ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextArea; import javax.swing.ListCellRenderer; public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener { @@ -62,10 +67,109 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto bag.addButtonListener(this); bag.addLEDChangeListener(this); - frame.add(new PunchingBagSim(), BorderLayout.WEST); + JSplitPane bottomTopSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); - frame.add(new LEDControlPanel(), BorderLayout.CENTER); + JSplitPane centerTopSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + JTabbedPane tabbedPane = new JTabbedPane(); + tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(), "Allows control of individual LED's"); + + centerTopSplitPane.setLeftComponent(new PunchingBagSim()); + centerTopSplitPane.setRightComponent(tabbedPane); + + bottomTopSplitPane.setTopComponent(centerTopSplitPane); + bottomTopSplitPane.setBottomComponent(new SerialPanel()); + + frame.add(bottomTopSplitPane, BorderLayout.CENTER); + } + + class SerialPanel extends JPanel implements ActionListener { + JTextArea buttonIn = new JTextArea(); + JTextArea buttonOut = new JTextArea(); + JTextArea ledIn = new JTextArea(); + JTextArea ledOut = new JTextArea(); + + public SerialPanel() { + JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + JSplitPane buttonSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + JSplitPane ledSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + /* + * splitPane.setOneTouchExpandable(true); + * buttonSplitPane.setOneTouchExpandable(true); + * ledSplitPane.setOneTouchExpandable(true); + */ + + splitPane.setMinimumSize(new Dimension(200, 50)); + buttonSplitPane.setMinimumSize(new Dimension(100, 50)); + ledSplitPane.setMinimumSize(new Dimension(100, 50)); + + JButton clear = new JButton("Clear"); + clear.addActionListener(this); + + // buttonIn.setColumns(20); + // buttonIn.setRows(5); + buttonIn.setBorder(BorderFactory.createTitledBorder("Serial In - Button Arduino")); + // buttonOut.setColumns(20); + // buttonOut.setRows(5); + buttonOut.setBorder(BorderFactory.createTitledBorder("Serial Out - Button Arduino")); + // ledIn.setColumns(20); + // ledIn.setRows(5); + ledIn.setBorder(BorderFactory.createTitledBorder("Serial In - LED Arduino")); + // ledOut.setColumns(20); + // ledOut.setRows(5); + ledOut.setBorder(BorderFactory.createTitledBorder("Serial Out - LED Arduino")); + + JPanel buttonInPanel = new JPanel(); + JScrollPane areaScrollPane = new JScrollPane(buttonIn); + areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + buttonInPanel.add(areaScrollPane, BorderLayout.CENTER); + clear.setActionCommand("BI"); + buttonInPanel.add(clear, BorderLayout.SOUTH); + buttonSplitPane.setLeftComponent(buttonInPanel); + + JPanel buttonOutPanel = new JPanel(); + areaScrollPane = new JScrollPane(buttonOut); + areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + buttonOutPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("BO"); + buttonOutPanel.add(clear, BorderLayout.SOUTH); + buttonSplitPane.setRightComponent(buttonOutPanel); + + JPanel ledInPanel = new JPanel(); + areaScrollPane = new JScrollPane(ledIn); + areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + ledInPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("LI"); + ledInPanel.add(clear, BorderLayout.SOUTH); + ledSplitPane.setLeftComponent(ledInPanel); + + JPanel ledOutPanel = new JPanel(); + areaScrollPane = new JScrollPane(ledOut); + areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + ledOutPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("LO"); + ledOutPanel.add(clear, BorderLayout.SOUTH); + ledSplitPane.setRightComponent(ledOutPanel); + + splitPane.setLeftComponent(buttonSplitPane); + splitPane.setRightComponent(ledSplitPane); + + this.add(splitPane); + + } + + @Override + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + + } } class LEDControlPanel extends JPanel implements ActionListener { @@ -278,7 +382,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto super.paintComponent(g); g.setColor(Color.black); - g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true); + g.fillRect(0, 0, this.getWidth(), this.getHeight()); + g.setColor(new Color(220,220,220)); + g.fillRect(4, 4, this.getWidth()-8, this.getHeight()-8); } @@ -394,7 +500,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto @Override public void buttonPressed(int x, int y) { System.out.println("Button Pressed: " + x + " " + y); - bag.circleExpand(x, y, 16); + // bag.circleExpand(x, y, 16); + bag.noise(new Rectangle(0, 0, 9, 20), 5000); } -- cgit v1.2.3