Skip to content

Commit

Permalink
I think the screen possibly repaints now
Browse files Browse the repository at this point in the history
  • Loading branch information
vstepchik committed Sep 5, 2014
1 parent 9b8b538 commit 4656978
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/gmail/vitalatron/game/exec/GameTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

public interface GameTask {

public static GameTask EMPTY = new GameTask() {
@Override
public void execute(double delta) {
// empty
}
};

public abstract void execute(double delta);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.gmail.vitalatron.game.visual;

import com.gmail.vitalatron.game.exec.ExecutorGameLoop;
import com.gmail.vitalatron.game.exec.GameTask;
import com.gmail.vitalatron.game.exec.TimedLoop;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractGameWindow implements GameWindow {
protected final List<Drawable> drawableItems = new ArrayList<>();
protected TimedLoop repaintLoop;
protected GameTask repaintTask = GameTask.EMPTY;

public AbstractGameWindow() {
repaintLoop = new ExecutorGameLoop();
repaintLoop.addTask(repaintTask);
}

@Override
public void setFps(int fps) {
repaintLoop.setIterationsPerSecond(fps);
}

@Override
public void addDrawableItem(Drawable drawable) {
drawableItems.add(drawable);
}

@Override
public void removeDrawableItem(Drawable drawable) {
drawableItems.remove(drawable);
}

@Override
public void clearDrawableItems() {
drawableItems.clear();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/gmail/vitalatron/game/visual/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gmail.vitalatron.game.visual;

import java.awt.*;

public interface Drawable {
public void draw(Graphics2D g);
}
28 changes: 28 additions & 0 deletions src/main/java/com/gmail/vitalatron/game/visual/DrawableSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gmail.vitalatron.game.visual;

import java.awt.*;
import java.util.List;
import java.util.ArrayList;

public class DrawableSet implements Drawable {
private final List<Drawable> drawableItems = new ArrayList<>();

@Override
public void draw(Graphics2D g) {
for (Drawable d : drawableItems) {
d.draw(g);
}
}

public void add(Drawable drawable) {
drawableItems.add(drawable);
}

public void remove(Drawable drawable) {
drawableItems.remove(drawable);
}

public void clear() {
drawableItems.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.gmail.vitalatron.game.visual;

import java.awt.*;

public interface GameWindow {
public void show();
public void close();
public void setIcon(Image icon);

public void setFps(int fps);

public void addDrawableItem(Drawable drawable);
public void removeDrawableItem(Drawable drawable);
public void clearDrawableItems();
}
12 changes: 12 additions & 0 deletions src/main/java/com/gmail/vitalatron/game/visual/Sprite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gmail.vitalatron.game.visual;

import java.awt.*;

public class Sprite implements Drawable {

@Override
public void draw(Graphics2D g) {

}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
package com.gmail.vitalatron.game.visual;

import com.gmail.vitalatron.game.exec.GameTask;

import javax.swing.*;
import java.awt.*;

public class SwingGameWindow implements GameWindow {
public class SwingGameWindow extends AbstractGameWindow {
public static final Dimension DEFAULT_FRAME_SIZE = new Dimension(400, 400);
private final JFrame frame;

public SwingGameWindow(String title) {
this(title, DEFAULT_FRAME_SIZE);
}

public SwingGameWindow(String title, Dimension size) {
frame = createGameFrame(title, size);
final Canvas canvas = new Canvas();
frame = createGameFrame(title, size, canvas);

repaintTask = new GameTask() {
private Canvas c = canvas;

@Override
public void execute(double delta) {
Graphics2D g = (Graphics2D) c.getGraphics();
for (Drawable item : drawableItems) {
item.draw(g);
}
}
};
}

@Override
Expand All @@ -31,11 +50,17 @@ public void run() {
});
}


@Override
public void setIcon(Image icon) {
frame.setIconImage(icon);
}

public JFrame getFrame() {
return frame;
}

protected static JFrame createGameFrame(String title, Dimension size) {
protected static JFrame createGameFrame(String title, Dimension size, Component screenComponent) {
JFrame frame = new JFrame(title);

// position
Expand All @@ -51,6 +76,10 @@ protected static JFrame createGameFrame(String title, Dimension size) {
frame.setFocusTraversalKeysEnabled(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setUndecorated(true);
screenComponent.setFocusable(false);

// placing components
frame.getContentPane().add(screenComponent);

return frame;
}
Expand Down

0 comments on commit 4656978

Please sign in to comment.