Cappuccino90

WackelSchloss

Apr 3rd, 2015
313
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.19 KB | None | 0 0
  1. package ep2_k_3;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.GridBagLayout;
  5.  
  6. import javax.swing.*;
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. public class WackelSchloss {
  12.    
  13.     boolean direction = true;
  14.     String[] combination = {"5","2","1","7","7","1"};
  15.     int i=0;
  16.     WackelThread thread1 = new WackelThread();
  17.     WackelThread2 thread2 = new WackelThread2();
  18.    
  19.     private JFrame frame;
  20.  
  21.     /**
  22.      * Launch the application.
  23.      */
  24.     public static void main(String[] args) {
  25.         EventQueue.invokeLater(new Runnable() {
  26.             public void run() {
  27.                 try {
  28.                     WackelSchloss window = new WackelSchloss();
  29.                     window.frame.setVisible(true);
  30.                     window.frame.setSize(200, 200);
  31.                 } catch (Exception e) {
  32.                     e.printStackTrace();
  33.                 }
  34.             }
  35.         });
  36.     }
  37.  
  38.     /**
  39.      * Create the application.
  40.      */
  41.     public WackelSchloss() {
  42.         initialize();
  43.     }
  44.  
  45.     /**
  46.      * Initialize the contents of the frame.
  47.      */
  48.     public void initialize() {
  49.         frame = new JFrame();
  50.         frame.setBounds(100, 100, 145, 147);
  51.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         GridBagLayout gridBagLayout = new GridBagLayout();
  53.         gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
  54.         gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
  55.         gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
  56.         gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  57.         frame.getContentPane().setLayout(gridBagLayout);
  58.        
  59.         JButton btn1 = new JButton("1");
  60.         GridBagConstraints gbc_btn1 = new GridBagConstraints();
  61.         gbc_btn1.insets = new Insets(0, 0, 5, 5);
  62.         gbc_btn1.gridx = 0;
  63.         gbc_btn1.gridy = 0;
  64.         frame.getContentPane().add(btn1, gbc_btn1);
  65.        
  66.         JButton btn2 = new JButton("2");
  67.         GridBagConstraints gbc_btn2 = new GridBagConstraints();
  68.         gbc_btn2.insets = new Insets(0, 0, 5, 5);
  69.         gbc_btn2.gridx = 1;
  70.         gbc_btn2.gridy = 0;
  71.         frame.getContentPane().add(btn2, gbc_btn2);
  72.        
  73.         JButton btn3 = new JButton("3");
  74.         GridBagConstraints gbc_btn3 = new GridBagConstraints();
  75.         gbc_btn3.insets = new Insets(0, 0, 5, 0);
  76.         gbc_btn3.gridx = 2;
  77.         gbc_btn3.gridy = 0;
  78.         frame.getContentPane().add(btn3, gbc_btn3);
  79.        
  80.         JButton btn0 = new JButton("0");
  81.         btn0.addActionListener(new ActionListener() {
  82.             public void actionPerformed(ActionEvent arg0) {
  83.             }
  84.         });
  85.         GridBagConstraints gbc_btn0 = new GridBagConstraints();
  86.         gbc_btn0.insets = new Insets(0, 0, 5, 5);
  87.         gbc_btn0.gridx = 0;
  88.         gbc_btn0.gridy = 1;
  89.         frame.getContentPane().add(btn0, gbc_btn0);
  90.        
  91.         JButton btn4 = new JButton("4");
  92.         GridBagConstraints gbc_btn4 = new GridBagConstraints();
  93.         gbc_btn4.insets = new Insets(0, 0, 5, 0);
  94.         gbc_btn4.gridx = 2;
  95.         gbc_btn4.gridy = 1;
  96.         frame.getContentPane().add(btn4, gbc_btn4);
  97.        
  98.         JButton btn9 = new JButton("9");
  99.         GridBagConstraints gbc_btn9 = new GridBagConstraints();
  100.         gbc_btn9.insets = new Insets(0, 0, 5, 5);
  101.         gbc_btn9.gridx = 0;
  102.         gbc_btn9.gridy = 2;
  103.         frame.getContentPane().add(btn9, gbc_btn9);
  104.        
  105.         JButton btn5 = new JButton("5");
  106.         GridBagConstraints gbc_btn5 = new GridBagConstraints();
  107.         gbc_btn5.insets = new Insets(0, 0, 5, 0);
  108.         gbc_btn5.gridx = 2;
  109.         gbc_btn5.gridy = 2;
  110.         frame.getContentPane().add(btn5, gbc_btn5);
  111.        
  112.         JButton btn8 = new JButton("8");
  113.         GridBagConstraints gbc_btn8 = new GridBagConstraints();
  114.         gbc_btn8.insets = new Insets(0, 0, 0, 5);
  115.         gbc_btn8.gridx = 0;
  116.         gbc_btn8.gridy = 3;
  117.         frame.getContentPane().add(btn8, gbc_btn8);
  118.        
  119.         JButton btn7 = new JButton("7");
  120.         GridBagConstraints gbc_btn7 = new GridBagConstraints();
  121.         gbc_btn7.insets = new Insets(0, 0, 0, 5);
  122.         gbc_btn7.gridx = 1;
  123.         gbc_btn7.gridy = 3;
  124.         frame.getContentPane().add(btn7, gbc_btn7);
  125.        
  126.         JButton btn6 = new JButton("6");
  127.         GridBagConstraints gbc_btn6 = new GridBagConstraints();
  128.         gbc_btn6.gridx = 2;
  129.         gbc_btn6.gridy = 3;
  130.         frame.getContentPane().add(btn6, gbc_btn6);
  131.        
  132.         // Zuweisen von ActionCommands zu den Buttons
  133.         btn0.setActionCommand("0");
  134.         btn1.setActionCommand("1");
  135.         btn2.setActionCommand("2");
  136.         btn3.setActionCommand("3");
  137.         btn4.setActionCommand("4");
  138.         btn5.setActionCommand("5");
  139.         btn6.setActionCommand("6");
  140.         btn7.setActionCommand("7");
  141.         btn8.setActionCommand("8");
  142.         btn9.setActionCommand("9");
  143.        
  144.        
  145.        
  146.         ActionListener listener = new ActionListener() {
  147.             @SuppressWarnings("deprecation")
  148.             @Override
  149.             public void actionPerformed(ActionEvent e) {
  150.                 //ActionCommand wird bei jedem Aufruf mit dem String Array gegengeprüft
  151.                 //in dem String Array ist der richtige Code gespeichert
  152.                 if (e.getActionCommand().equals(combination[i])) {
  153.                    
  154.                     if (i==5) {
  155.                         System.exit(0);
  156.                     }
  157.                     frame.getContentPane().setBackground(Color.green);
  158.                     i++;
  159.                 }
  160.                 else {
  161.                    
  162.                     i=0;
  163.                     frame.getContentPane().setBackground(Color.red);
  164.                     //zahlenRotieren(ba);
  165.                     if (direction==true) {
  166.                         System.out.println("direction true erreicht im listener");
  167.                         thread1.stop();
  168.                         thread2.start();
  169.                     }
  170.                     else {
  171.                             thread2.stop();
  172.                             thread1.start();           
  173.                     }
  174.                     direction = !direction;
  175.                 }
  176.             }
  177.         };
  178.        
  179.        
  180.         // Zuweisen von Actionlistenern zu den Buttons
  181.        
  182.         btn0.addActionListener(listener);
  183.         btn1.addActionListener(listener);
  184.         btn2.addActionListener(listener);
  185.         btn3.addActionListener(listener);
  186.         btn4.addActionListener(listener);
  187.         btn5.addActionListener(listener);
  188.         btn6.addActionListener(listener);
  189.         btn7.addActionListener(listener);
  190.         btn8.addActionListener(listener);
  191.         btn9.addActionListener(listener);
  192.        
  193.         JButton[] baa = {btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0};
  194.        
  195.         zahlenRotieren(baa);
  196.        
  197.         //zahlenRueckwaertsrotieren(ba);
  198.     }
  199.  
  200.     public void zahlenRotieren(JButton[] ba){
  201.         for (JButton b : ba ) {
  202.             if (b.getActionCommand().equals("0") && b.getText().equals("0")) {
  203.                 b.setActionCommand("1");
  204.                 b.setText("1");
  205.             }
  206.             else if (b.getActionCommand().equals("1") && b.getText().equals("1")) {
  207.                 b.setActionCommand("2");
  208.                 b.setText("2");
  209.             }
  210.             else if (b.getActionCommand().equals("2") && b.getText().equals("2")) {
  211.                 b.setActionCommand("3");
  212.                 b.setText("3");
  213.             }
  214.             else if (b.getActionCommand().equals("3") && b.getText().equals("3")) {
  215.                 b.setActionCommand("4");
  216.                 b.setText("4");
  217.             }
  218.             else if (b.getActionCommand().equals("4") && b.getText().equals("4")) {
  219.                 b.setActionCommand("5");
  220.                 b.setText("5");
  221.             }
  222.             else if (b.getActionCommand().equals("5") && b.getText().equals("5")) {
  223.                 b.setActionCommand("6");
  224.                 b.setText("6");
  225.             }
  226.             else if (b.getActionCommand().equals("6") && b.getText().equals("6")) {
  227.                 b.setActionCommand("7");
  228.                 b.setText("7");
  229.             }
  230.             else if (b.getActionCommand().equals("7") && b.getText().equals("7")) {
  231.                 b.setActionCommand("8");
  232.                 b.setText("8");
  233.             }
  234.             else if (b.getActionCommand().equals("8") && b.getText().equals("8")) {
  235.                 b.setActionCommand("9");
  236.                 b.setText("9");
  237.             }
  238.             else if (b.getActionCommand().equals("9") && b.getText().equals("9")) {
  239.                 b.setActionCommand("0");
  240.                 b.setText("0");
  241.             }
  242.             b.repaint();
  243.         }
  244.     }
  245.    
  246.     public void zahlenRueckwaertsrotieren(JButton[] ba){
  247.        
  248.         for (JButton b : ba ) {
  249.             if (b.getActionCommand().equals("2") && b.getText().equals("2")) {
  250.                 b.setActionCommand("1");
  251.                 b.setText("1");
  252.             }
  253.             else if (b.getActionCommand().equals("3") && b.getText().equals("3")) {
  254.                 b.setActionCommand("2");
  255.                 b.setText("2");
  256.             }
  257.             else if (b.getActionCommand().equals("4") && b.getText().equals("4")) {
  258.                 b.setActionCommand("3");
  259.                 b.setText("3");
  260.             }
  261.             else if (b.getActionCommand().equals("5") && b.getText().equals("5")) {
  262.                 b.setActionCommand("4");
  263.                 b.setText("4");
  264.             }
  265.             else if (b.getActionCommand().equals("6") && b.getText().equals("6")) {
  266.                 b.setActionCommand("5");
  267.                 b.setText("5");
  268.             }
  269.             else if (b.getActionCommand().equals("7") && b.getText().equals("7")) {
  270.                 b.setActionCommand("6");
  271.                 b.setText("6");
  272.             }
  273.             else if (b.getActionCommand().equals("8") && b.getText().equals("8")) {
  274.                 b.setActionCommand("7");
  275.                 b.setText("7");
  276.             }
  277.             else if (b.getActionCommand().equals("9") && b.getText().equals("9")) {
  278.                 b.setActionCommand("8");
  279.                 b.setText("8");
  280.             }
  281.             else if (b.getActionCommand().equals("0") && b.getText().equals("0")) {
  282.                 b.setActionCommand("9");
  283.                 b.setText("9");
  284.             }
  285.             else if (b.getActionCommand().equals("1") && b.getText().equals("1")) {
  286.                 b.setActionCommand("0");
  287.                 b.setText("0");
  288.             }
  289.             b.repaint();
  290.         }
  291.        
  292.     }
  293.    
  294. }
  295.  
  296.  
  297. package ep2_k_3;
  298.  
  299.  
  300. public class WackelThread extends Thread {
  301.  
  302.     @Override
  303.     public void run() {
  304.         super.run();
  305.         System.out.println("Thread 1 erreicht");
  306.         //Neue Fenster erstellen und sichtbar machen
  307.         WackelSchloss wackel = new WackelSchloss();
  308.        
  309.         // For Schleife für den Farbwechsel
  310.         while (true) {
  311.             try {
  312.                 // Eine Sekunde warten
  313.                 Thread.sleep(1000);
  314.            
  315.                 System.out.println("Sleep erreicht von thread1");
  316.                 wackel.zahlenRotieren(wackel.baa);
  317.             } catch (InterruptedException e) {
  318.                 e.printStackTrace();
  319.             }
  320.         }
  321.     }
  322.    
  323.    
  324.     private int buffer =-1;
  325.    
  326.     synchronized void send(int i) {
  327.         try {
  328.             while (buffer >= 0) wait();
  329.             buffer = i;
  330.             notify();  
  331.         } catch (InterruptedException e) {}
  332.     }
  333.    
  334.     synchronized int receive() {
  335.         int result = 0;
  336.         try {
  337.             while ((result=buffer)<0) wait();
  338.             buffer = -1;
  339.             notify();
  340.         } catch (InterruptedException e) {}
  341.         return result;
  342.     }  
  343. }
  344.  
  345.  
  346. package ep2_k_3;
  347.  
  348. public class WackelThread2 extends Thread {
  349.  
  350.     @Override
  351.     public void run() {
  352.         super.run();
  353.         //Neue Fenster erstellen und sichtbar machen
  354.         WackelSchloss wackel = new WackelSchloss();
  355.    
  356.         // For Schleife für den Farbwechsel
  357.         while (true) {
  358.             try {
  359.                 // Eine Sekunde warten
  360.                 Thread.sleep(1000);
  361.                 System.out.println("Thread 2 erreicht");
  362.                 wackel.zahlenRueckwaertsrotieren(wackel.baa);
  363.                
  364.             } catch (InterruptedException e) {
  365.                 e.printStackTrace();
  366.             }
  367.         }
  368.     }
  369.    
  370.     private int buffer =-1;
  371.    
  372.     synchronized void send(int i) {
  373.         try {
  374.             while (buffer >= 0) wait();
  375.             buffer = i;
  376.             notify();  
  377.         } catch (InterruptedException e) {}
  378.     }
  379.    
  380.     synchronized int receive() {
  381.         int result = 0;
  382.         try {
  383.             while ((result=buffer)<0) wait();
  384.             buffer = -1;
  385.             notify();
  386.         } catch (InterruptedException e) {}
  387.         return result;
  388.     }
  389. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment