Erster Prototyp

This commit is contained in:
Mogk 2014-10-21 11:25:49 +02:00
parent 7b4429b931
commit 5ec52879ac
7 changed files with 184 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
*~ *~
.project .project
.settings .settings
.classpath
bin

View File

@ -0,0 +1,31 @@
package de.teamteamteam.spacescooter;
import java.awt.EventQueue;
import de.teamteamteam.spacescooter.gui.GameFrame;
import de.teamteamteam.spacescooter.threads.PaintThread;
import de.teamteamteam.spacescooter.threads.UpdateThread;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
final GameFrame gf = new GameFrame();
EventQueue.invokeLater(new Runnable() {
public void run() {
gf.init();
}
});
PaintThread pt = new PaintThread(gf);
pt.start();
UpdateThread ut = new UpdateThread(gf);
ut.start();
}
}

View File

@ -0,0 +1,15 @@
package de.teamteamteam.spacescooter.entities;
import java.awt.Graphics;
public abstract class Entity {
public void update() {
}
public void paint(Graphics g) {
}
}

View File

@ -0,0 +1,28 @@
package de.teamteamteam.spacescooter.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class TestEntity extends Entity {
private Color color;
private Random random;
public TestEntity() {
super();
this.color = new Color(255, 0, 0);
this.random = new Random();
}
public void paint(Graphics g) {
g.setColor(this.color);
g.drawRect(100, 200, 300, 300);
}
public void update() {
System.out.println("TestEntity.update()!");
this.color = new Color(this.random.nextInt(255),this.random.nextInt(255),this.random.nextInt(255));
}
}

View File

@ -0,0 +1,48 @@
package de.teamteamteam.spacescooter.gui;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import de.teamteamteam.spacescooter.entities.Entity;
import de.teamteamteam.spacescooter.entities.TestEntity;
public class GameFrame extends JFrame {
private ArrayList<Entity> entities;
public GameFrame() {
super();
this.entities = new ArrayList<Entity>();
//TODO: Remove this!
TestEntity t = new TestEntity();
this.entities.add(t);
}
public ArrayList<Entity> getEntityList() {
return this.entities;
}
public void init() {
this.setTitle("Unser schöner Titel");
this.setSize(800, 600);
this.setUndecorated(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator<Entity> i = this.entities.iterator();
while(i.hasNext()) {
Entity e = i.next();
e.paint(g);
}
}
}

View File

@ -0,0 +1,23 @@
package de.teamteamteam.spacescooter.threads;
import de.teamteamteam.spacescooter.gui.GameFrame;
public class PaintThread extends Thread {
private GameFrame gf;
public PaintThread(GameFrame gf) {
this.gf = gf;
}
public void run() {
while (true) {
try {
Thread.sleep(16);
} catch (InterruptedException e) {
System.err.println(e);
}
this.gf.repaint();
}
}
}

View File

@ -0,0 +1,37 @@
package de.teamteamteam.spacescooter.threads;
import java.util.ArrayList;
import java.util.Iterator;
import de.teamteamteam.spacescooter.entities.Entity;
import de.teamteamteam.spacescooter.gui.GameFrame;
public class UpdateThread extends Thread {
private GameFrame gf;
public UpdateThread(GameFrame gf) {
this.gf = gf;
}
public void run() {
while (true) {
try {
Thread.sleep(16);
} catch (InterruptedException e) {
System.err.println(e);
}
this.updateEntities();
}
}
public void updateEntities() {
ArrayList<Entity> entityList = this.gf.getEntityList();
Iterator<Entity> i = entityList.iterator();
while(i.hasNext()) {
Entity e = i.next();
e.update();
}
}
}