Permalink
Browse files

add game structure and animating engine

  • Loading branch information...
Jeky Cui
Jeky Cui committed Nov 28, 2017
1 parent e86e83e commit d837c001f0e59658dccbf4ca22f106793434f719

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,10 @@
package org.jam.core.game;

import java.awt.Graphics2D;

public interface Animation {

void update();

void draw(Graphics2D g2d);
}
@@ -0,0 +1,68 @@
package org.jam.core.game;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class Game implements Runnable {

public static final int DEFAULT_FPS = 60;

private long timeDelta;
private GameCanvas canvas;
private JFrame gameWindow;
private boolean start;

public Game() {
this(DEFAULT_FPS);
}

public Game(int fps) {
this((long) 1000 / fps, new GameCanvas());
}

public Game(long timeDelta, GameCanvas canvas) {
this.timeDelta = timeDelta;
this.canvas = canvas;

start = false;

gameWindow = new JFrame();
gameWindow.add(canvas);
gameWindow.pack();
gameWindow.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
stop();
}
});
}

public GameCanvas getCanvas() {
return canvas;
}

public void start() {
start = true;
Thread gameThread = new Thread(this);
gameThread.start();
gameWindow.setVisible(true);
}

public void stop() {
start = false;
gameWindow.dispose();
}

@Override
public void run() {
while (start) {
canvas.repaint();
try {
Thread.sleep(timeDelta);
} catch (InterruptedException e) {
start = false;
}
}
}
}
@@ -0,0 +1,42 @@
package org.jam.core.game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;

public class GameCanvas extends JComponent {

private List<Animation> animations;
private Color backgroundColor;

public GameCanvas() {
this(Color.black);
}

public GameCanvas(Color backgroundColor) {
this.backgroundColor = backgroundColor;
this.animations = new LinkedList<>();
this.setPreferredSize(new Dimension(800, 600));
}

public void addAnimation(Animation animation) {
animations.add(animation);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, getWidth(), getHeight());

animations.forEach(a -> {
a.update();
a.draw(g2d);
});
}
}
@@ -0,0 +1,37 @@
package org.jam.demo;

import java.awt.Color;
import java.awt.Graphics2D;

import org.jam.core.game.Animation;

public class AnimatedCircle implements Animation {

private int x;
private int y;
private int r;
private int dx;
private int dy;
private Color color;

public AnimatedCircle(int x, int y, int r, int dx, int dy, Color color) {
this.x = x;
this.y = y;
this.r = r;
this.dx = dx;
this.dy = dy;
this.color = color;
}

@Override
public void update() {
x += dx;
y += dy;
}

@Override
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fillOval(x, y, r, r);
}
}
@@ -1,10 +1,27 @@
package org.jam.demo;

import org.jam.core.Game;
import java.awt.Color;
import java.util.Random;
import java.util.stream.IntStream;

import org.jam.core.game.Game;

public class Main {

public static void main(String[] args) {
System.out.println(Game.HELLO);
Random random = new Random();

Game game = new Game();
IntStream.range(0, 10)
.forEach(i -> {
int x = random.nextInt(100) + 350;
int y = random.nextInt(100) + 250;
int dx = random.nextInt(3) - 1;
int dy = random.nextInt(3) - 1;
int r = 5;
Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
game.getCanvas().addAnimation(new AnimatedCircle(x, y, r, dx, dy, c));
});
game.start();
}
}

0 comments on commit d837c00

Please sign in to comment.