Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ep2_k_3;
- import java.awt.EventQueue;
- import java.awt.GridBagLayout;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class WackelSchloss {
- boolean direction = true;
- String[] combination = {"5","2","1","7","7","1"};
- int i=0;
- WackelThread thread1 = new WackelThread();
- WackelThread2 thread2 = new WackelThread2();
- private JFrame frame;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- WackelSchloss window = new WackelSchloss();
- window.frame.setVisible(true);
- window.frame.setSize(200, 200);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public WackelSchloss() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- public void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 145, 147);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- GridBagLayout gridBagLayout = new GridBagLayout();
- gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
- gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
- gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
- gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
- frame.getContentPane().setLayout(gridBagLayout);
- JButton btn1 = new JButton("1");
- GridBagConstraints gbc_btn1 = new GridBagConstraints();
- gbc_btn1.insets = new Insets(0, 0, 5, 5);
- gbc_btn1.gridx = 0;
- gbc_btn1.gridy = 0;
- frame.getContentPane().add(btn1, gbc_btn1);
- JButton btn2 = new JButton("2");
- GridBagConstraints gbc_btn2 = new GridBagConstraints();
- gbc_btn2.insets = new Insets(0, 0, 5, 5);
- gbc_btn2.gridx = 1;
- gbc_btn2.gridy = 0;
- frame.getContentPane().add(btn2, gbc_btn2);
- JButton btn3 = new JButton("3");
- GridBagConstraints gbc_btn3 = new GridBagConstraints();
- gbc_btn3.insets = new Insets(0, 0, 5, 0);
- gbc_btn3.gridx = 2;
- gbc_btn3.gridy = 0;
- frame.getContentPane().add(btn3, gbc_btn3);
- JButton btn0 = new JButton("0");
- btn0.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- }
- });
- GridBagConstraints gbc_btn0 = new GridBagConstraints();
- gbc_btn0.insets = new Insets(0, 0, 5, 5);
- gbc_btn0.gridx = 0;
- gbc_btn0.gridy = 1;
- frame.getContentPane().add(btn0, gbc_btn0);
- JButton btn4 = new JButton("4");
- GridBagConstraints gbc_btn4 = new GridBagConstraints();
- gbc_btn4.insets = new Insets(0, 0, 5, 0);
- gbc_btn4.gridx = 2;
- gbc_btn4.gridy = 1;
- frame.getContentPane().add(btn4, gbc_btn4);
- JButton btn9 = new JButton("9");
- GridBagConstraints gbc_btn9 = new GridBagConstraints();
- gbc_btn9.insets = new Insets(0, 0, 5, 5);
- gbc_btn9.gridx = 0;
- gbc_btn9.gridy = 2;
- frame.getContentPane().add(btn9, gbc_btn9);
- JButton btn5 = new JButton("5");
- GridBagConstraints gbc_btn5 = new GridBagConstraints();
- gbc_btn5.insets = new Insets(0, 0, 5, 0);
- gbc_btn5.gridx = 2;
- gbc_btn5.gridy = 2;
- frame.getContentPane().add(btn5, gbc_btn5);
- JButton btn8 = new JButton("8");
- GridBagConstraints gbc_btn8 = new GridBagConstraints();
- gbc_btn8.insets = new Insets(0, 0, 0, 5);
- gbc_btn8.gridx = 0;
- gbc_btn8.gridy = 3;
- frame.getContentPane().add(btn8, gbc_btn8);
- JButton btn7 = new JButton("7");
- GridBagConstraints gbc_btn7 = new GridBagConstraints();
- gbc_btn7.insets = new Insets(0, 0, 0, 5);
- gbc_btn7.gridx = 1;
- gbc_btn7.gridy = 3;
- frame.getContentPane().add(btn7, gbc_btn7);
- JButton btn6 = new JButton("6");
- GridBagConstraints gbc_btn6 = new GridBagConstraints();
- gbc_btn6.gridx = 2;
- gbc_btn6.gridy = 3;
- frame.getContentPane().add(btn6, gbc_btn6);
- // Zuweisen von ActionCommands zu den Buttons
- btn0.setActionCommand("0");
- btn1.setActionCommand("1");
- btn2.setActionCommand("2");
- btn3.setActionCommand("3");
- btn4.setActionCommand("4");
- btn5.setActionCommand("5");
- btn6.setActionCommand("6");
- btn7.setActionCommand("7");
- btn8.setActionCommand("8");
- btn9.setActionCommand("9");
- ActionListener listener = new ActionListener() {
- @SuppressWarnings("deprecation")
- @Override
- public void actionPerformed(ActionEvent e) {
- //ActionCommand wird bei jedem Aufruf mit dem String Array gegengeprüft
- //in dem String Array ist der richtige Code gespeichert
- if (e.getActionCommand().equals(combination[i])) {
- if (i==5) {
- System.exit(0);
- }
- frame.getContentPane().setBackground(Color.green);
- i++;
- }
- else {
- i=0;
- frame.getContentPane().setBackground(Color.red);
- //zahlenRotieren(ba);
- if (direction==true) {
- System.out.println("direction true erreicht im listener");
- thread1.stop();
- thread2.start();
- }
- else {
- thread2.stop();
- thread1.start();
- }
- direction = !direction;
- }
- }
- };
- // Zuweisen von Actionlistenern zu den Buttons
- btn0.addActionListener(listener);
- btn1.addActionListener(listener);
- btn2.addActionListener(listener);
- btn3.addActionListener(listener);
- btn4.addActionListener(listener);
- btn5.addActionListener(listener);
- btn6.addActionListener(listener);
- btn7.addActionListener(listener);
- btn8.addActionListener(listener);
- btn9.addActionListener(listener);
- JButton[] baa = {btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0};
- zahlenRotieren(baa);
- //zahlenRueckwaertsrotieren(ba);
- }
- public void zahlenRotieren(JButton[] ba){
- for (JButton b : ba ) {
- if (b.getActionCommand().equals("0") && b.getText().equals("0")) {
- b.setActionCommand("1");
- b.setText("1");
- }
- else if (b.getActionCommand().equals("1") && b.getText().equals("1")) {
- b.setActionCommand("2");
- b.setText("2");
- }
- else if (b.getActionCommand().equals("2") && b.getText().equals("2")) {
- b.setActionCommand("3");
- b.setText("3");
- }
- else if (b.getActionCommand().equals("3") && b.getText().equals("3")) {
- b.setActionCommand("4");
- b.setText("4");
- }
- else if (b.getActionCommand().equals("4") && b.getText().equals("4")) {
- b.setActionCommand("5");
- b.setText("5");
- }
- else if (b.getActionCommand().equals("5") && b.getText().equals("5")) {
- b.setActionCommand("6");
- b.setText("6");
- }
- else if (b.getActionCommand().equals("6") && b.getText().equals("6")) {
- b.setActionCommand("7");
- b.setText("7");
- }
- else if (b.getActionCommand().equals("7") && b.getText().equals("7")) {
- b.setActionCommand("8");
- b.setText("8");
- }
- else if (b.getActionCommand().equals("8") && b.getText().equals("8")) {
- b.setActionCommand("9");
- b.setText("9");
- }
- else if (b.getActionCommand().equals("9") && b.getText().equals("9")) {
- b.setActionCommand("0");
- b.setText("0");
- }
- b.repaint();
- }
- }
- public void zahlenRueckwaertsrotieren(JButton[] ba){
- for (JButton b : ba ) {
- if (b.getActionCommand().equals("2") && b.getText().equals("2")) {
- b.setActionCommand("1");
- b.setText("1");
- }
- else if (b.getActionCommand().equals("3") && b.getText().equals("3")) {
- b.setActionCommand("2");
- b.setText("2");
- }
- else if (b.getActionCommand().equals("4") && b.getText().equals("4")) {
- b.setActionCommand("3");
- b.setText("3");
- }
- else if (b.getActionCommand().equals("5") && b.getText().equals("5")) {
- b.setActionCommand("4");
- b.setText("4");
- }
- else if (b.getActionCommand().equals("6") && b.getText().equals("6")) {
- b.setActionCommand("5");
- b.setText("5");
- }
- else if (b.getActionCommand().equals("7") && b.getText().equals("7")) {
- b.setActionCommand("6");
- b.setText("6");
- }
- else if (b.getActionCommand().equals("8") && b.getText().equals("8")) {
- b.setActionCommand("7");
- b.setText("7");
- }
- else if (b.getActionCommand().equals("9") && b.getText().equals("9")) {
- b.setActionCommand("8");
- b.setText("8");
- }
- else if (b.getActionCommand().equals("0") && b.getText().equals("0")) {
- b.setActionCommand("9");
- b.setText("9");
- }
- else if (b.getActionCommand().equals("1") && b.getText().equals("1")) {
- b.setActionCommand("0");
- b.setText("0");
- }
- b.repaint();
- }
- }
- }
- package ep2_k_3;
- public class WackelThread extends Thread {
- @Override
- public void run() {
- super.run();
- System.out.println("Thread 1 erreicht");
- //Neue Fenster erstellen und sichtbar machen
- WackelSchloss wackel = new WackelSchloss();
- // For Schleife für den Farbwechsel
- while (true) {
- try {
- // Eine Sekunde warten
- Thread.sleep(1000);
- System.out.println("Sleep erreicht von thread1");
- wackel.zahlenRotieren(wackel.baa);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private int buffer =-1;
- synchronized void send(int i) {
- try {
- while (buffer >= 0) wait();
- buffer = i;
- notify();
- } catch (InterruptedException e) {}
- }
- synchronized int receive() {
- int result = 0;
- try {
- while ((result=buffer)<0) wait();
- buffer = -1;
- notify();
- } catch (InterruptedException e) {}
- return result;
- }
- }
- package ep2_k_3;
- public class WackelThread2 extends Thread {
- @Override
- public void run() {
- super.run();
- //Neue Fenster erstellen und sichtbar machen
- WackelSchloss wackel = new WackelSchloss();
- // For Schleife für den Farbwechsel
- while (true) {
- try {
- // Eine Sekunde warten
- Thread.sleep(1000);
- System.out.println("Thread 2 erreicht");
- wackel.zahlenRueckwaertsrotieren(wackel.baa);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private int buffer =-1;
- synchronized void send(int i) {
- try {
- while (buffer >= 0) wait();
- buffer = i;
- notify();
- } catch (InterruptedException e) {}
- }
- synchronized int receive() {
- int result = 0;
- try {
- while ((result=buffer)<0) wait();
- buffer = -1;
- notify();
- } catch (InterruptedException e) {}
- return result;
- }
- }
Advertisement