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

📄 canvas1.java

📁 J2ME 技术开发的扑克牌类游戏 是开源代码
💻 JAVA
字号:
/*
 * Canvas1.java
 *
 * Created on 2 de junio de 2005, 21:07
 */

package telefono;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * Este es un Midlet de ejemplo.
 * 
 * pinta un tablero de 4 jugadores con algunos resultados. 
 * Para ver que aspecto tendr?. Imprime en pantalla la resoluci?n 
 * de la pantalla (esta es su principal utildad).
 * @author Enrique Vicent Ramis
 */
public class Canvas1 extends MIDlet implements CommandListener
{
    
    private Command exitCommand;
    private Display display;
    private SSCanvas screen;
    
    public Canvas1()
    {
        display=Display.getDisplay(this);
        exitCommand = new Command("Salir",Command.SCREEN,2);
        
        screen=new SSCanvas();
        screen.addCommand(exitCommand);
        screen.setCommandListener(this);
    }
    
    public void startApp() throws MIDletStateChangeException
    {
        display.setCurrent(screen);
    }
    
    public void pauseApp()
    {}
    
    public void destroyApp(boolean unconditional)
    {}
    
    public void commandAction(Command c, Displayable s)
    {
        
        if (c == exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        
    }
    
}

class SSCanvas 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;
    
    
   
    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_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
                g.setFont(fuente);
                g.drawString(Integer.toString(valor), fx,ty,Graphics.BASELINE|Graphics.LEFT);
            }
        }
        /** 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*/
        public void escribirTitulo(String valor)
        {
            if(this.isDrawable())
            {
                g.setColor(0,0,0);
                Font fuente = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL);
                g.setFont(fuente);
                g.drawString(valor, fx+getAncho()/2,ty,Graphics.BASELINE|Graphics.HCENTER);
            }
        }
        /** 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);
        (new Cell(g,1,0,jugadores,true)).escribirTitulo("Chema");
        (new Cell(g,2,0,jugadores,true)).escribirTitulo("Dani");
        (new Cell(g,3,0,jugadores,true)).escribirTitulo("Rafa");
        (new Cell(g,4,0,jugadores,true)).escribirTitulo("Quique");
        (new Cell(g,sx,sy,jugadores,true)).setBorde(128, 255, 128);
        (new Cell(g,sx,sy,jugadores,true)).setRelleno(232, 255, 232);
        (new Cell(g,1,1,jugadores,true)).escribirParcial(10);
        (new Cell(g,2,1,jugadores,true)).escribirParcial(15);
        (new Cell(g,3,1,jugadores,true)).escribirParcial(-5);
        (new Cell(g,1,2,jugadores,true)).escribirParcial(-10);
        (new Cell(g,2,2,jugadores,true)).escribirParcial(20);
        (new Cell(g,3,2,jugadores,true)).escribirParcial(10);
        (new Cell(g,4,2,jugadores,true)).escribirParcial(10);
        (new Cell(g,4,1,jugadores,true)).escribirParcial(10);
        (new Cell(g,1,1,jugadores,true)).escribirAcumulado(10);
        (new Cell(g,2,1,jugadores,true)).escribirAcumulado(15);
        (new Cell(g,3,1,jugadores,true)).escribirAcumulado(-5);
        (new Cell(g,4,1,jugadores,true)).escribirAcumulado(10);
        (new Cell(g,1,2,jugadores,true)).escribirAcumulado(0);
        (new Cell(g,2,2,jugadores,true)).escribirAcumulado(35);
        (new Cell(g,3,2,jugadores,true)).escribirAcumulado(5);
        (new Cell(g,4,2,jugadores,true)).escribirAcumulado(20);
        // Poner texto
        Font fuente = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        g.setFont(fuente);
        
        g.drawString("J2ME:"+ancho+"X"+alto, getWidth()/2, getHeight()/2,Graphics.BASELINE|Graphics.HCENTER);
        
        
        // Cargar y mostrar gr?fico
        try
        {
            img = Image.createImage("/logo.png");
        }
        catch (Exception e)
        {
            System.err.println("error: " + e);
        }
        
        g.drawImage(img, getWidth()/2, 40, Graphics.HCENTER|Graphics.VCENTER);
        
    }
     /**
     *borra la pantalla
     */
    public void cls(Graphics g)
    {
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    
    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 SSCanvas()
    {
        altoCelda=getAltoCelda()+1;
        alto=getHeight();
        ancho=getWidth();
        jugadores=4;
        
    }
    
    public void keyPressed(int keyCode) 
    {
        
        int action=getGameAction(keyCode);

        switch (action) 
        {

            
            case KEY_NUM4:
                break;
            case KEY_NUM5:
                break;
            case KEY_NUM6:
                break;
            case KEY_NUM2:
                break;
            case KEY_NUM8:
                break;
            case FIRE:
                // Disparar
                break;
            case LEFT:
                sx--;
                this.repaint();
                break;
            case RIGHT:
                sx++;
                this.repaint();
                break;
            case UP:
                sy--;
                this.repaint();
                break;
            case DOWN:
                sy++;
                this.repaint();
                break;
        }
        
    }
}

⌨️ 快捷键说明

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