rntest.Piece.java Source code

Java tutorial

Introduction

Here is the source code for rntest.Piece.java

Source

/*
 * 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 rntest;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author nticknor19
 */
public class Piece {
    private int side, pID;

    private Texture textureF, textureB;
    private boolean faceUp;

    public Piece(int s, int p, boolean f) throws IOException {
        pID = p;
        side = s;
        textureF = TextureLoader.getTexture("PNG",
                ResourceLoader.getResourceAsStream("res/piece" + this.side + this.pID + "_front.png"));
        textureB = TextureLoader.getTexture("PNG",
                ResourceLoader.getResourceAsStream("res/piece" + this.side + this.pID + "_back.png"));
        faceUp = f;
    }

    public void draw(float x, float y) {
        draw(x, y, (faceUp ? textureF : textureB).getTextureWidth(),
                (faceUp ? textureF : textureB).getTextureHeight());
    }

    public void draw(float x, float y, float width, float height) {
        (faceUp ? textureF : textureB).bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(x, y);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(x + width, y);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(x + width, y + height);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(x, y + height);
        GL11.glEnd();
    }

    public int getSide() {
        return side;
    }

    public int getpID() {
        return pID;
    }

    public boolean flip() {
        return this.faceUp = !this.faceUp;
    }

    public void morph(int sid, int pid, boolean updT) throws IOException {
        this.side = sid;
        this.pID = pid;
        if (updT) {
            textureF = TextureLoader.getTexture("PNG",
                    ResourceLoader.getResourceAsStream("res/piece" + this.side + this.pID + "_front.png"));
            textureB = TextureLoader.getTexture("PNG",
                    ResourceLoader.getResourceAsStream("res/piece" + this.side + this.pID + "_back.png"));
        }
    }

}