2016-03-29 6 views
-1

Ich versuche, eine Ampel Semaphor mit Threads zu bauen, aber ich kann nicht richtig.Semaphore sollte rot, gelb, grün mit Pause zwischen den Farben anzeigen. Jede Hilfe wird sehr geschätzt. Danke. Hier ist mein Code ...Ampel-Semaphor mit Threads

import java.awt.BorderLayout; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class StopTheLights extends JFrame implements ActionListener { 

    JButton start; 
    JButton stop; 
     JPanel panel; 
    Boolean flag; 

    public StopTheLights(String title) { 
       Container c = getContentPane(); 

     start = new JButton("Start"); 
     stop = new JButton("Stop"); 

     start.addActionListener(this); 
     stop.addActionListener(this); 

     panel= new JPanel(); 
     panel.add(start); 
     panel.add(stop); 

     c.add(panel, BorderLayout.SOUTH); 

     setSize(300, 450);      
     setVisible(true); 
     setLocation(200,200); 

    } 

    public void paint(Graphics g){ 
     super.paint(g); 
     g.setColor(Color.black); 
     g.drawOval(50,50,100,100); 
     g.drawOval(50,155,100,100); 
     g.drawOval(50,260,100,100); 
    } 

     public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == start){ 
      flag = true; 
      new ThreadExtend(this).start(); 
     }else{ 
      flag = false; 
     } 
    } 
    public class ThreadExtend extends Thread { 
     Graphics g; 
     JFrame frame; 

     public ThreadExtend(JFrame frame){ 
      this.frame = frame; 
      g = frame.getGraphics(); 
     } 
     public void run(){ 
      while(flag) { 
       try{ 
        paintRed(g); 
        if(!flag) { 
         break; 
        } 
        Thread.sleep(1000); 
        paintAmber(g); 
        if(!flag) { 
        break; 
        } 
        Thread.sleep(1000); 
        paintGreen(g); 
        if(!flag){ 
         break; 
        } 


       }catch(InterruptedException e) { 

       } 
      } 

     } 

    } 
    public void paintRed(Graphics g){ 
     g.setColor(new Color(255,0,0)); 
     g.fillOval(52, 52, 96, 96); 
      g.fillOval(52, 52, 96, 96); 
     g.fillOval(52, 262, 96, 96); 

    } 
    public void paintAmber(Graphics g){ 
     g.setColor(new Color(250,170,0)); 
     g.fillOval(52, 157, 96, 96); 
      g.fillOval(52, 52, 96, 96); 
     g.fillOval(52, 262, 96, 96); 

    } 
    public void paintGreen(Graphics g){ 
     g.setColor(new Color(0,250,0)); 
     g.fillOval(52, 262, 96, 96); 
      g.fillOval(52, 52, 96, 96); 
     g.fillOval(52, 157, 96, 96); 

    } 
     public static void main(String[] args) { 
     new StopTheLights(); 

    } 


} 
+1

was ist das Problem? – jhamon

Antwort

1

Es gibt hier mehrere Probleme.

  • Ihr Thread wartet nicht auf grünes Licht.
  • Sie malen alle Lichter in allen Methoden ruft, die nicht benötigt wird.
  • Sie ändern die Farbe nicht, wenn "Schließen" andere Lichter.

habe ich einige der Fixierung in Ihrem Code, und jetzt funktioniert es:

import java.awt.BorderLayout; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class StopTheLights extends JFrame implements ActionListener { 

    JButton start; 
    JButton stop; 
    Boolean i; 
    //create constructor method 
    public StopTheLights(String title) { 
      // create Start button 
      start = new JButton("Start"); 
      //create Stop button 
      stop = new JButton("Stop"); 
      //add Action listeners 
      start.addActionListener(this); 
      stop.addActionListener(this); 
      //create new Button Panel 
      JPanel buttonPanel = new JPanel(); 
      buttonPanel.add(start); 
      buttonPanel.add(stop); 
      Container c = getContentPane(); 
      c.add(buttonPanel, BorderLayout.SOUTH); 

      setSize(200, 425); 
      setLocationRelativeTo(null); 
      setResizable(false); 
      setVisible(true); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public void paint(Graphics g){ 
      super.paint(g); 
      g.setColor(Color.black); 
      g.fillOval(50,50,100,100); 
      g.fillOval(50,155,100,100); 
      g.fillOval(50,260,100,100); 
    } 

    public static void main(String[] args) { 
      new StopTheLights("Stop The Lights"); 

    } 
    public void actionPerformed(ActionEvent e){ 
      if(e.getSource() == start){ 
        i = true; 
        new ThreadExtend(this).start(); 
      }else{ 
        i = false; 
      } 
    } 
    public class ThreadExtend extends Thread { 
      Graphics g; 
      JFrame frame; 

      public ThreadExtend(JFrame frame){ 
        this.frame = frame; 
        g = frame.getGraphics(); 
      } 
      public void run(){ 
        while(i) { 
          try{ 
            red(g); 
            if(!i) { 
              break; 
            } 
            Thread.sleep(1000); 
            amber(g); 
            if(!i) { 
              break; 
            } 
            Thread.sleep(1000); 
            green(g); 
            if(!i){ 
              break; 
            } 
            Thread.sleep(1000); 


          }catch(InterruptedException e) { 
             e.printStackTrace(); 
         } 
        } 
      } 

    } 
    public void red(Graphics g){ 
      g.setColor(Color.black); 
      g.fillOval(52, 262, 96, 96); 
      g.setColor(new Color(255,0,0)); 
      g.fillOval(52, 52, 96, 96); 
    } 
    public void amber(Graphics g){ 
      g.setColor(Color.black); 
      g.fillOval(52, 52, 96, 96); 
      g.setColor(new Color(250,170,0)); 
      g.fillOval(52, 157, 96, 96); 
    } 
    public void green(Graphics g){ 
      g.setColor(Color.black); 
      g.fillOval(52, 157, 96, 96); 
      g.setColor(new Color(0,250,0)); 
      g.fillOval(52, 262, 96, 96); 
    } 

} 
+0

Danke. Du hattest Recht. Es funktioniert gut. – danielG