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

📄 kcanvas.java

📁 KCanvas class for java application
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
import java.util.*;

public abstract class KCanvas extends FullCanvas
{
    protected final static int FORM = 0;
    protected final static int LIST = 1;
    
    protected Font font;
    protected int iFontSize;
    protected int iTitleH;
    protected String strTitle;
    protected boolean bVertical;
    
    protected int iWidth;
    protected int iHeight;
    
    protected int iCanvasType;
    
    protected boolean bUpArrow;
    protected boolean bDownArrow;
    
    protected Image buffer;
    protected Graphics bg;
    
    private DirectGraphics dg;
    
    public Object oCanvasObj;
    
    public KCommandListener oListenerObj;
    
    private Vector vCommand;
    private Vector vAllCommand;
    private Vector vSubCommand;
    private boolean bMenu;
    private boolean bSubMenu;
    private int iMenuFocus;
    private int iSubMenuFocus;
    
    protected KCanvas()
    {   
        iFontSize = Font.SIZE_MEDIUM;
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, iFontSize);
        iTitleH = font.getHeight() + 4;
        
        bUpArrow = false;
        bDownArrow = true;
        
        bVertical = true;
        bMenu = false;
        bSubMenu = false;
        
        vCommand = new Vector();
        vSubCommand = new Vector();
        vAllCommand = new Vector();
    }
    
    protected void SetTitle(String title)
    {
        strTitle = title;
    }
    
    protected void addKCommand(KCommand command)
    {
        vAllCommand.addElement(command);
        
        if(command.root == null)
            vCommand.addElement(command);
    }
    
    protected void setKCommandListener(KCommandListener obj)
    {
        oListenerObj = obj;
    }
    
    private void RepaintCanvas()
    {
        if(iCanvasType == FORM)
        {
            ((KForm)oCanvasObj).repaint();
        }
        else if(iCanvasType == LIST)
        {
            ((KList)oCanvasObj).repaint();
        }
    }
    
    private void SubCommandKeyPress(int i)
    {
         int tus = getGameAction(i);
        
        switch(tus)
        {
            case UP:
                if(bVertical)
                {
                    iSubMenuFocus--;
                    if(iSubMenuFocus < 0)
                        iSubMenuFocus = vSubCommand.size() - 1;
                    
                    RepaintCanvas();
                }
                else
                {
                    bSubMenu = false;
                    
                    RepaintCanvas();
                }
                return;
                
            case DOWN:
                if(bVertical)
                {
                    iSubMenuFocus++;
                    if(iSubMenuFocus > vSubCommand.size() - 1)
                        iSubMenuFocus = 0;
                    
                    RepaintCanvas();
                }
                return;
                
            case LEFT:
                if(bVertical)
                {   
                    bSubMenu = false;
                    
                    RepaintCanvas();
                }
                else
                {
                    iSubMenuFocus++;
                    if(iSubMenuFocus > vSubCommand.size() - 1)
                        iSubMenuFocus = 0;
                    
                    RepaintCanvas();
                }
                return;
                
            case RIGHT:
                if(!bVertical)
                {
                   iSubMenuFocus--;
                    if(iSubMenuFocus < 0)
                        iSubMenuFocus = vSubCommand.size() - 1;
                    
                    RepaintCanvas();
                }
                return;
        }
    }
    
    private void OpenSubMenu(KCommand command)
    {
        bSubMenu = true;

        iSubMenuFocus = 0;

        vSubCommand.removeAllElements();

        for(int j = 1; j < vAllCommand.size(); j++)
        {
            KCommand cmd = (KCommand) vAllCommand.elementAt(j);

            if(cmd.root == command)
            {
               vSubCommand.addElement(cmd);
            }
        }

        RepaintCanvas();
    }
    
    private void CommandKeyPress(int i)
    {
         int tus = getGameAction(i);
        
        switch(tus)
        {
            case UP:
                if(bVertical)
                {
                    iMenuFocus--;
                    if(iMenuFocus < 1)
                        iMenuFocus = vCommand.size() - 1;
                    
                    RepaintCanvas();
                }
                return;
                
            case DOWN:
                if(bVertical)
                {
                    iMenuFocus++;
                    if(iMenuFocus > vCommand.size() - 1)
                        iMenuFocus = 1;
                    
                    RepaintCanvas();
                }
                else
                {
                   KCommand command = (KCommand) vCommand.elementAt(iMenuFocus);
                    
                   if(command.type == KCommand.SUB)
                   {
                        OpenSubMenu(command);
                   }
                }
                return;
                
            case LEFT:
                if(!bVertical)
                {   
                   iMenuFocus++;
                    if(iMenuFocus > vCommand.size() - 1)
                        iMenuFocus = 1;
                    
                    RepaintCanvas();
                }
                return;
                
            case RIGHT:
                if(bVertical)
                {
                   KCommand command = (KCommand) vCommand.elementAt(iMenuFocus);
                    
                   if(command.type == KCommand.SUB)
                   {
                        OpenSubMenu(command);
                   }
                }
                else
                {
                    iMenuFocus--;
                    if(iMenuFocus < 1)
                        iMenuFocus = vCommand.size() - 1;
                    
                    RepaintCanvas();
                }
                return;
        }
    }
    
    public synchronized void keyPressed(int i)
    {
        if(bSubMenu)
        {
            SubCommandKeyPress(i);
        }
        else if(bMenu)
        {
            CommandKeyPress(i);
        }
        else if(iCanvasType == FORM)
        {
            ((KForm)oCanvasObj).KeyPressed(i);
        }
        else if(iCanvasType == LIST)
        {
            ((KList)oCanvasObj).KeyPressed(i);
        }
        
        if(i == Const.SOFTKEY1)
        {
            if(bSubMenu)
            {
                oListenerObj.KCommandAction(((KCommand) vSubCommand.elementAt(iSubMenuFocus)) );
                
                bMenu = false;
                bSubMenu = false;
                
                RepaintCanvas();
            }
            else if(bMenu)
            {
                KCommand command = (KCommand) vCommand.elementAt(iMenuFocus);
                    
                if(command.type == KCommand.SUB)
                {
                    OpenSubMenu(command);
                }
                else
                {
                    oListenerObj.KCommandAction(((KCommand) vCommand.elementAt(iMenuFocus)) );

                    bMenu = false;

                    RepaintCanvas();
                }
            }
            else if(vCommand.size() > 2)
            {         
                bMenu = true;
                
                iMenuFocus = 1;
                
                RepaintCanvas();
            }
            else
            {
                oListenerObj.KCommandAction(((KCommand) vCommand.elementAt(1)) );
            }
        }
        else if(i == Const.SOFTKEY2)
        {
            if(bSubMenu)
            {
                bSubMenu = false;
                
                RepaintCanvas();
            }
            else if(bMenu)
            {
                bMenu = false;
                
                RepaintCanvas();
            }
            else
            {
                oListenerObj.KCommandAction(((KCommand) vCommand.elementAt(0)) );
            }
        }
        else if(getGameAction(i) == FIRE)
        {
            if(bSubMenu)
            {
                oListenerObj.KCommandAction(((KCommand) vSubCommand.elementAt(iSubMenuFocus)) );
                
                bMenu = false;
                bSubMenu = false;
                
                RepaintCanvas();
            }
            else if(bMenu)
            {
               KCommand command = (KCommand) vCommand.elementAt(iMenuFocus);
                    
               if(command.type == KCommand.SUB)
               {
                    OpenSubMenu(command);
               }
               else
               {
                    oListenerObj.KCommandAction(((KCommand) vCommand.elementAt(iMenuFocus)) );

                    bMenu = false;

                    RepaintCanvas();
               }
            }
            else if(iCanvasType == LIST)
            {
                oListenerObj.KCommandAction(KList.SELECT_COMMAND);
            }
        }
    }
    
    public synchronized void keyReleased(int i)
    {
        if(iCanvasType == FORM)
        {
            ((KForm)oCanvasObj).KeyReleased(i);
        }
    }
    
    private void DrawTitle(Graphics g)
    {
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, iFontSize);
        g.setFont(font);
        iTitleH = font.getHeight() + 4;
        
        int a = 0;
        
        for(int i = 0; i < iTitleH; i++)
        {
            g.setColor(0x99BEFF);
            g.drawLine(0 + a * i - 1, i, iWidth - a * i + 1, i);
            
            g.setColor(0xB2CFFF + i * 0x20100);
            g.drawLine(0 + a * i, i, iWidth - a * i, i);
        }
        
        g.setColor(0x99BEFF);
        g.drawLine(0 + a * (iTitleH - 1), iTitleH, iWidth - a * (iTitleH - 1), iTitleH);
        
        g.setColor(0xFFFFFF);
        g.drawString(".: " + strTitle + " :.", iWidth / 2 + 1, 2 + 1, g.TOP | g.HCENTER);
        
        g.setColor(0x000000);
        g.drawString(".: " + strTitle + " :.", iWidth / 2, 2, g.TOP | g.HCENTER);
    }
    
    private void DrawStatus(Graphics g)
    {
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, iFontSize);
        g.setFont(font);
        iTitleH = font.getHeight() + 4;
        
        for(int i = 0; i < iTitleH; i++)
        {
            g.setColor(0xB2CFFF + i * 0x20100);
            g.drawLine(0, iHeight - i, iWidth, iHeight - i);
        }
        
        g.setColor(0x99BEFF);
        g.drawLine(0, iHeight - iTitleH, iWidth, iHeight - iTitleH);
        
        //Command ile olusturulacak
        if(bVertical)
        {
            g.setColor(0x000000);

            if(bMenu)
                g.drawString("Se

⌨️ 快捷键说明

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