Refactored main gui
This commit is contained in:
@@ -13,10 +13,10 @@ public class FieldCanvas extends JPanel{
|
||||
private static final long serialVersionUID = 1L;
|
||||
static final int borderLeft = 5;
|
||||
static final int borderRight = 5;
|
||||
static final int borderTop = 100;
|
||||
static final int borderTop = 5;
|
||||
static final int borderBottom = 5;
|
||||
|
||||
static final Color[] colors = {
|
||||
public static final Color[] colors = {
|
||||
new Color(0xD66436),
|
||||
new Color(0x486F70),
|
||||
new Color(0xCBD840),
|
||||
@@ -25,14 +25,13 @@ public class FieldCanvas extends JPanel{
|
||||
};
|
||||
|
||||
|
||||
|
||||
private int size;
|
||||
private Game game;
|
||||
private Point src;
|
||||
private Point dst;
|
||||
private List<Point> path;
|
||||
|
||||
FieldCanvas(){
|
||||
FieldCanvas(Game g){
|
||||
MouseAdapter ad = new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
@@ -78,6 +77,8 @@ public class FieldCanvas extends JPanel{
|
||||
|
||||
addMouseListener(ad);
|
||||
addMouseMotionListener(ad);
|
||||
this.game=g;
|
||||
this.size = game.getSize();
|
||||
|
||||
}
|
||||
|
||||
@@ -90,28 +91,12 @@ public class FieldCanvas extends JPanel{
|
||||
return new Point(globalPos.x/space,globalPos.y/space);
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
this.game=game;
|
||||
}
|
||||
|
||||
public void setSize(int s) {
|
||||
this.size = s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.lightGray);
|
||||
|
||||
List<Integer> nextBlocks = game.getNextBlocks();
|
||||
for(int i=0; i< nextBlocks.size(); i++) {
|
||||
g.setColor(colors[nextBlocks.get(i)-1]);
|
||||
g.fillRect(borderLeft + borderTop/2 *i, borderTop/4, borderTop/2, borderTop/2);
|
||||
g.setColor(Color.white);
|
||||
g.drawRect(borderLeft + borderTop/2 *i, borderTop/4, borderTop/2, borderTop/2);
|
||||
}
|
||||
|
||||
|
||||
g.setColor(Color.lightGray);
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,11 @@ import java.util.Random;
|
||||
|
||||
public class Game {
|
||||
|
||||
interface UpdateListener {
|
||||
public void gameUpdate();
|
||||
}
|
||||
|
||||
|
||||
// Constants
|
||||
static final int numberOfColors = 5;
|
||||
|
||||
@@ -20,6 +25,7 @@ public class Game {
|
||||
private int freeMoves;
|
||||
private int numUndos;
|
||||
private Random rand;
|
||||
private ArrayList<UpdateListener> updateListeners;
|
||||
|
||||
public Game(){
|
||||
this(7);
|
||||
@@ -31,16 +37,33 @@ public class Game {
|
||||
this.level = 1;
|
||||
this.score = 0;
|
||||
this.freeBlocks = size * size;
|
||||
this.updateListeners = new ArrayList<UpdateListener>();
|
||||
|
||||
rand = new Random(); // Initialize random object
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void addUpdateListener(UpdateListener listener) {
|
||||
updateListeners.add(listener);
|
||||
}
|
||||
public void removeUpdateListener(UpdateListener listener) {
|
||||
updateListeners.remove(listener);
|
||||
}
|
||||
private void emitUpdateEvent() {
|
||||
for(UpdateListener e: updateListeners) {
|
||||
e.gameUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isGameOver(){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public int getScore(){
|
||||
return score;
|
||||
|
||||
@@ -251,6 +274,7 @@ public class Game {
|
||||
if(!checkRemoveBlocks(lastPoint)){
|
||||
populateField(); //add new blocks
|
||||
}
|
||||
emitUpdateEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
44
src/ch/bfh/sevennotseven/NextMovesCanvas.java
Normal file
44
src/ch/bfh/sevennotseven/NextMovesCanvas.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package ch.bfh.sevennotseven;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class NextMovesCanvas extends JPanel {
|
||||
private Game game;
|
||||
|
||||
static final int borderLeft = 1;
|
||||
static final int borderRight = 1;
|
||||
static final int borderTop = 1;
|
||||
static final int borderBottom = 1;
|
||||
|
||||
public NextMovesCanvas(Game g) {
|
||||
this.game = g;
|
||||
g.addUpdateListener(new Game.UpdateListener() {
|
||||
@Override
|
||||
public void gameUpdate() {
|
||||
NextMovesCanvas.this.repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
|
||||
g.setColor(Color.lightGray);
|
||||
int height = getHeight() - borderTop-borderBottom;
|
||||
|
||||
List<Integer> nextBlocks = game.getNextBlocks();
|
||||
for(int i=0; i< nextBlocks.size(); i++) {
|
||||
g.setColor(FieldCanvas.colors[nextBlocks.get(i)-1]);
|
||||
g.fillRect(borderLeft + height *i, borderTop, height,height);
|
||||
g.setColor(Color.white);
|
||||
g.drawRect(borderLeft + height *i, borderTop, height,height);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,22 +3,37 @@
|
||||
*/
|
||||
package ch.bfh.sevennotseven;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Button;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.Panel;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author aaron
|
||||
*
|
||||
*/
|
||||
public class Window extends Frame {
|
||||
public class Window extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Game game;
|
||||
private FieldCanvas field;
|
||||
private NextMovesCanvas moves;
|
||||
|
||||
private JButton buttonUndo;
|
||||
private JButton buttonFreeMove;
|
||||
private JLabel labelScore;
|
||||
|
||||
|
||||
public Window(String title) throws HeadlessException {
|
||||
@@ -31,16 +46,42 @@ public class Window extends Frame {
|
||||
}
|
||||
});
|
||||
|
||||
field = new FieldCanvas();
|
||||
field.setSize(7);
|
||||
|
||||
game = new Game();
|
||||
field.setGame(game);
|
||||
moves = new NextMovesCanvas(game);
|
||||
field = new FieldCanvas(game);
|
||||
|
||||
moves.setPreferredSize(new Dimension(200, 40));
|
||||
|
||||
|
||||
buttonFreeMove= new JButton("FreeMove");
|
||||
buttonUndo = new JButton("Undo");
|
||||
labelScore= new JLabel("Score: 0");
|
||||
|
||||
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||
|
||||
topPanel.add(buttonFreeMove);
|
||||
topPanel.add(buttonUndo);
|
||||
topPanel.add(labelScore);
|
||||
topPanel.add(moves);
|
||||
|
||||
game.addUpdateListener(new Game.UpdateListener() {
|
||||
|
||||
@Override
|
||||
public void gameUpdate() {
|
||||
labelScore.setText("Score: "+game.getScore());
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
this.add(topPanel, BorderLayout.NORTH);
|
||||
this.add(field);
|
||||
this.setSize(400,400);
|
||||
this.setSize(470,460);
|
||||
this.setVisible(true);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user