Facebook
From reeeeeeeeeeeeee, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 417
  1. package org.example;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.net.DatagramPacket;
  8. import java.net.DatagramSocket;
  9. import java.net.InetAddress;
  10. import java.nio.charset.StandardCharsets;
  11.  
  12. public class AmpelGUI extends JFrame {
  13.   private JPanel carLightPanel;
  14.   private JPanel pedestrianLightPanel;
  15.   private JButton pedestrianButton;
  16.   private UDPClient client;
  17.  
  18.   private LightPanel carRedLight;
  19.   private LightPanel carYellowLight;
  20.   private LightPanel carGreenLight;
  21.   private LightPanel pedestrianRedLight;
  22.   private LightPanel pedestrianGreenLight;
  23.  
  24.   public AmpelGUI() throws Exception {
  25.     client = new UDPClient();
  26.     initUI();
  27.     startAmpelsteuerung();
  28.   }
  29.  
  30.   private void initUI() {
  31.     setTitle("Ampelsimulation");
  32.     setSize(400, 400);
  33.     setLayout(new GridLayout(1, 2));
  34.  
  35.     carLightPanel = new JPanel(new GridLayout(3, 1)); // Vertical arrangement
  36.     carRedLight = new LightPanel(Color.RED);
  37.     carYellowLight = new LightPanel(Color.GRAY);
  38.     carGreenLight = new LightPanel(Color.GRAY);
  39.     carLightPanel.add(carRedLight);
  40.     carLightPanel.add(carYellowLight);
  41.     carLightPanel.add(carGreenLight);
  42.     add(carLightPanel);
  43.  
  44.     pedestrianLightPanel = new JPanel(new GridLayout(3, 1)); // Vertical arrangement
  45.     pedestrianRedLight = new LightPanel(Color.RED);
  46.     pedestrianGreenLight = new LightPanel(Color.GRAY);
  47.     pedestrianLightPanel.add(pedestrianRedLight);
  48.     pedestrianLightPanel.add(new JLabel()); // Spacer
  49.     pedestrianLightPanel.add(pedestrianGreenLight);
  50.     add(pedestrianLightPanel);
  51.  
  52.     pedestrianButton = new JButton("Taster 1");
  53.     pedestrianButton.setBackground(Color.GRAY);
  54.     pedestrianButton.addActionListener(new ActionListener() {
  55.       @Override
  56.       public void actionPerformed(ActionEvent e) {
  57.         pedestrianButton.setBackground(Color.BLUE);
  58.         Timer timer = new Timer(100, new ActionListener() {
  59.           @Override
  60.           public void actionPerformed(ActionEvent e) {
  61.             pedestrianButton.setBackground(Color.GRAY);
  62.           }
  63.         });
  64.         timer.setRepeats(false);
  65.         timer.start();
  66.       }
  67.     });
  68.  
  69.     JPanel buttonPanel = new JPanel(new BorderLayout());
  70.     buttonPanel.add(pedestrianButton, BorderLayout.SOUTH);
  71.     add(buttonPanel);
  72.  
  73.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74.     setLocationRelativeTo(null);
  75.     setVisible(true);
  76.   }
  77.  
  78.   private void startAmpelsteuerung() {
  79.     new Thread(() -> {
  80.       while (true) {
  81.         try {
  82.           setTrafficLights(Color.RED, Color.GRAY, Color.GRAY, Color.RED, Color.GRAY);
  83.  
  84.           Thread.sleep(10000);
  85.  
  86.           setTrafficLights(Color.RED, Color.YELLOW, Color.GRAY, Color.RED, Color.GRAY);
  87.           Thread.sleep(2000);
  88.  
  89.           setTrafficLights(Color.GRAY, Color.GRAY, Color.GREEN, Color.RED, Color.GRAY);
  90.           Thread.sleep(15000);
  91.  
  92.           setTrafficLights(Color.GRAY, Color.YELLOW, Color.GRAY, Color.RED, Color.GRAY);
  93.           Thread.sleep(2000);
  94.  
  95.           setTrafficLights(Color.RED, Color.GRAY, Color.GRAY, Color.RED, Color.GRAY);
  96.           Thread.sleep(2000);
  97.  
  98.           setTrafficLights(Color.RED, Color.GRAY, Color.GRAY, Color.GRAY, Color.GREEN);
  99.           Thread.sleep(10000);
  100.         } catch (InterruptedException e) {
  101.           e.printStackTrace();
  102.         } catch (Exception e) {
  103.           throw new RuntimeException(e);
  104.         }
  105.       }
  106.     }).start();
  107.   }
  108.  
  109.   private void setTrafficLights(Color carRed, Color carYellow, Color carGreen, Color pedestrianRed, Color pedestrianGreen) {
  110.     carRedLight.setColor(carRed);
  111.     carYellowLight.setColor(carYellow);
  112.     carGreenLight.setColor(carGreen);
  113.     pedestrianRedLight.setColor(pedestrianRed);
  114.     pedestrianGreenLight.setColor(pedestrianGreen);
  115.   }
  116.  
  117.   public static void main(String[] args) {
  118.     try {
  119.       new AmpelGUI();
  120.     } catch (Exception e) {
  121.       e.printStackTrace();
  122.     }
  123.   }
  124. }
  125.  
  126. class LightPanel extends JPanel {
  127.   private Color color;
  128.  
  129.   public LightPanel(Color initialColor) {
  130.     this.color = initialColor;
  131.     setPreferredSize(new Dimension(100, 100));
  132.   }
  133.  
  134.   public void setColor(Color color) {
  135.     this.color = color;
  136.     repaint();
  137.   }
  138.  
  139.   @Override
  140.   protected void paintComponent(Graphics g) {
  141.     super.paintComponent(g);
  142.     g.setColor(color);
  143.     g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);
  144.   }
  145. }
  146.  
  147. class UDPClient {
  148.   private static final String SERVER_IP = "90.0.0.2"; // Beispiel IP-Adresse, bitte anpassen
  149.   private static final int SERVER_PORT = 29060; // Beispiel Port, bitte anpassen
  150.  
  151.   private DatagramSocket socket;
  152.   private InetAddress address;
  153.   private String lastMessage;
  154.  
  155.   public UDPClient() throws Exception {
  156.     socket = new DatagramSocket();
  157.     address = InetAddress.getByName(SERVER_IP);
  158.   }
  159.  
  160.   public String sendRequest(String message) throws Exception {
  161.     lastMessage = message;
  162.     byte[] buf = message.getBytes(StandardCharsets.US_ASCII);
  163.     DatagramPacket packet = new DatagramPacket(buf, buf.length, address, SERVER_PORT);
  164.     socket.send(packet);
  165.  
  166.     byte[] responseBuf = new byte[256];
  167.     packet = new DatagramPacket(responseBuf, responseBuf.length);
  168.     socket.receive(packet);
  169.     return new String(packet.getData(), 0, packet.getLength(), StandardCharsets.US_ASCII);
  170.   }
  171.  
  172.   public String requestAnalogValues() throws Exception {
  173.     String message = "AE,?";
  174.     String response = sendRequest(message);
  175.  
  176.     if (response.startsWith("AE,")) {
  177.       return response;
  178.     } else if (response.startsWith("FE,")) {
  179.       handleError(response);
  180.       return null;
  181.     } else {
  182.       throw new Exception("Unerwartete Antwort: " + response);
  183.     }
  184.   }
  185.  
  186.   public String requestDigitalInputs() throws Exception {
  187.     String message = "DE,?";
  188.     String response = sendRequest(message);
  189.  
  190.     if (response.startsWith("DE,")) {
  191.       return response;
  192.     } else if (response.startsWith("FE,")) {
  193.       handleError(response);
  194.       return null;
  195.     } else {
  196.       throw new Exception("Unerwartete Antwort: " + response);
  197.     }
  198.   }
  199.  
  200.   public void setDigitalOutputs(String outputStates) throws Exception {
  201.     String message = "DA," + outputStates;
  202.     String response = sendRequest(message);
  203.  
  204.     if (response.startsWith("FE,")) {
  205.       handleError(response);
  206.     } else {
  207.       throw new Exception("Unerwartete Antwort: " + response);
  208.     }
  209.   }
  210.  
  211.   private void handleError(String response) {
  212.     System.out.println("Fehler: " + response);
  213.     // Fehlerbehandlung hier implementieren
  214.  
  215.     // Wiederholen, bis keine Fehler mehr auftreten
  216.     while (response.startsWith("FE,")) {
  217.       try {
  218.         Thread.sleep(1000); // Wartezeit vor erneutem Versuch
  219.         response = sendRequest(lastMessage);
  220.       } catch (Exception e) {
  221.         e.printStackTrace();
  222.       }
  223.     }
  224.   }
  225. }
  226.