2016-04-15 5 views
0

Grundsätzlich muss mein Belisha Beacon Orange bleiben, wenn ich auf die Steady-Taste klicke, aber in meinem Programm, wenn ich auf den Steady Button klicke, bleibt die Beacon stattdessen Hellgrau. Kann mir bitte jemand sagen, wo ich falsch liege? Vielen Dank :). Hier ist mein Code:JButton Abfrage im Belisha Beacon Programm

package Homework; 


import java.awt.*; 
import java.awt.geom.*; 
import java.awt.event.*; 
import javax.swing.Timer; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class BelishaBeacon { 
    private static Timer timer; 
    public class Drawing extends JPanel { 

     private int x = 125; 
     private int y = 80; 
     private boolean changeColors = false; 


     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 

      Rectangle box1 = new Rectangle(165, 180, 20, 45); 
      Rectangle box2 = new Rectangle(165, 225, 20, 45); 
      Rectangle box3 = new Rectangle(165, 270, 20, 45); 
      Rectangle box4 = new Rectangle(165, 315, 20, 45); 
      Rectangle box5 = new Rectangle(165, 360, 20, 45); 
      Rectangle box6 = new Rectangle(165, 405, 20, 45); 


      Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100); 
      g2.draw(ball); 
      g2.draw(box1); 
      g2.draw(box2); 
      g2.draw(box3); 
      g2.draw(box4); 
      g2.draw(box5); 
      g2.draw(box6); 

      g2.setColor(Color.BLACK); 
      g2.fill(box1); 
      g2.fill(box3); 
      g2.fill(box5); 
      g2.setColor(Color.ORANGE); 
      g2.fill(ball); 
      changeColors = !changeColors; 
      if (changeColors) { 
       g2.setColor(Color.lightGray); 
       g2.fill(new Ellipse2D.Double(x, y, 100, 100)); 
      } 
     } 




     public void changeColors() { 
      changeColors = true; 
      repaint(); 
     } 
    } 

    public BelishaBeacon() { 

     JFrame frame = new JFrame(); 
     frame.setSize(350, 570); 
     frame.setTitle("Belisha Beacon"); 
     frame.setLayout(new BorderLayout(0, 0)); 
     final Drawing shapes = new Drawing(); 

     timer = new Timer(500, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       shapes.repaint(); 
      } 
     }); 



     JButton jbtFlash = new JButton("Flash"); 
     jbtFlash.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Flashing"); 
       if (!timer.isRunning()) { 
        timer.start(); 
       } 

      } 
     }); 




     final JButton jbtSteady = new JButton("Steady"); 
     jbtSteady.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 

         timer.stop(); 
        } 
       }); 


     JPanel controlPanel = new JPanel(); 
     controlPanel.setLayout(new GridLayout(1, 2, 0, 0)); 
     controlPanel.add(jbtFlash); 
     controlPanel.add(jbtSteady); 

     frame.add(controlPanel, BorderLayout.SOUTH); 
     frame.add(shapes); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new BelishaBeacon(); 
     timer.start(); 
    } 
} 
+0

Dies ist genau das gleiche wie diese http://stackoverflow.com/questions/36651192/jbutton-query-in-belisha-beacon-program. Bist du zufällig ein Zwilling von Federer? – bili

+0

Sie könnten wahrscheinlich den Hinweis verwenden, den ich für Federer gelassen habe :) – bili

+0

@bili ich und er sind in der gleichen Klasse aha und sowohl ich als auch er haben versucht, aber haben keine wo :(. –

Antwort

2

versuchen Sie dies:

public class BelishaBeacon { 
    private static Timer timer; 
    public class Drawing extends JPanel { 

     private int x = 125; 
     private int y = 80; 
     private boolean changeColors = false; 


     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 

      Rectangle box1 = new Rectangle(165, 180, 20, 45); 
      Rectangle box2 = new Rectangle(165, 225, 20, 45); 
      Rectangle box3 = new Rectangle(165, 270, 20, 45); 
      Rectangle box4 = new Rectangle(165, 315, 20, 45); 
      Rectangle box5 = new Rectangle(165, 360, 20, 45); 
      Rectangle box6 = new Rectangle(165, 405, 20, 45); 


      Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100); 
      g2.draw(ball); 
      g2.draw(box1); 
      g2.draw(box2); 
      g2.draw(box3); 
      g2.draw(box4); 
      g2.draw(box5); 
      g2.draw(box6); 

      g2.setColor(Color.BLACK); 
      g2.fill(box1); 
      g2.fill(box3); 
      g2.fill(box5); 
      g2.setColor(Color.ORANGE); 
      g2.fill(ball); 
      // changeColors = !changeColors; 
      if (changeColors) { 
       g2.setColor(Color.lightGray); 
       g2.fill(new Ellipse2D.Double(x, y, 100, 100)); 
      } 
     } 




     public void changeColors() { 
      changeColors = !changeColors; 
      repaint(); 
     } 

     public boolean getChangeColors() { 
      return changeColors; 
     } 
    } 

    public BelishaBeacon() { 

     JFrame frame = new JFrame(); 
     frame.setSize(350, 570); 
     frame.setTitle("Belisha Beacon"); 
     frame.setLayout(new BorderLayout(0, 0)); 
     final Drawing shapes = new Drawing(); 

     timer = new Timer(500, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       //shapes.repaint(); 
       shapes.changeColors(); 
      } 
     }); 



     JButton jbtFlash = new JButton("Flash"); 
     jbtFlash.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Flashing"); 
       if (!timer.isRunning()) { 
        timer.start(); 
       } 

      } 
     }); 




     final JButton jbtSteady = new JButton("Steady"); 
     jbtSteady.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 

         timer.stop(); 
         if(shapes.getChangeColors()) { 
          shapes.changeColors(); 
         } 
        } 
       }); 


     JPanel controlPanel = new JPanel(); 
     controlPanel.setLayout(new GridLayout(1, 2, 0, 0)); 
     controlPanel.add(jbtFlash); 
     controlPanel.add(jbtSteady); 

     frame.add(controlPanel, BorderLayout.SOUTH); 
     frame.add(shapes); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new BelishaBeacon(); 
     timer.start(); 
    } 
} 
+0

Ich habe das ausprobiert, aber ich brauche das Beacon vor der Hand zu blinken und erst wenn ich auf den Steady Button klicke, wird es orange und wenn ich den Flash Button erneut klicke muss es wieder blinken. –

+0

Ich habe gerade den Code bearbeitet. Es funktioniert – Paul

+0

Ich habe es gerade jetzt versucht und es funktioniert. Oh, ich danke dir so sehr für deine Hilfe :). –

2

Statt in paintComponent()-Toggle die Farbe versucht, geben Drawing eine Color und es verwenden, den Ball zu machen:

private Color color = Color.lightGray; 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     … 
     g2.setColor(color); 
     g2.fill(ball); 
     … 
    } 

Make changeColors() tatsächlich Farben ändern:

public void changeColors() { 
     if (Color.orange.equals(color)) { 
      color = Color.lightGray; 
     } else { 
      color = Color.orange; 
     } 
     repaint(); 
    } 

Und ein makeSteady() Methode hinzufügen:

public void makeSteady() { 
    color = Color.orange; 
    repaint(); 
} 

Nun Aktion Ihre Timers nur shapes.changeColors() tun können, Ihre Flash- Handler-Taste können diese nur tun timer.restart() und Handler Ihre Stetig Taste kann nur tun, :

timer.stop(); 
shapes.makeSteady(); 

Vergessen Sie auch nicht invokeLater().

+0

vielen Dank: D. –