Problem bei Java Doodle Jump


Funresort

Erfahrenes Mitglied
Hi Leute,

die Physik des dritten Threads funktioniert leider nur teilweise hat da jemand ne Idee****? BIn grad am verzweifeln :(
Java:
package test;

import java.awt.Color;

public class Test {

	private static JFrame frmJump;
	JLabel label;
	JLabel jumper;
	JLabel background;
	JLabel background2;
	Thread t2;
	Thread t;
	Thread t3;
	JButton start;
	private int count = 0;
	private JButton exit;
	private JButton b6;
	private JButton b7;
	private JButton b8;
	private JButton b9;
	private JButton b11;
	private boolean checka = false;
	private boolean checkd = false;
	private boolean checkw = false;
	private JButton b10;
	JButton btnNewGame;
	public static boolean checkup = false;
	private JButton b12;
	JButton b1;
	JButton b2;
	JButton b3;
	JButton b4;
	JButton b5;
	int yPosb1 = 00;
	int yPosb2 = 60;
	int yPosb3 = 80;
	int yPosb4 = 100;
	int points = 0;
	int yPosb5 = 140;
	int yPosb6 = 160;
	int yPosb7 = 180;
	int yPosb8 = 220;
	int yPosb9 = 240;
	int yPosb10 = 260;
	int yPosb11 = 290;
	int yPosb12 = 20;
	int xPosb1 = 48;
	int xPosb2 = 171;
	int xPosb3 = 304;
	int xPosb4 = 97;
	int xPosb5 = 239;
	int xPosb6 = 171;
	int xPosb7 = 27;
	int xPosb8 = 329;
	int xPosb9 = 259;
	int xPosb10 = 342;
	int xPosb11 = 115;
	int xPosb12 = 10;
	int jumpyPos = 40;
	JPanel panel;
	int jumpxPos = 210;
	private JTextField txtVerloren;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@SuppressWarnings("static-access")
			public void run() {
				try {
					Test window = new Test();
					window.frmJump.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * Create the application.
	 */
	public Test() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frmJump = new JFrame();
		frmJump.setTitle("Test");
		frmJump.setUndecorated(true);
		

		frmJump.setBounds(100, 100, 450, 300);
		frmJump.setLocationRelativeTo(null);
		frmJump.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmJump.getContentPane().setLayout(null);

		panel = new JPanel();
		panel.setBackground(Color.BLUE);
		panel.setOpaque(true);
		panel.setBounds(0, 0, 450, 300);
		frmJump.getContentPane().add(panel);
		panel.setLayout(null);

		start = new JButton("Start");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (count == 0) {
					startThreads();
					count++;
					start.setVisible(false);

				}
			}
		});

		txtVerloren = new JTextField();
		txtVerloren.setVisible(false);

		exit = new JButton("X");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		exit.setBorder(null);
		exit.setBackground(Color.GREEN);
		exit.setForeground(Color.BLACK);
		exit.setBounds(429, 0, 21, 23);
		panel.add(exit);
		txtVerloren.setFont(new Font("SansSerif", Font.BOLD, 14));
		txtVerloren.setHorizontalAlignment(SwingConstants.CENTER);
		txtVerloren.setForeground(Color.RED);
		txtVerloren.setEditable(false);
		txtVerloren.setText("Verloren!");
		txtVerloren.setBounds(150, 100, 122, 28);
		panel.add(txtVerloren);
		txtVerloren.setColumns(10);
		start.setBounds(163, 50, 91, 28);
		panel.add(start);

		btnNewGame = new JButton("New Game?");
		btnNewGame.addActionListener(new ActionListener() {
			@SuppressWarnings({ "static-access", "deprecation" })
			public void actionPerformed(ActionEvent arg0) {
				t.stop();
				t.interrupt();
				t2.stop();
				t2.interrupt();
				t3.stop();
				t3.interrupt();
				frmJump.dispose();

				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							Test window = new Test();
							window.frmJump.setVisible(true);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		btnNewGame.setBounds(160, 162, 103, 28);
		btnNewGame.setVisible(false);
		panel.add(btnNewGame);
		panel.setFocusCycleRoot(true);
		panel.setFocusTraversalPolicyProvider(true);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checkd = false;
				checkw = false;
				checka = true;
			}

		}, KeyStroke.getKeyStroke('a'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checka = false;
				checkw = false;
				checkd = true;
			}
		}, KeyStroke.getKeyStroke('d'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checka = false;
				checkd = false;
				checkw = true;
			}
		}, KeyStroke.getKeyStroke('w'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		jumper = new JLabel("I");
		jumper.setForeground(Color.WHITE);
		jumper.setFont(new Font("Tahoma", Font.BOLD, 15));
		jumper.setBounds(210, 40, 9, 20);
		jumper.setVisible(false);
		panel.add(jumper);

		b1 = new JButton("");
		b1.setEnabled(false);
		b1.setBorderPainted(false);
		b1.setBounds(48, 250, 56, 14);
		panel.add(b1);

		b2 = new JButton("");
		b2.setEnabled(false);
		b2.setBorderPainted(false);
		b2.setBounds(171, 250, 56, 14);
		panel.add(b2);

		b3 = new JButton("");
		b3.setEnabled(false);
		b3.setBorderPainted(false);
		b3.setBounds(304, 250, 56, 14);
		panel.add(b3);

		b4 = new JButton("");
		b4.setEnabled(false);
		b4.setBorderPainted(false);
		b4.setBounds(97, 205, 56, 14);
		panel.add(b4);

		b5 = new JButton("");
		b5.setEnabled(false);
		b5.setBorderPainted(false);
		b5.setBounds(239, 191, 56, 14);
		panel.add(b5);

		b6 = new JButton("");
		b6.setEnabled(false);
		b6.setBorderPainted(false);
		b6.setBounds(171, 136, 56, 14);
		panel.add(b6);

		b7 = new JButton("");
		b7.setEnabled(false);
		b7.setBorderPainted(false);
		b7.setBounds(27, 89, 56, 14);
		panel.add(b7);

		b8 = new JButton("");
		b8.setEnabled(false);
		b8.setBorderPainted(false);
		b8.setBounds(329, 151, 56, 14);
		panel.add(b8);

		b9 = new JButton("");
		b9.setEnabled(false);
		b9.setBorderPainted(false);
		b9.setBounds(259, 84, 56, 14);
		panel.add(b9);

		b12 = new JButton("");
		b12.setEnabled(false);
		b12.setBorderPainted(false);
		b12.setBounds(10, 9, 56, 14);
		panel.add(b12);

		background = new JLabel();
		background.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background.setBounds(0, 0, 800, 600);
		panel.add(background);

		background2 = new JLabel();
		background2.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background2.setBounds(0, -600, 800, 600);
		panel.add(background2);

		JLabel background3 = new JLabel();
		background3.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background3.setBounds(0, 0, 800, 600);
		panel.add(background3);

		b11 = new JButton("");
		b11.setEnabled(false);
		b11.setBorderPainted(false);
		b11.setBounds(115, 47, 56, 14);
		panel.add(b11);

		b10 = new JButton("");
		b10.setEnabled(false);
		b10.setBorderPainted(false);
		b10.setBounds(342, 21, 56, 14);
		panel.add(b10);
	}

	public void startThreads() {

		t = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					while (checka) {
						jumpxPos--;
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					while (checkd) {
						jumpxPos++;
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					while (checkw) {
					}
				}

			}

		});

		t2 = new Thread(new Runnable() {
			public void run() {
				
				
				int counti = 0;
				int checki = 0;
				int counti2 = -600;
				int speed = 20;
				int counter2 = 0;
				boolean check = false;
				Random r = new Random();
				jumper.setVisible(true);

				while (!check) {

					if (jumper.getLocation().y == 280) {
						check = true;
					}

					if (checki == 25){
						checkup = false;
						checki = 0;
					}
					
					
					if (checkup) {
						jumpyPos = jumpyPos - 4;
						checki++;
					} else {
						jumpyPos = jumpyPos + 4;
					}
					jumper.setLocation(jumpxPos, jumpyPos);
					count++;
					if (counti == 600) {
						counti = -600;
						background.setLocation(0, counti);
					}
					if (counti2 == 600) {
						counti2 = -600;
						background2.setLocation(0, counti2);
					}

					background.setLocation(0, counti);
					background2.setLocation(0, counti2);

					b1.setLocation(xPosb1, yPosb1);
					b2.setLocation(xPosb2, yPosb2);
					b4.setLocation(xPosb4, yPosb4);
					b5.setLocation(xPosb5, yPosb5);
					b7.setLocation(xPosb7, yPosb7);
					b8.setLocation(xPosb8, yPosb8);
					b10.setLocation(xPosb10, yPosb10);
					b11.setLocation(xPosb11, yPosb11);

					if (points >= 1000) {

						if (b3.getLocation().y == 0) {
							b3.setVisible(false);
						} else {
							b3.setLocation(xPosb3, yPosb3);
							yPosb3++;
						}

						if (b6.getLocation().y == 0) {
							b6.setVisible(false);
						} else {
							b6.setLocation(xPosb6, yPosb6);
							yPosb6++;
						}

						if (b9.getLocation().y == 0) {
							b9.setVisible(false);
						} else {
							b9.setLocation(xPosb9, yPosb9);
							yPosb9++;
						}

						if (b12.getLocation().y == 0) {
							b12.setVisible(false);
						} else {
							b12.setLocation(xPosb12, yPosb12);
							yPosb12++;
						}

					} else {

						b3.setLocation(xPosb3, yPosb3);
						yPosb3++;

						b6.setLocation(xPosb6, yPosb6);
						yPosb6++;

						b9.setLocation(xPosb9, yPosb9);
						yPosb9++;

						b12.setLocation(xPosb12, yPosb12);
						yPosb12++;
					}

					counti++;
					counti2++;
					points++;
					counter2++;
					yPosb1++;
					yPosb2++;
					yPosb4++;
					yPosb5++;
					yPosb7++;
					yPosb8++;
					yPosb10++;
					yPosb11++;

					if (yPosb1 == 300) {
						yPosb1 = 0;
						xPosb1 = r.nextInt(394);
					} else if (yPosb2 == 300) {
						yPosb2 = 0;
						xPosb2 = r.nextInt(394);
					} else if (yPosb3 == 300) {
						yPosb3 = 0;
						xPosb3 = r.nextInt(394);
					} else if (yPosb4 == 300) {
						yPosb4 = 0;
						xPosb4 = r.nextInt(394);
					} else if (yPosb5 == 300) {
						yPosb5 = 0;
						xPosb5 = r.nextInt(394);
					} else if (yPosb6 == 300) {
						yPosb6 = 0;
						xPosb6 = r.nextInt(394);
					} else if (yPosb7 == 300) {
						yPosb7 = 0;
						xPosb7 = r.nextInt(394);
					} else if (yPosb8 == 300) {
						yPosb8 = 0;
						xPosb8 = r.nextInt(394);
					} else if (yPosb9 == 300) {
						yPosb9 = 0;
						xPosb9 = r.nextInt(394);
					} else if (yPosb10 == 300) {
						yPosb10 = 0;
						xPosb10 = r.nextInt(394);
					} else if (yPosb11 == 300) {
						yPosb11 = 0;
						xPosb11 = r.nextInt(394);
					} else if (yPosb12 == 300) {
						yPosb12 = 0;
						xPosb12 = r.nextInt(394);
					}

					if (counter2 == 100) {
						if (speed != 5) {
							speed--;
							counter2 = 0;
						}
					}

					try {
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				btnNewGame.setVisible(true);
				txtVerloren.setText(Integer.toString(points) + " Points");
				txtVerloren.setVisible(true);

			}

		});

		t3 = new Thread(new Runnable() {
			

			@Override
			public void run() {
				
				while (jumpyPos != 0) {
					try {
						Thread.sleep(4);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					if (jumpyPos == yPosb1 && jumpxPos >= xPosb1
							&& jumpxPos <= b1.getSize().width+xPosb1) {
						checkup = true;
					}
					if (jumpyPos == yPosb2 && jumpxPos >= xPosb2
							&& jumpxPos <= b2.getSize().width+xPosb2) {
						checkup = true;
					}
					if (jumpyPos == yPosb3 && jumpxPos >= xPosb3
							&& jumpxPos <= b3.getSize().width+xPosb3) {
						checkup = true;
					}
					if (jumpyPos == yPosb4 && jumpxPos >= xPosb4
							&& jumpxPos <= b4.getSize().width+xPosb4) {
						checkup = true;
					}
					if (jumpyPos == yPosb5 && jumpxPos >= xPosb5
							&& jumpxPos <= b5.getSize().width+xPosb5) {
						checkup = true;
					}
					if (jumpyPos == yPosb6 && jumpxPos >= xPosb6
							&& jumpxPos <= b6.getSize().width+xPosb6) {
						checkup = true;
					}
					if (jumpyPos == yPosb7 && jumpxPos >= xPosb7
							&& jumpxPos <=  b7.getSize().width+xPosb7) {
						checkup = true;
					}
					if (jumpyPos == yPosb8 && jumpxPos >= xPosb8
							&& jumpxPos <=  b8.getSize().width+xPosb8) {
						checkup = true;
					}
					if (jumpyPos == yPosb9 && jumpxPos >= xPosb9
							&& jumpxPos <=  b9.getSize().width+xPosb9) {
						checkup = true;
					}
					if (jumpyPos == yPosb10 && jumpxPos >= xPosb10
							&& jumpxPos <=  b10.getSize().width+xPosb10) {
						checkup = true;
					}
					if (jumpyPos == yPosb11 && jumpxPos >= xPosb11
							&& jumpxPos <=  b11.getSize().width+xPosb11) {
						checkup = true;
					}
					if (jumpyPos == yPosb12 && jumpxPos >= xPosb12
							&& jumpxPos <=  b12.getSize().width+xPosb12) {
						checkup = true;
					}

				}

			}

		});

		t.start();
		t2.start();
		t3.start();

	}
}

Gruß Chris
 
Zuletzt bearbeitet:

HonniCilest

Erfahrenes Mitglied
Darf ich dir ein paar allgemeine Tipps geben? In erster Linie den Code übersichtlicher, leichter anpassbar und leichter verständlich zu machen. Das sieht professioneller aus und erhöht die Wahrscheinlichkeit, dass sich Leute mit dem Thema beschäftigen.

1)
Java:
b1 = new JButton("");
        b1.setEnabled(false);
        b1.setBorderPainted(false);
        b1.setBounds(48, 250, 56, 14);
        panel.add(b1);

Das Ganze machst du 12 mal. Hast du mal ein Methode gedacht, die dir den fertigen Button zurückliefert (die Position übergibst du als Parameter) oder zumindest Konstanten für 56 und 14? Weil angenommen du willst den Button nun größer machen oder eine andere Eigenschaft hinzufügen/ändern/entfernen, dann musst du alles 12 mal ändern. Gleichzeitig würde durch die Funktion der Code ein wenig schmaler werden.

Ähnliches gilt für deinen Hintergrund.

2)
Java:
   int yPosb1 = 00;
    int yPosb2 = 60;
Eine solche Definition hast du glaube ich auch 13 mal? Hast du mal an http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Point.html gedacht? Ein Point-Objekt kann auch an setLocation übergeben werden. Außerdem kannst du statt z.B. y++ dir die translate-Methode zu Nutzen machen. Das wäre sicherlich auch für die Abbildung von Geschwindigkeit interessant. Hier sehe ich im gesamten jede Menge Zeilen, die du dir einsparen kannst.

3)
Soweit ich das sehe arbeitest du mit Timer-artigen gebilden, hast du mal darüber nachgedacht tatsächlich einen Timer zu nehmen?
http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/Timer.html

4)
funktioniert auch so weit, bis auf das meine Figur sobald sie einen Button berührt immer noch weiter fällt, hättet ihr ne Idee worans liegen könnte?

Ist t3 dafür zuständig?

Java:
if (jumpyPos == yPosb2 && jumpxPos == xPosb2
                            && jumpxPos <= b2.getSize().width - xPosb2) {
                        checkup = true;
                    }
Auf dem ersten Blick sieht es danach aus, als würde es nur für die linke Ecke funktionieren, wegen dem == bei x. Bei b1 hast du ein >=, funktioniert es da vielleicht? btw hier würde ich auch eher if-else nehmen, denn die Figur kollidiert wohl kaum mit allen Ebenen.

Nachtrag: Ist es eventuell möglich, dass der Thread den Zeitpunkt verpasst, indem die y-Positionen den gleichen Wert besitzen?

5)
Mir ist nicht Klar warum für die Ebenen Buttons verwendet werden, man klickt doch garnicht darauf?

Edit: Manche Aussagen von mir sind nun hinfällig, da deinen Beitrag editiert hattest :)
 
Zuletzt bearbeitet:

Funresort

Erfahrenes Mitglied
das Problem besteht leider weiterhin :(

ich find die buttons passen vom design her perfekt ;=)

ich schau mir ma die Timer Klasse an danke :)
 

Funresort

Erfahrenes Mitglied
Update

Physik funktioniert jetzt großteils, da ich meinen physik thread als methode in meinen 2 thread integriert habe.
Nur leider spinnt die Physik noch nen Wing, hat da jemand ne Idee, wieso meine physik als erfolgreich angezeigt wird, obwohl ich keinen button berühre?

Java:
package test;

import java.awt.Color;

public class Test {

	private static JFrame frmJump;
	JLabel label;
	JLabel jumper;
	JLabel background;
	JLabel background2;
	Thread t2;
	Thread t;
	Thread t3;
	JButton start;
	private int count = 0;
	private JButton exit;
	private JButton b6;
	private JButton b7;
	private JButton b8;
	private JButton b9;
	private JButton b11;
	private boolean checka = false;
	private boolean checkd = false;
	private boolean checkw = false;
	private JButton b10;
	JButton btnNewGame;
	public static int checkup = 55;
	private JButton b12;
	JButton b1;
	JButton b2;
	JButton b3;
	JButton b4;
	JButton b5;
	int yPosb1 = 00;
	int yPosb2 = 60;
	int yPosb3 = 80;
	int yPosb4 = 100;
	int points = 0;
	int yPosb5 = 140;
	int yPosb6 = 160;
	int yPosb7 = 180;
	int yPosb8 = 220;
	int yPosb9 = 240;
	int yPosb10 = 260;
	int yPosb11 = 290;
	int yPosb12 = 20;
	int xPosb1 = 48;
	int xPosb2 = 171;
	int xPosb3 = 304;
	int xPosb4 = 97;
	int xPosb5 = 239;
	boolean physic = false;
	boolean running = false;
	int xPosb6 = 171;
	int xPosb7 = 27;
	int xPosb8 = 329;
	int xPosb9 = 259;
	int xPosb10 = 342;
	int xPosb11 = 115;
	int xPosb12 = 10;
	int jumpyPos = 20;
	JPanel panel;
	int jumpxPos = 210;
	private JTextField txtVerloren;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@SuppressWarnings("static-access")
			public void run() {
				try {
					Test window = new Test();
					window.frmJump.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * Create the application.
	 */
	public Test() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frmJump = new JFrame();
		frmJump.setTitle("Test");
		frmJump.setUndecorated(true);

		frmJump.setBounds(100, 100, 450, 300);
		frmJump.setLocationRelativeTo(null);
		frmJump.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmJump.getContentPane().setLayout(null);

		panel = new JPanel();
		panel.setBackground(Color.BLUE);
		panel.setOpaque(true);
		panel.setBounds(0, 0, 450, 300);
		frmJump.getContentPane().add(panel);
		panel.setLayout(null);

		start = new JButton("Start");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (count == 0) {
					startThreads();
					count++;
					start.setVisible(false);

				}
			}
		});

		txtVerloren = new JTextField();
		txtVerloren.setVisible(false);

		exit = new JButton("X");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		exit.setBorder(null);
		exit.setBackground(Color.GREEN);
		exit.setForeground(Color.BLACK);
		exit.setBounds(429, 0, 21, 23);
		panel.add(exit);
		txtVerloren.setFont(new Font("SansSerif", Font.BOLD, 14));
		txtVerloren.setHorizontalAlignment(SwingConstants.CENTER);
		txtVerloren.setForeground(Color.RED);
		txtVerloren.setEditable(false);
		txtVerloren.setText("Verloren!");
		txtVerloren.setBounds(150, 100, 122, 28);
		panel.add(txtVerloren);
		txtVerloren.setColumns(10);
		start.setBounds(163, 50, 91, 28);
		panel.add(start);

		btnNewGame = new JButton("New Game?");
		btnNewGame.addActionListener(new ActionListener() {
			@SuppressWarnings({ "static-access", "deprecation" })
			public void actionPerformed(ActionEvent arg0) {
				try {
					t.stop();
					t.interrupt();
					t2.stop();
					t2.interrupt();
					t3.stop();
					t3.interrupt();
					frmJump.dispose();
				} catch (Exception e) {

				}
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							Test window = new Test();
							window.frmJump.setVisible(true);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		btnNewGame.setBounds(160, 162, 103, 28);
		btnNewGame.setVisible(false);
		panel.add(btnNewGame);
		panel.setFocusCycleRoot(true);
		panel.setFocusTraversalPolicyProvider(true);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checkd = false;
				checkw = false;
				checka = true;
			}

		}, KeyStroke.getKeyStroke('a'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checka = false;
				checkw = false;
				checkd = true;
			}
		}, KeyStroke.getKeyStroke('d'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		panel.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				checka = false;
				checkd = false;
				checkw = true;
			}
		}, KeyStroke.getKeyStroke('w'), JComponent.WHEN_IN_FOCUSED_WINDOW);

		jumper = new JLabel("I");
		jumper.setForeground(Color.WHITE);
		jumper.setFont(new Font("Tahoma", Font.BOLD, 15));
		jumper.setBounds(210, 40, 9, 20);
		jumper.setVisible(false);
		panel.add(jumper);

		b1 = new JButton("");
		b1.setEnabled(false);
		b1.setBorderPainted(false);
		b1.setBounds(48, 250, 56, 14);
		panel.add(b1);

		b2 = new JButton("");
		b2.setEnabled(false);
		b2.setBorderPainted(false);
		b2.setBounds(171, 250, 56, 14);
		panel.add(b2);

		b3 = new JButton("");
		b3.setEnabled(false);
		b3.setBorderPainted(false);
		b3.setBounds(304, 250, 56, 14);
		panel.add(b3);

		b4 = new JButton("");
		b4.setEnabled(false);
		b4.setBorderPainted(false);
		b4.setBounds(97, 205, 56, 14);
		panel.add(b4);

		b5 = new JButton("");
		b5.setEnabled(false);
		b5.setBorderPainted(false);
		b5.setBounds(239, 191, 56, 14);
		panel.add(b5);

		b6 = new JButton("");
		b6.setEnabled(false);
		b6.setBorderPainted(false);
		b6.setBounds(171, 136, 56, 14);
		panel.add(b6);

		b7 = new JButton("");
		b7.setEnabled(false);
		b7.setBorderPainted(false);
		b7.setBounds(27, 89, 56, 14);
		panel.add(b7);

		b8 = new JButton("");
		b8.setEnabled(false);
		b8.setBorderPainted(false);
		b8.setBounds(329, 151, 56, 14);
		panel.add(b8);

		b9 = new JButton("");
		b9.setEnabled(false);
		b9.setBorderPainted(false);
		b9.setBounds(259, 84, 56, 14);
		panel.add(b9);

		b12 = new JButton("");
		b12.setEnabled(false);
		b12.setBorderPainted(false);
		b12.setBounds(10, 9, 56, 14);
		panel.add(b12);

		background = new JLabel();
		background.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background.setBounds(0, 0, 800, 600);
		panel.add(background);

		background2 = new JLabel();
		background2.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background2.setBounds(0, -600, 800, 600);
		panel.add(background2);

		JLabel background3 = new JLabel();
		background3.setIcon(new ImageIcon(Test.class
				.getResource("/Community/background.png")));
		background3.setBounds(0, 0, 800, 600);
		panel.add(background3);

		b11 = new JButton("");
		b11.setEnabled(false);
		b11.setBorderPainted(false);
		b11.setBounds(115, 47, 56, 14);
		panel.add(b11);

		b10 = new JButton("");
		b10.setEnabled(false);
		b10.setBorderPainted(false);
		b10.setBounds(342, 21, 56, 14);
		panel.add(b10);
	}

	public void startThreads() {

		t = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					while (checka) {
						jumpxPos--;
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					while (checkd) {
						jumpxPos++;
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					while (checkw) {
					}
				}

			}

		});

		t2 = new Thread(new Runnable() {

			public void physic() {

				if (jumper.getLocation().y <= b1.getLocation().y && jumper.getLocation().x  >= b1.getLocation().x
						&& jumper.getLocation().x <= b1.getSize().width + b1.getLocation().x
						&& jumper.getLocation().y >= b1.getLocation().y - b1.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b2.getLocation().y && jumper.getLocation().x  >= b2.getLocation().x
						&& jumper.getLocation().x <= b2.getSize().width + b2.getLocation().x
						&& jumper.getLocation().y >= b2.getLocation().y - b2.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b3.getLocation().y && jumper.getLocation().x  >= b3.getLocation().x
						&& jumper.getLocation().x <= b3.getSize().width + b3.getLocation().x
						&& jumper.getLocation().y >= b3.getLocation().y - b3.getSize().height && points < 1000) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b4.getLocation().y && jumper.getLocation().x  >= b4.getLocation().x
						&& jumper.getLocation().x <= b4.getSize().width + b4.getLocation().x
						&& jumper.getLocation().y >= b4.getLocation().y - b4.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b5.getLocation().y && jumper.getLocation().x  >= b5.getLocation().x
						&& jumper.getLocation().x <= b5.getSize().width + b5.getLocation().x
						&& jumper.getLocation().y >= b5.getLocation().y - b5.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b6.getLocation().y && jumper.getLocation().x  >= b6.getLocation().x
						&& jumper.getLocation().x <= b6.getSize().width + b6.getLocation().x
						&& jumper.getLocation().y >= b6.getLocation().y - b6.getSize().height && points < 1000) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b7.getLocation().y && jumper.getLocation().x  >= b7.getLocation().x
						&& jumper.getLocation().x <= b7.getSize().width + b7.getLocation().x
						&& jumper.getLocation().y >= b7.getLocation().y - b7.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b8.getLocation().y && jumper.getLocation().x  >= b8.getLocation().x
						&& jumper.getLocation().x <= b8.getSize().width + b8.getLocation().x
						&& jumper.getLocation().y >= b8.getLocation().y - b8.getSize().height) {
					checkup = 0;
				} else if (jumper.getLocation().y <= b9.getLocation().y && jumper.getLocation().x  >= b9.getLocation().x
						&& jumper.getLocation().x <= b9.getSize().width + b9.getLocation().x
						&& jumper.getLocation().y >= b9.getLocation().y - b9.getSize().height && points < 1000) {
					checkup = 0;
				
				} else if (jumper.getLocation().y <= b10.getLocation().y && jumper.getLocation().x  >= b10.getLocation().x
						&& jumper.getLocation().x <= b10.getSize().width + b10.getLocation().x
						&& jumper.getLocation().y >= b10.getLocation().y - b10.getSize().height) {
					checkup = 0;
				
				} else if (jumper.getLocation().y <= b11.getLocation().y && jumper.getLocation().x  >= b11.getLocation().x
						&& jumper.getLocation().x <= b11.getSize().width + b11.getLocation().x
						&& jumper.getLocation().y >= b11.getLocation().y - b11.getSize().height) {
					checkup = 0;
				
				} else if (jumper.getLocation().y <= b12.getLocation().y && jumper.getLocation().x  >= b12.getLocation().x
						&& jumper.getLocation().x <= b12.getSize().width + b12.getLocation().x
						&& jumper.getLocation().y >= b12.getLocation().y - b12.getSize().height && points < 1000) {
					checkup = 0;
				}

			}

			public void run() {

				int counti = 0;
				int counti2 = -600;
				int speed = 15;
				int physi = 0;
				int counter2 = 0;
				boolean check = false;
				Random r = new Random();
				jumper.setVisible(true);

				while (!check) {
					physic();

					if (jumper.getLocation().y == 280) {
						check = true;
					}

					if (checkup <= 25) {
						while (physi != 4) {
							physi++;
							jumpyPos = jumpyPos - 1;
							physic();
							jumper.setLocation(jumpxPos, jumpyPos);
						}
						physi = 0;
						checkup++;
					} else {
						while (physi != 4) {
							physi++;
							jumpyPos = jumpyPos + 1;
							physic();
							jumper.setLocation(jumpxPos, jumpyPos);
						}
						physi = 0;
					}

					count++;
					if (counti == 600) {
						counti = -600;
						background.setLocation(0, counti);
					}
					if (counti2 == 600) {
						counti2 = -600;
						background2.setLocation(0, counti2);
					}

					background.setLocation(0, counti);
					background2.setLocation(0, counti2);

					b1.setLocation(xPosb1, yPosb1);
					b2.setLocation(xPosb2, yPosb2);
					b4.setLocation(xPosb4, yPosb4);
					b5.setLocation(xPosb5, yPosb5);
					b7.setLocation(xPosb7, yPosb7);
					b8.setLocation(xPosb8, yPosb8);
					b10.setLocation(xPosb10, yPosb10);
					b11.setLocation(xPosb11, yPosb11);

					if (points >= 1000) {

						if (b3.getLocation().y == 0) {
							b3.setVisible(false);
						} else {
							b3.setLocation(xPosb3, yPosb3);
							yPosb3++;
						}

						if (b6.getLocation().y == 0) {
							b6.setVisible(false);
						} else {
							b6.setLocation(xPosb6, yPosb6);
							yPosb6++;
						}

						if (b9.getLocation().y == 0) {
							b9.setVisible(false);
						} else {
							b9.setLocation(xPosb9, yPosb9);
							yPosb9++;
						}

						if (b12.getLocation().y == 0) {
							b12.setVisible(false);
						} else {
							b12.setLocation(xPosb12, yPosb12);
							yPosb12++;
						}

					} else {

						b3.setLocation(xPosb3, yPosb3);
						yPosb3++;

						b6.setLocation(xPosb6, yPosb6);
						yPosb6++;

						b9.setLocation(xPosb9, yPosb9);
						yPosb9++;

						b12.setLocation(xPosb12, yPosb12);
						yPosb12++;
					}

					counti++;
					counti2++;
					points++;
					counter2++;
					yPosb1++;
					yPosb2++;
					yPosb4++;
					yPosb5++;
					yPosb7++;
					yPosb8++;
					yPosb10++;
					yPosb11++;

					if (yPosb1 == 300) {
						yPosb1 = 0;
						xPosb1 = r.nextInt(394);
					} else if (yPosb2 == 300) {
						yPosb2 = 0;
						xPosb2 = r.nextInt(394);
					} else if (yPosb3 == 300) {
						yPosb3 = 0;
						xPosb3 = r.nextInt(394);
					} else if (yPosb4 == 300) {
						yPosb4 = 0;
						xPosb4 = r.nextInt(394);
					} else if (yPosb5 == 300) {
						yPosb5 = 0;
						xPosb5 = r.nextInt(394);
					} else if (yPosb6 == 300) {
						yPosb6 = 0;
						xPosb6 = r.nextInt(394);
					} else if (yPosb7 == 300) {
						yPosb7 = 0;
						xPosb7 = r.nextInt(394);
					} else if (yPosb8 == 300) {
						yPosb8 = 0;
						xPosb8 = r.nextInt(394);
					} else if (yPosb9 == 300) {
						yPosb9 = 0;
						xPosb9 = r.nextInt(394);
					} else if (yPosb10 == 300) {
						yPosb10 = 0;
						xPosb10 = r.nextInt(394);
					} else if (yPosb11 == 300) {
						yPosb11 = 0;
						xPosb11 = r.nextInt(394);
					} else if (yPosb12 == 300) {
						yPosb12 = 0;
						xPosb12 = r.nextInt(394);
					}

					if (counter2 == 500) {
						if (speed != 5) {
							speed--;
							counter2 = 0;
						}
					}

					try {
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				btnNewGame.setVisible(true);
				txtVerloren.setText(Integer.toString(points) + " Points");
				txtVerloren.setVisible(true);

			}
		});

		t.start();
		t2.start();

	}
}
 
Zuletzt bearbeitet:

Neue Beiträge