Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package rainet; import java.io.IOException; import java.util.ArrayList; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.util.ResourceLoader; /** * * @author Nial8r */ public class Board { private Texture bgTex; private int width, height; ArrayList<ArrayList> pieces = new ArrayList<>(); private float sX, sY, bX, bY, wX, wY; public Board(String s, int w, int h, float sx, float sy, float bx, float by, float wx, float wy) throws IOException { this.bgTex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(s)); this.width = w; this.height = h; this.sX = sx; this.sY = sy; this.bX = bx; this.bY = by; this.wX = wx; this.wY = wy; } public void draw() throws IOException { bgTex.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(0, 0); GL11.glTexCoord2f(1, 0); GL11.glVertex2f(Display.getWidth(), 0); GL11.glTexCoord2f(1, 1); GL11.glVertex2f(Display.getWidth(), Display.getHeight()); GL11.glTexCoord2f(0, 1); GL11.glVertex2f(0, Display.getHeight()); GL11.glEnd(); for (int i = 0; i < pieces.size(); i++) { for (int j = 0; j < pieces.get(i).size(); j++) { if (pieces.get(i).get(j) != null) ((Piece) pieces.get(i).get(j)).draw(this.sX + (this.bX + this.wX) * j, this.sY + (this.bY + this.wY) * i, this.wX, this.wY); } } } public void drawSelect(int spx, int spy) throws IOException { if (this.inSpace(spx, spy) != null) ((Piece) pieces.get(spy).get(spx)).drawSelect(this.sX + (this.bX + this.wX) * spx, this.sY + (this.bY + this.wY) * spy, this.wX, this.wY); } public void initBoard() { for (int i = 0; i < this.height; i++) { ArrayList<Piece> tar = new ArrayList<>(this.width); for (int j = 0; j < this.width; j++) { tar.add(null); } pieces.add(tar); } } public void fillPieces(ArrayList<ArrayList<int[]>> p) throws IOException { for (int i = 0; i < p.size(); i++) { for (int j = 0; j < p.get(i).size(); j++) { int[] tia = p.get(i).get(j); if (tia.length > 0) pieces.get(i).set(j, new Piece(tia[0], tia[1], tia[2] == 1)); } } } public void removePiece(int x, int y) { pieces.get(y).set(x, null); } public void place(int s, int p, int x, int y, boolean f) throws IOException { //System.out.println("mayB?"); if (s != -1 && p != -1) pieces.get(y).set(x, new Piece(s, p, f)); } public int[] getSpace(float x, float y) { return new int[] { (int) ((x - sX) * (width) / ((this.wX + this.bX) * (width))), (height - 1) - (int) ((y - sY) * (height) / ((this.wY + this.bY) * (height))) }; } public boolean inGameBoard(float x, float y) { return (x > this.sX && x < this.sX + (this.bX + this.wX) * this.width && y > this.sY && y < this.sY + (this.bY + this.wY) * this.height); } public Piece inSpace(int x, int y) throws IOException { return (Piece) pieces.get(y).get(x); } public boolean movePiece(int x1, int y1, int x2, int y2) { if (pieces.get(y1).get(x1) == null) return false; pieces.get(y2).set(x2, pieces.get(y1).get(x1)); pieces.get(y1).set(x1, null); return true; } }