Package xunome.graphics

Source Code of xunome.graphics.GraphicPlayer

package xunome.graphics;

/**
*
* @author Sem iNick
*/
import xunome.game.Player;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import xunome.game.Card;

public class GraphicPlayer {

    private Player player;
    private Image rendered;
    private int screenWX;
    private int screenWY;
    private boolean showFace;
    //Constants for render image
    private final int WIDTH = 15;
    private final int HEIGHT = 23;

    private static final GraphicCard BACK = GraphicCard.createGraphicCard(new Card(Card.BACK));
    public static final byte DOWN = 0;
    public static final byte LEFT = 1;
    public static final byte UP = 2;
    public static final byte RIGHT = 3;

    public GraphicPlayer(Player owner, int screenWidth, int screenHeight) {

        player = owner;
        screenWX = screenWidth;
        screenWY = screenHeight;
        showFace = false;
    }

    public void repaint() {

        int numberOfCards = player.getNumberOfCards();
        Graphics gRender = null;
       
        switch(player.getPos()) {

            case DOWN:

                    int maxCardX = screenWX / WIDTH;
                    int width = (numberOfCards < maxCardX)?(numberOfCards + 1) * WIDTH
                                :maxCardX * WIDTH;
                    rendered = Image.createImage(width, HEIGHT * 3);
                    gRender = rendered.getGraphics();
                    //Fill background with black color.
                    gRender.fillRect(0, 0, rendered.getWidth(), rendered.getHeight());

                    int card = 0;
                    int end = numberOfCards - 1;
                    int spacer = 0;
                    int atual = player.getAtualCardP();
                    //counting starts in 0
                    if (player.getNumberOfCards() >= maxCardX) {

                        if ((atual >= maxCardX - 4)
                            && (atual < numberOfCards - 3)) {

                            card = atual - (maxCardX - 4);
                        } else if (atual >= numberOfCards - 3) {

                            card = numberOfCards - maxCardX;
                            if (atual == numberOfCards - 1) {

                                spacer = -1;
                            }
                        }

                        end = card + maxCardX - 1;
                    }
                   
                    //Draw cards
                    for(; card <= end; card++, spacer++) {

                        Image render = player.getCard(card).getGraphic().getRender();
                        int posy = (card == atual)?0:HEIGHT;
                        gRender.drawRegion(render, 0, 0, render.getWidth(),
                                           render.getHeight(), Sprite.TRANS_NONE,
                                           spacer * WIDTH, posy,
                                           Graphics.TOP|Graphics.LEFT);
                    }
            break;

            case UP: {

                int width2 = (numberOfCards < 15)?(numberOfCards + 1) * WIDTH
                                :15 * WIDTH;
                rendered = Image.createImage(width2, HEIGHT*2);
                gRender = rendered.getGraphics();
                //Fill background with black color.
                gRender.fillRect(0, 0, rendered.getWidth(), rendered.getHeight());

                int max = (numberOfCards < 15)?numberOfCards:14;
                //Draw player
                for(int n = 0; n < max; n++) {

                    Image render = ((showFace)
                                    ?player.getCard(n).getGraphic()
                                    :BACK).getRender();
                    gRender.drawRegion(render, 0, 0, render.getWidth(),
                                       render.getHeight(), Sprite.TRANS_NONE,
                                       n*WIDTH, 0, Graphics.TOP|Graphics.LEFT);
                }
            }
            break;
           
            case LEFT:
            case RIGHT:

                int capacity = (screenWY - 136) / 15;
                int height = (numberOfCards < capacity)?(numberOfCards + 1) * WIDTH
                                                     : (capacity - 1) * WIDTH;
                rendered = Image.createImage(HEIGHT * 2, height);
                gRender = rendered.getGraphics();
                //Fill background with black color.
                gRender.fillRect(0, 0, rendered.getWidth(), rendered.getHeight());
               
                int max = (numberOfCards < capacity - 2)?numberOfCards:capacity - 2;
                for(int n = 0; n < max; n++) {

                    Image render = ((showFace)
                                    ?player.getCard(n).getGraphic()
                                    :BACK).getRender();
                    gRender.drawRegion(render, 0, 0, render.getWidth(),
                                       render.getHeight(),
                                       (player.getPos() == LEFT)
                                       ?Sprite.TRANS_ROT270
                                       :Sprite.TRANS_ROT90,
                                       0, n*WIDTH, Graphics.TOP|Graphics.LEFT);
                }
            break;
        }
    }

    public Image getRendered() {
        return rendered;
    }

    public void setShowFace(boolean show) {

        showFace = show;
    }
}
TOP

Related Classes of xunome.graphics.GraphicPlayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.