⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tablero.java

📁 J2ME 技术开发的扑克牌类游戏 是开源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Tablero.java
 *
 * Created on 3 de junio de 2005, 12:03
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package telefono;

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.xmlpull.v1.XmlPullParser;
/**
 *
 * @author Enrique Vicent Ramis
 */

 class Tablero extends Canvas  
{
    int jugadores=3;
    boolean borde=true;
    int alto=300;
    int ancho=200;  
    final static int vBorde=5;
    private int altoCelda;
    private int sx=1;
    private int sy=1;
    private Partida partida;
    private int vScroll=0;
    private Command exitCommand;
    private Command graficoCommand;
    /** mano es quien da en cada turo (mano % jugadores). un 0 significa que no nos importa*/
    int mano=0;

    static final String xmlTag="pantalla";
   
    class Cell
    {
        public int tx,ty,fx,fy;
        public int getAncho(){return tx-fx;}
        public int getAlto(){return ty-fy;}
        private boolean drawable=true;
        public boolean isDrawable(){return drawable;}
        private Graphics g;
        
        /** construye una celda
         * establece sus 4 coordenadas y si es imprimible en la pantalla 
         */
        public Cell(Graphics g,int xPos,int yPos,int jugadores,boolean borde)
        {
            this.g=g;
            if (xPos<0 || yPos < 0 || xPos>jugadores || yPos>getMaxFilas() )
                drawable=false;
            else if(g==null)
                drawable=false;
            else
            {
                int inicio=borde?vBorde:0;
                int fAncho=((ancho-inicio)/jugadores);
                if (xPos==0)//borde
                {
                    fx=0;
                    tx=vBorde;
                }
                else
                {
                    tx=inicio+(fAncho*xPos);
                    fx=tx-fAncho;
                }
                int fAlto=altoCelda;
                fy=fAlto*yPos;
                ty=fy+fAlto;
            }
        }
        /** escribe un resultado parcial en la celda*/
        public void escribirParcial(int valor)
        {
            if(this.isDrawable())
            {
                g.setColor(valor<0?255:0,0,valor<0?0:255);
                Font fuente = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
                g.setFont(fuente);
                g.drawString(Integer.toString(valor), fx,ty,Graphics.BASELINE|Graphics.LEFT);
            }
        }
        /** dibuja una marca en la celda como "celda-mano" */
        public void marcarMano()
        {
            if(this.isDrawable())
            {
                g.setColor(0, 0xff, 0x66);
                g.fillRect(tx-5,ty-5,4,4);
            }
        }
        
        /** escribe un resultado acumulado en la celda*/
        public void escribirAcumulado(int valor)
        {
            if(this.isDrawable())
            {
                g.setColor(0,0,60);
                Font fuente = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
                g.setFont(fuente);
                g.drawString(Integer.toString(valor), tx,ty,Graphics.BASELINE|Graphics.RIGHT);
            }            
        }
        /** 
         * escribe un nombre de jugador en la celda
         * @param valor nombre del jugado
         * @color color (opcional)
         */
        public void escribirTitulo(String valor,int color)
        {
            if(this.isDrawable())
            {
                g.setColor(color);
                Font fuente = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
                g.setFont(fuente);
                g.drawString(valor, fx+getAncho()/2,ty,Graphics.BASELINE|Graphics.HCENTER);
            }
        }
        
        /** escribe un nombre de jugador en la celda*/
        public void escribirTitulo(String valor)
        {
            escribirTitulo(valor, 0);
        }
        
        /** calcula el m?ximo de filas disponibles para la pantalla*/
        public int getMaxFilas()
        {
            return (alto-altoCelda)/altoCelda;
        }
        
        public void setRelleno(int R,int G,int B)
        {
            if(isDrawable())
            {
                g.setColor(R, G, B);
                g.fillRect(fx+1,fy+1,getAncho()-1,getAlto()-1);
            }   
        }
        
        public void setBorde(int R,int G,int B)
        {
            if(isDrawable())
            {
                g.setColor(R, G, B);
                g.drawRect(fx,fy,getAncho(),getAlto());
            }   
        }
    }
   
    private int getAltoCelda()
    {
        Font fuente = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        return fuente.getHeight();
    }
    
    public void paint(Graphics g)
    { 
        Image img=null;
        // Borrar la pantalla
        cls(g);
        // dibujar tablero
        tablero(g, jugadores,true);
        seleccion(g,jugadores,true);
        lineas(g,partida.getLineas());
        //nombres de los jugadores
        for(int i=0;i<jugadores;i++)
        {
            (new Cell(g,i+1,0,jugadores,true)).escribirTitulo(partida.getNombres()[i],Grafico.getColor(i));
        }
        //puntuaciones y rondas
        for(int i=vScroll;i<partida.getRondas();i++)
        {
            try
            {
                (new Cell(g,0,i+1-vScroll,jugadores,true)).escribirAcumulado(partida.getCartas(i+1));
            }
            catch(Exception e){}
            for(int j=0;j<jugadores;j++)
            {
                try
                {
                    if(isMano(j+1, i+1))
                        (new Cell(g,j+1,i+1-vScroll,jugadores,true)).marcarMano();
                }
                catch(Exception e){}
                try
                {
                    (new Cell(g,j+1,i+1-vScroll,jugadores,true)).escribirParcial(partida.getParcial(j+1, i+1));
                }
                catch(Exception e){}
                try
                {
                    (new Cell(g,j+1,i+1-vScroll,jugadores,true)).escribirAcumulado(partida.getAcumulado(j+1, i+1));
                }
                catch(Exception e){}
            }
        }
    }
     /**
     *borra la pantalla
     * @param g pantalla
     */
    public void cls(Graphics g)
    {
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    
    /**
     * pinta las l?neas horizaontales de la pantalla, en funci?n de las
     * indicaciones contenidas en las reglas de partida
     * @param g pantalla
     * @param l array de lineas veritcales, indicando las posiciones donde va la
     * l?nea, viene indicada por las reglas de partida, e indica detr?s de que 
     * ronda se pinta la l?nea
     */
    public void lineas(Graphics g,int[]l)
    {
        for (int i=0;i<l.length;i++)
        {
            g.setColor(0,0,0);
            Cell test=new Cell(g, 0, l[i]-vScroll,jugadores, borde);
            g.drawLine(vBorde, test.ty, ancho-vBorde, test.ty);
        }
    }
    
    /** pinta el tablero*/
    public void tablero(Graphics g,int jugadores, boolean borde)
    {
        g.setColor(0,0,0);
        int inicio=0;
        if (borde)
        {
            inicio=vBorde;
            g.drawLine(inicio, 0, inicio, alto);
        }
        for(int i=1;i<jugadores;i++)
        {
            int x=((ancho-inicio)/jugadores)*i;
            x+=inicio;
            g.drawLine(x, 0, x, alto);
        }
        g.drawLine(inicio, altoCelda, ancho, altoCelda);
    }

    public void activeCell(Graphics g,int x,int y,int jugadores,boolean borde)
    {
        Cell celda=new Cell(g,x,y,jugadores,borde);
        if(celda.isDrawable())
        {
            g.setColor(128, 255, 128);
            g.fillRect(celda.fx,celda.fy,celda.getAncho(),celda.getAlto());
        }
    }


    public Tablero(Partida partida,CommandListener cl)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -