diff --git a/src/ch/bfh/sevennotseven/FieldCanvas.java b/src/ch/bfh/sevennotseven/FieldCanvas.java index cb07e4a..cf12a79 100644 --- a/src/ch/bfh/sevennotseven/FieldCanvas.java +++ b/src/ch/bfh/sevennotseven/FieldCanvas.java @@ -1,52 +1,73 @@ package ch.bfh.sevennotseven; -import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; +import java.awt.Point; import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; -public class FieldCanvas extends Canvas{ - +import javax.swing.JPanel; + +public class FieldCanvas extends JPanel{ + private static final long serialVersionUID = 1L; private int size; - private int[][] field; + private Game game; static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta}; FieldCanvas(){ + + addMouseListener(new MouseAdapter(){ + @Override + public void mousePressed(MouseEvent e) { + super.mousePressed(e); + Point point = e.getPoint(); + + int total = Math.min(FieldCanvas.this.getHeight(),FieldCanvas.this.getWidth())-10; + int space = total/size; + + + + + + repaint(); + } + }); } + public void setGame(Game game) { + this.game=game; + } + public void setSize(int s) { this.size = s; } - public void setField(int[][] field){ - this.field = field; - } - - - public void paint(Graphics g) { + public void paintComponent(Graphics g) { + super.paintComponent(g); g.setColor(Color.lightGray); - int total = this.getHeight(); + g.translate(5, 5); + int total = Math.min(this.getHeight(),this.getWidth())-10; int space = total/size; - g.setClip(0, 0, total,total); - g.fillRect(0, 0, total,total); + g.setClip(0, 0, total-1,total-1); + g.fillRect(0, 0, total-1,total-1); g.setColor(Color.white); for(int i=0; i<=size; i++) { - g.drawLine(0,i*space,total,i*space); - g.drawLine(i*space,0,i*space,total); + g.drawLine(0,i*space,total-2,i*space); + g.drawLine(i*space,0,i*space,total-2); } - - if(field==null) return; + + if(game==null) return; for(int x=0; x getPath(Point src, Point dst){ @@ -74,7 +79,15 @@ public class Game { } public boolean doFreeMove(Point src, Point dst){ - return false; + //move without path checking + if(getAvailFreeMoves() <= 0 ) return false; + if(field[src.x][src.y]==0) return false; + field[dst.x][dst.y] = field[src.x][src.y]; + field[src.x][src.y] = 0; + nextStep(dst); //cleanup rows or add new blocks + return true; + + } public int getAvailFreeMoves(){ @@ -101,12 +114,24 @@ public class Game { this.populateField(); } + + /** + * Calculates the next game step. This method will either call populateField, or it will cleanup blocks + */ + private void nextStep(Point lastPoint) { + //TODO: Check if there are any new rows (with at least 4 elements horizontally, vertically, or diagonal) near lastpoint + //TODO: if so: remove the row, add some points or extras and quit + //TODO: if not: + populateField(); //add new blocks + + } + /* - * Calculates the next game step. - * Add n new blocks to random positions on the field, + + * Adds n new blocks to random positions on the field, * according to the level number. */ - public void populateField(){ + private void populateField(){ // while there are blocks left in nextBlocks while((nextBlocks.size() > 0) && (freeBlocks > 0)){ diff --git a/src/ch/bfh/sevennotseven/Window.java b/src/ch/bfh/sevennotseven/Window.java index a139488..aeff62c 100644 --- a/src/ch/bfh/sevennotseven/Window.java +++ b/src/ch/bfh/sevennotseven/Window.java @@ -5,8 +5,6 @@ package ch.bfh.sevennotseven; import java.awt.Frame; import java.awt.HeadlessException; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -18,13 +16,11 @@ import java.awt.event.WindowEvent; */ public class Window extends Frame { + private static final long serialVersionUID = 1L; private Game game; private FieldCanvas field; - /** - * @param title - * @throws HeadlessException - */ + public Window(String title) throws HeadlessException { super(title); @@ -38,28 +34,21 @@ public class Window extends Frame { field = new FieldCanvas(); field.setSize(7); game = new Game(); + field.setGame(game); - field.addMouseListener(new MouseAdapter(){ - @Override - public void mousePressed(MouseEvent e) { - super.mousePressed(e); - game.populateField(); - field.repaint(); - } - }); + this.add(field); this.setSize(400,400); this.setVisible(true); - - field.setField(game.getField()); + } /** * @param args */ public static void main(String[] args) { - new Window("Test"); + new Window("7 not 7"); }