Added simple rendering

This commit is contained in:
t-moe
2016-05-27 11:55:45 +02:00
parent 7ffd3cdb12
commit f5119d8be9
3 changed files with 75 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
package ch.bfh.sevennotseven;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class Field extends Canvas {
private int size;
private int[][] field;
static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta};
public void setSize(int s) {
this.size = s;
}
public void setField(int[][] field){
this.field = field;
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
int total = this.getHeight();
int space = total/size;
g.setClip(0, 0, total,total);
g.fillRect(0, 0, total,total);
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);
}
if(field==null) return;
for(int x=0; x<size; x++) {
for(int y=0; y<size; y++) {
int colorCode = field[x][y];
if(colorCode!=0) {
g.setColor(colors[colorCode-1]);
g.fillRect(x*space+2, y*space+2, space -3, space -3);
}
}
}
}
}

View File

@@ -4,11 +4,12 @@
package ch.bfh.sevennotseven;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author aaron
*
@@ -16,6 +17,7 @@ import java.awt.event.WindowEvent;
public class Window extends Frame {
private Game game;
private Field field;
/**
* @param title
@@ -23,7 +25,7 @@ public class Window extends Frame {
*/
public Window(String title) throws HeadlessException {
super(title);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
@@ -31,7 +33,22 @@ public class Window extends Frame {
}
});
field = new Field();
field.setSize(7);
game = new Game();
this.add(field);
this.setSize(400,400);
this.setVisible(true);
int [][] testfield = new int[7][7];
testfield[0][0] = 2;
testfield[1][3] = 4;
field.setField(testfield);
}
/**