diff --git a/executablecode/example/MainPressureController.java b/executablecode/example/MainPressureController.java index 73816e383bfee96d74bd04ddf1057a22db72257a..554ad3cd6ef7d79a7232198dc4110ee26b50aec6 100644 --- a/executablecode/example/MainPressureController.java +++ b/executablecode/example/MainPressureController.java @@ -8,7 +8,7 @@ import java.io.*; import java.util.*; -public class MainPressureController extends JFrame implements Feeder, MouseListener { +public class MainPressureController extends JFrame implements Feeder, ChangeListener { static final int PRESSURE_MIN = 1; static final int PRESSURE_MAX = 30; @@ -17,7 +17,6 @@ public class MainPressureController extends JFrame implements Feeder, MouseListe static final String ALARM_ON = "ALARM ON"; static final String ALARM_OFF = "alarm off"; - private PressureControllerPanel mp; private DatagramServer ds; private JSlider pressureValue; @@ -25,7 +24,7 @@ public class MainPressureController extends JFrame implements Feeder, MouseListe public MainPressureController() { super("Pressure Controller demonstration"); - setSize(800, 600); + setSize(400, 150); setVisible(true); ds = new DatagramServer(); ds.setFeeder(this); @@ -37,79 +36,60 @@ public class MainPressureController extends JFrame implements Feeder, MouseListe public void initComponents() { setLayout(new BorderLayout()); pressureValue = new JSlider(JSlider.HORIZONTAL, PRESSURE_MIN, PRESSURE_MAX, PRESSURE_INIT); - pressureValue..addChangeListener(this); + pressureValue.addChangeListener(this); //Turn on labels at major tick marks. - pressure.setMajorTickSpacing(5); - pressure.setMinorTickSpacing(1); - pressure.setPaintTicks(true); - pressure.setPaintLabels(true); + pressureValue.setMajorTickSpacing(5); + pressureValue.setMinorTickSpacing(1); + pressureValue.setPaintTicks(true); + pressureValue.setPaintLabels(true); Font font = new Font("Serif", Font.ITALIC, 15); - pressure.setFont(font); + pressureValue.setFont(font); - add(pressure, BorderLayout.NORTH); + add(pressureValue, BorderLayout.NORTH); alarm = new JLabel(ALARM_OFF); - add(alarm, BorderLayout.SOUTH); + alarm.setHorizontalAlignment(JLabel.CENTER); + alarm.setVerticalAlignment(JLabel.CENTER); + add(alarm, BorderLayout.CENTER); //mp = new PressureControllerPanel(); //mp.addMouseListener(this); // mp.setPreferredSize(new Dimension(800,600)); //add(mp, BorderLayout.CENTER); - mp.revalidate(); + //mp.revalidate(); } public void setMessage(String msg) { - if (mp == null) { - return; - } - + int index; String s; int duration; System.out.println("Got message:" + msg); try { - if (msg.startsWith("Duration ")) { + if (msg.startsWith("+")) { s = msg.substring(9, msg.length()); - - duration = Integer.decode(s.trim()).intValue(); - mp.setDuration(duration); - mp.setStart(false); - //System.out.println("Setting new duration :" + duration); - mp.setCookingFinished(false); - } else if (msg.startsWith("Start ")) { - s = msg.substring(6, msg.length()); - - duration = Integer.decode(s.trim()).intValue(); - mp.setDuration(duration); - mp.setStart(true); - mp.setCookingFinished(false); - //System.out.println("Setting new duration (start): " + duration); - } else if (msg.startsWith("Magnetron_ON")) { - mp.setMagnetronON(true); - mp.setCookingFinished(false); - } else if (msg.startsWith("Magnetron_OFF")) { - mp.setMagnetronON(false); - mp.setCookingFinished(false); - } else if (msg.startsWith("Open Door")) { - mp.setDoorOpened(true); - mp.setCookingFinished(false); - } else if (msg.startsWith("Close Door")) { - mp.setDoorOpened(false); - mp.setCookingFinished(false); - } else if (msg.startsWith("Dring")) { - mp.setCookingFinished(true); - } + alarm.setText(ALARM_ON); + + } else if (msg.startsWith("-")) { + alarm.setText(ALARM_OFF); + } } catch (Exception e) { System.out.println("Exception when computing message: " + e.getMessage()); } - mp.repaint(); } - public void mouseClicked(MouseEvent e){ + public void stateChanged(ChangeEvent e) { + System.out.println("Value of pressure changed:" + pressureValue.getValue()); + if (ds != null) { + ds.sendDatagramTo("PRESSURE=" + pressureValue.getValue()); + } + } + + /*public void mouseClicked(MouseEvent e){ int x = e.getX(); int y = e.getY(); @@ -128,10 +108,10 @@ public class MainPressureController extends JFrame implements Feeder, MouseListe public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} - public void mouseReleased(MouseEvent e){} + public void mouseReleased(MouseEvent e){}*/ public static void main(String[] args) { - MainMicrowave mmw = new MainMicrowave(); + MainPressureController mmw = new MainPressureController(); } } diff --git a/executablecode/example/PressureControllerPanel.java b/executablecode/example/PressureControllerPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..dd70aecb3c70e15400dd64d4263986c920b12cbe --- /dev/null +++ b/executablecode/example/PressureControllerPanel.java @@ -0,0 +1,175 @@ +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import java.io.*; +import java.util.*; + + +public class PressureControllerPanel extends JPanel { + + private int duration = 0; + private boolean magnetronON =false; + private boolean start = false; + private boolean doorOpened = false; + private boolean cookingFinished = false; + private boolean alarmOn = false; + + public PressureControllerPanel() { + setBackground(Color.white); + } + + public void setAlarm(boolean _alarmOn) { + alarmOn = _alarmOn; + } + + public void setCookingFinished(boolean finished) { + cookingFinished = finished; + } + + public void setDoorOpened(boolean opened) { + doorOpened = opened; + } + + public void setStart(boolean on) { + start = on; + } + + public void setMagnetronON(boolean isOn) { + magnetronON = isOn; + } + + public void setDuration(int _duration) { + duration = _duration; + } + + + + /*public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g; + //System.out.println("Salut .. repainting"); + + // Foot + + g.setColor(Color.black); + g.fillRect(100, 500, 100, 30); + g.fillRect(600, 500, 100, 30); + + // Main oven + g.setColor(Color.gray); + g.fillRect(50, 50, 700, 450); + g.setColor(Color.black); + g.drawRect(50, 50, 700, 450); + + + g.setColor(Color.white); + g.fillRoundRect(75, 75, 500, 400, 20, 20); + + + // Inside of the oven + g.setColor(Color.black); + g.fillRoundRect(100, 100, 450, 350, 20, 20); + + + if (doorOpened) { + // Inside the oven + int dec = -5; + g.setColor(Color.white); + g.fillRoundRect(140, 140, 370, 270, 20, 20); + g.setColor(Color.black); + g.drawRect(140, 140, 370, 270); + g.drawRect(200, 190, 250, 170); + + g.drawLine(140, 140, 200, 190); + g.drawLine(510, 140, 450, 190); + g.drawLine(510, 410, 450, 360); + g.drawLine(140, 410, 200, 360); + + //Door + g.setColor(Color.white); + g.fillRect(95+dec, 110, 35, 330); + g.setColor(Color.black); + g.drawRect(95+dec, 110, 35, 330); + g.fillRect(95+dec, 110, 10, 330); + g.setColor(Color.gray); + g.fillRect(65+dec, 100, 25, 350); + g.fillRect(65+dec, 150, 35, 30); + g.fillRect(65+dec, 370, 35, 30); + g.setColor(Color.black); + g.drawRect(65+dec, 100, 25, 350); + + + + + } else { + + } + + if (magnetronON) { + g.setColor(Color.red); + g.fillRoundRect(150, 150, 350, 250, 20, 20); + } + + if (doorOpened) { + // Handle of the door + + } else { + // Door closed + g.setColor(Color.gray); + g.fillRect(460, 100, 50, 350); + g.setColor(Color.white); + g.drawRect(460, 100, 50, 350); + } + + // Panel to print duration & information + g.setColor(Color.white); + g.fillRect(625, 100, 100, 50); + g.setColor(Color.black); + g.fillRect(630, 105, 90, 40); + + g.setColor(Color.white); + g.fillRect(625, 160, 100, 150); + g.setColor(Color.black); + g.fillRect(630, 165, 90, 140); + + // Panel for start button + g.setColor(Color.white); + g.fillRect(625, 330, 100, 40); + g.setColor(Color.black); + g.fillRect(630, 335, 90, 30); + g.setColor(Color.white); + g.drawString("START", 655, 355); + g.setColor(Color.black); + + g.setColor(Color.green); + //System.out.println("Duration=" + duration); + Font fold = g.getFont(); + Font f = fold.deriveFont(30); + g.setFont(f); + + g.drawString(""+duration, 690, 130); + + if (magnetronON) { + g.drawString("Cooking", 650, 220); + } + + if (start) { + g.drawString("Start", 650, 190); + } + + if (cookingFinished) { + g.drawString("Finished", 650, 250); + } + + if (doorOpened) { + g.drawString("Door opened", 634, 280); + } + + + }*/ + + +}