Swing, bewegung einer Komponente sichtbar machen.

Funresort

Erfahrenes Mitglied
Hi leute habe folgendes Problem, ich will ein Towerdefence spiel machen, nur leider ist die bewegung des monsters nicht sichtbar.

Java:
t = new Thread(new Runnable() {

			public void run() {

				int speed = 15;
				boolean check = false;
				creep1x = 793;
				creep1y = 75;
				creep1.setVisible(true);
				int counter = 0;
				ArrayList<Integer> koordis;
				Filelesen fl = new Filelesen(XtremeDefense.class
						.getResource("/minis/map1.txt").getPath());
				koordis = fl.getList();
				
				while (!check) {

						creep1.setLocation(koordis.get(counter),
								koordis.get(counter + 1));
						creep1x--;
						counter = counter + 2;
						if (counter >= koordis.size()- 1) {
							check = true;
						}
						if (creep1x <= 680) {
							creep1y++;
						}
						try {
							Thread.sleep(speed);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			
		});
		t.run();

aber das funktioniert komischer weise:

Java:
		t = new Thread(new Runnable() {

			public void run() {


				int speed = 15;
				boolean check = false;
				creep1x = 793;
				creep1y = 75;
				creep1.setVisible(true);
				int counter = 0;
				
				
				
				while (!check) {
					
					creep1.setLocation(creep1x, creep1y);
					creep1x--;
					counter++;

					if (creep1x <= 680) {
						creep1y++;
					}
					try {
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

				}
			}
		});


Wieso kann ich beim 2ten sehen wie es sich bewegt und beim ersten nicht?

Gruß Chris
 
Öhm, was ich gerade gesehen habe:
t.run();

So wird einfach die run()-Methode aufgerufen, aber es wird kein separater Thread gestartet. Da fällt mir dazu ein:
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

Dann wichtig, painting in awt/swing:
http://java.sun.com/products/jfc/tsc/articles/painting/

Und dieser Link wird dir sicher am besten weiterhelfen, dort werden die Basics für 2d-Spiele in Java2D erklärt...
http://zetcode.com/tutorials/javagamestutorial/

Gruss
slowy
 
Zurück