diff options
author | tjung | 2012-06-16 12:00:49 +0000 |
---|---|---|
committer | Thomas Schuetz | 2012-06-16 12:08:38 +0000 |
commit | 56075d7da3b76d7375dcceed4dcb4d604fa3d807 (patch) | |
tree | fc19cba81f4c5a7f997de73f9b3078e33251c3d3 | |
parent | 73996db7dbf55911e4eefcca7f85041a984594d2 (diff) | |
download | org.eclipse.etrice-56075d7da3b76d7375dcceed4dcb4d604fa3d807.tar.gz org.eclipse.etrice-56075d7da3b76d7375dcceed4dcb4d604fa3d807.tar.xz org.eclipse.etrice-56075d7da3b76d7375dcceed4dcb4d604fa3d807.zip |
initial import of Traffic Light with TCP Socket Server
15 files changed, 395 insertions, 0 deletions
diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.classpath b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.classpath new file mode 100644 index 000000000..18d70f02c --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.project b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.project new file mode 100644 index 000000000..a0bb7df0c --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.etrice.tutorials.simulators.trafficlight</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.settings/org.eclipse.jdt.core.prefs b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..a23aa28f4 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Sat Jun 16 13:47:44 CEST 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.class Binary files differnew file mode 100644 index 000000000..744cd0b19 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.class Binary files differnew file mode 100644 index 000000000..d28f1068e --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.class Binary files differnew file mode 100644 index 000000000..3249b05f2 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.class Binary files differnew file mode 100644 index 000000000..0a4c434ab --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.class Binary files differnew file mode 100644 index 000000000..733f31070 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.class b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.class Binary files differnew file mode 100644 index 000000000..d40cfc1e0 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/bin/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.class diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.java new file mode 100644 index 000000000..a8e6a92ff --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/ButtonActionListener.java @@ -0,0 +1,35 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; + + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.OutputStream; +import java.io.IOException; + +public class ButtonActionListener +implements ActionListener { + + private OutputStream out; + private String cmd1 = "requestGreen"; + + public ButtonActionListener(OutputStream out){ + this.out = out; + } + + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + String s = arg0.getActionCommand(); + + if (s.equals("REQUEST")) { + System.out.println("Taste gedrückt"); + try { + out.write(cmd1.getBytes()); + out.flush(); + } + catch (IOException e){ + System.out.println(e.toString()); + } + // out.write(cmd1.getBytes()); + } + } +} diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.java new file mode 100644 index 000000000..8f90361da --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/Main.java @@ -0,0 +1,13 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; + +public class Main { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + PedastrianLightWnd wnd = new PedastrianLightWnd(); + } + +} diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.java new file mode 100644 index 000000000..9e9a13940 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/PedastrianLightWnd.java @@ -0,0 +1,125 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; +import java.awt.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; +import java.io.OutputStream; +import java.net.*; +import java.awt.event.ActionEvent; + +public class PedastrianLightWnd extends Frame { + // Konstructor + public PedastrianLightWnd (){ + super("PedestrianLightsGUI"); + GridBagLayout layout = new GridBagLayout(); + GridBagConstraints gbc; + TrafficLight3 carLights = new TrafficLight3(4); + TrafficLight2 pedLights = new TrafficLight2(2); + TextField statusLine = new TextField(); + Button requestButton = new Button("REQUEST"); + requestButton.setEnabled(false); + addWindowListener(new WindowClosingAdapter(true)); + char cmdString[]=new char[100]; + int cmdCounter = 0, cmdState = 0; + + setLayout(layout); + + gbc=makeGbc(0,1,1,1); + layout.setConstraints(carLights,gbc); + add(carLights); + + gbc=makeGbc(1,1,1,1); + layout.setConstraints(pedLights,gbc); + add(pedLights); + + statusLine.setText("listening on TCP Port 4444"); + statusLine.setEditable(false); + statusLine.setFocusable(false); + gbc = makeGbc(0,0,2,1); + gbc.fill = GridBagConstraints.BOTH; + layout.setConstraints(statusLine,gbc); + add(statusLine); + + gbc = makeGbc(0,2,2,1); + gbc.fill = GridBagConstraints.BOTH; + layout.setConstraints(requestButton,gbc); + add(requestButton); + + pack(); + setVisible(true); + + // open socket + try { + ServerSocket echod = new ServerSocket(4444); + Socket socket = echod.accept(); + statusLine.setText("connected !"); + + // InputStream in = socket.getInputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + OutputStream out = socket.getOutputStream(); + + requestButton.addActionListener(new ButtonActionListener(out)); + requestButton.setEnabled(true); + + carLights.setState(4); + pedLights.setState(2); + + String cmd; + + while ((cmd = in.readLine()) != null){ + + if (cmd.equals("carLights=red")){ + carLights.setState(0); + } + if (cmd.equals("carLights=yellowRed")){ + carLights.setState(1); + } + if (cmd.equals("carLights=green")){ + carLights.setState(2); + } + if (cmd.equals("carLights=yellow")){ + carLights.setState(3); + } + if (cmd.equals("pedLights=red")){ + pedLights.setState(0); + } + if (cmd.equals("pedLights=green")){ + pedLights.setState(1); + } + if (cmd.equals("pedLights=off")){ + pedLights.setState(2); + } + if (cmd.equals("carLights=off")){ + carLights.setState(4); + } + if (cmd.equals("button=on")){ + requestButton.setEnabled(true); + } + if (cmd.equals("button=off")){ + requestButton.setEnabled(false); + } + } + // System.out.println("Verbindung beendet"); + socket.close(); + echod.close(); + }catch (IOException e) { + System.err.println(e.toString()); + System.exit(1); + } + } + + private GridBagConstraints makeGbc(int x, int y, int width, int height){ + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = x; + gbc.gridy = y; + gbc.gridwidth = width; + gbc.gridheight = height; + gbc.weightx = 1; + gbc.weighty = 1; + // gbc.fill = GridBagConstraints.BOTH; + gbc.anchor = GridBagConstraints.CENTER; + gbc.insets = new Insets(1,1,1,1); + return gbc; + } +} diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.java new file mode 100644 index 000000000..945080843 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight2.java @@ -0,0 +1,71 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; +import java.awt.*; +import java.awt.event.*; + + +public class TrafficLight2 extends Canvas { + // Traffic light has 4 states + // 0 = red + // 1 = yellowRed + // 2 = green + // 3 = yellow + + private int state; + + public TrafficLight2(int state){ + this.state = state; + } + public TrafficLight2(){ + this.state = 0; + } + + private int singleLightSize = 20; + private int xPosition = 5; + private int gap = 5; + + public void paint(Graphics g){ + Color lightRed = new Color(255,0,0); + Color darkRed = new Color (127,0,0); + Color lightGreen = new Color (0,255,0); + Color darkGreen = new Color (0,127,0); + + Color colors [][] = { + {lightRed,darkGreen}, + {darkRed,lightGreen}, + {darkRed,darkGreen} + }; + + // set Background + g.setColor(Color.lightGray); + g.fillRect(0,0,getSize().width,getSize().height); + // draw boarder + g.setColor(Color.BLACK); + g.drawRect(0,0,getSize().width-1,getSize().height-1); + + for (int i = 0; i<2; i++) { + g.setColor(colors[state][i]); + g.fillOval(xPosition,i*singleLightSize+gap,singleLightSize,singleLightSize); + g.setColor(Color.black); + g.drawOval(xPosition,i*singleLightSize+gap,singleLightSize-1,singleLightSize-1); + } + } + + public Dimension getPreferredSize(){ + return new Dimension(singleLightSize + 2 * gap, singleLightSize * 2 + 2 * gap); + } + + public Dimension getMinimumSize() + { + return new Dimension(singleLightSize + 2 * gap, singleLightSize * 2 + 2 * gap); + } + + public void setState(int state) + { + // avoid array error + if (state < 3){ + this.state = state; + repaint(); + } + } + +} diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.java new file mode 100644 index 000000000..aaf94e8d0 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/TrafficLight3.java @@ -0,0 +1,75 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; +import java.awt.*; +import java.awt.event.*; + + +public class TrafficLight3 extends Canvas { + // Traffic light has 4 states + // 0 = red + // 1 = yellowRed + // 2 = green + // 3 = yellow + // 4 = OFF + + private int state; + + public TrafficLight3(int state){ + this.state = state; + } + public TrafficLight3(){ + this.state = 0; + } + + private int singleLightSize = 20; + private int xPosition = 5; + private int gap = 5; + + public void paint(Graphics g){ + Color lightRed = new Color(255,0,0); + Color darkRed = new Color (127,0,0); + Color lightGreen = new Color (0,255,0); + Color darkGreen = new Color (0,127,0); + Color lightYellow = new Color (255,255,0); + Color darkYellow = new Color (127,127,0); + + Color colors [][] = { + {lightRed,darkYellow,darkGreen}, + {lightRed,lightYellow,darkGreen}, + {darkRed,darkYellow,lightGreen}, + {darkRed,lightYellow,darkGreen}, + {darkRed,darkYellow,darkGreen} + }; + + + // set Background + g.setColor(Color.lightGray); + g.fillRect(0,0,getSize().width,getSize().height); + // draw boarder + g.setColor(Color.BLACK); + g.drawRect(0,0,getSize().width-1,getSize().height-1); + + for (int i = 0; i<3; i++) { + g.setColor(colors[state][i]); + g.fillOval(xPosition,i*singleLightSize+gap,singleLightSize,singleLightSize); + g.setColor(Color.black); + g.drawOval(xPosition,i*singleLightSize+gap,singleLightSize-1,singleLightSize-1); + } + } + + public Dimension getPreferredSize(){ + return new Dimension(singleLightSize + 2 * gap, singleLightSize * 3 + 2 * gap); + } + + public Dimension getMinimumSize() + { + return new Dimension(singleLightSize + 2 * gap, singleLightSize * 3 + 2 * gap); + } + + public void setState(int state) + { + if (state < 5){ + this.state = state; + repaint(); + } + } +} diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.java new file mode 100644 index 000000000..68b16287d --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight/src/org/eclipse/etrice/tutorials/simulators/trafficlight/WindowClosingAdapter.java @@ -0,0 +1,41 @@ +package org.eclipse.etrice.tutorials.simulators.trafficlight; + + +/* WindowClosingAdapter.java */ + +import java.awt.*; +import java.awt.event.*; + +public class WindowClosingAdapter +extends WindowAdapter +{ + private boolean exitSystem; + + /** + * Erzeugt einen WindowClosingAdapter zum Schliessen + * des Fensters. Ist exitSystem true, wird das komplette + * Programm beendet. + */ + public WindowClosingAdapter(boolean exitSystem) + { + this.exitSystem = exitSystem; + } + + /** + * Erzeugt einen WindowClosingAdapter zum Schliessen + * des Fensters. Das Programm wird nicht beendet. + */ + public WindowClosingAdapter() + { + this(false); + } + + public void windowClosing(WindowEvent event) + { + event.getWindow().setVisible(false); + event.getWindow().dispose(); + if (exitSystem) { + System.exit(0); + } + } +} |