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

📄 browsing.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Displayable;
import java.util.Enumeration;
import java.util.Vector;

public class Browsing extends Canvas implements Invoker, Runnable {
    private final int WIDTH=176, HEIGHT=220;
    public static final int TYPE_SET_HOMEDIR=3;
    private static String ROOT= "/";
    private static String UP = "..";
    
    private int dirIndex =0;
    private String currentDir="/";
    private Vector dirList = new Vector();
    private int dirSize=0;
    private int lastDirIndex=0;
    
    private Image folderImage;
    private Image fileImage;
    private Image downArrowImage;
    private Image upArrowImage;
    private Image bg;
    
    private boolean stopped=false;
    private int browserType;
    
    private Font small_Font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    private Font medium_Font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    private int font_medium_Height;
    private int font_small_Height;
    private int font_medium_Width;
    private int font_small_Width;
    private int visibleTopIndex;
    private int visibleBottomIndex;
    private int visibleBottomIndex_START;
    
    private Thread thread= null;
    private boolean dirIndexIsSound=false;
    protected FileAction fileAction= null;
    private Displayable next;
    
    public Browsing(Displayable next, int browserType) {
        setFullScreenMode(true);
        this.next=next;
        this.browserType=browserType;
        fileAction = new FileAction(this);
        setProperties();
        thread = new Thread(this);
        thread.start();
        new Thread(new Runnable() {
            public void run() {
                fileAction.getDirContent(currentDir);
            }
        }).start();
    }
    
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, 0);
        g.setColor(0x000000);
        g.setFont(medium_Font);
        g.drawString("请选择音乐文件:",5,5,0);
        g.drawString("上一层",125,199,0);
        g.drawString("选  择",5,199,0);
        g.setFont(small_Font);
        
        int lastIndex = visibleBottomIndex < dirSize? visibleBottomIndex:dirSize;
        int j = 0;
        for(int i = visibleTopIndex; i<lastIndex;++i, j++) {
            if(dirIndex==i) {
                g.setColor(83,148,166);
                g.drawRoundRect(1, font_medium_Height+(j+1)*font_small_Height-1,WIDTH-3, font_small_Height+2, 4, 2);
            }
            if(i<lastDirIndex)
                g.drawImage(folderImage,3,font_medium_Height+(j+1)*font_small_Height+2, 0);
            else
                g.drawImage(fileImage,3,font_medium_Height+(j+1)*font_small_Height+2, 0);
            g.drawString((String)dirList.elementAt(i), 19,font_medium_Height+(j+1)*font_small_Height, 0);
            g.setColor(0x00);
        }
        g.setColor(83,148,166);
        if(this.visibleBottomIndex<dirSize)
            g.drawImage(downArrowImage,WIDTH-16,HEIGHT-font_medium_Height*2, 0);
        if(this.visibleTopIndex>0)
            g.drawImage(upArrowImage,WIDTH-16,font_medium_Height+12, 0);
    }
    
    public void keyPressed(int key) {
        switch(key) {
            case -1:
                if(dirIndex==0)
                    return;
                if(visibleTopIndex==0) {
                    dirIndex--;
                    dirIndexIsSound=isSound( ((String)dirList.elementAt(dirIndex)) );
                } else {
                    dirIndex--;
                    visibleTopIndex--;
                    visibleBottomIndex--;
                    dirIndexIsSound=isSound( ((String)dirList.elementAt(dirIndex)) );
                }
                break;
            case -2:
                if(dirIndex>=dirList.size()-1)
                    return;
                else if(visibleBottomIndex>dirIndex+1) {
                    dirIndex++;
                    dirIndexIsSound=isSound(((String)dirList.elementAt(dirIndex)) );
                } else {
                    dirIndex++;
                    visibleBottomIndex++;
                    visibleTopIndex++;
                    dirIndexIsSound=isSound( ((String)dirList.elementAt(dirIndex)) );
                }
                break;
            case -5:
                if(dirIndexIsSound) {
                    this.stopped=true;
                    Start.fileURL = "file://"+currentDir+(String)dirList.elementAt(dirIndex);
                    Start.display.setCurrent(next);
                    break;
                }
                if(dirIndex<lastDirIndex)
                    traverse();
                break;
            case -6:
                if(dirIndexIsSound) {
                    this.stopped=true;
                    Start.fileURL = "file://"+currentDir+(String)dirList.elementAt(dirIndex);
                    Start.display.setCurrent(next);
                    break;
                }
                if(dirIndex<lastDirIndex)
                    traverse();
                break;
            case -7:
                dirIndex=0;
                traverse();
                break;
            case -11:
                this.stopped=true;
                Start.display.setCurrent(next);
                break;
        }
    }
    
    // 更新目录
    public void updateDirList(Enumeration e, boolean isRoot) {
        if(e==null)
            return;
        // 移出所有dirList内容
        dirList.removeAllElements();
        // 设置index数
        lastDirIndex=0;
        visibleTopIndex=0;
        visibleBottomIndex=visibleBottomIndex_START;
        dirIndex=0;
        // 如果不是根目录,画..
        if(!isRoot) {
            dirList.addElement("..");
            lastDirIndex++;
        }
        // 添加所有目录名称
        while (e.hasMoreElements()) {
            String s =(String)e.nextElement();
            if(s.endsWith("/"))
            {
                dirList.insertElementAt(s,lastDirIndex);
                lastDirIndex++;
            } else if(browserType!=TYPE_SET_HOMEDIR)
                dirList.addElement(s);
        }
        dirSize=dirList.size();
        dirIndexIsSound=false;
    }
    
    // 变换目录
    private void traverse() {
        String choosenDir = (String)dirList.elementAt(dirIndex);
        if (choosenDir.equals("..")) {
            int i = currentDir.lastIndexOf('/', currentDir.length()-2);
            if (i != -1)
                currentDir= currentDir.substring(0, i+1);
            else
                currentDir= "/";
            
            new Thread(new Runnable() {
                public void run() {
                    fileAction.getDirContent(currentDir);
                }
            }).start();
        } else if(dirIndex<lastDirIndex )
        {
            currentDir=currentDir+choosenDir;
            
            new Thread(new Runnable() {
                public void run() {
                    fileAction.getDirContent(currentDir);
                }
            }).start();
        }
    }
    
    public void run() {
        while(!stopped) {
            repaint();
            try {
                thread.sleep(10);
            } catch(Exception e){}
        }
    }
    
    // 判断是否是声音
    private boolean isSound(String im) {
        int length = im.length()-4;
        if(length<1)
            return false;
        
        String s = im.substring(length);
        System.out.println(s);
        
        if(s.equalsIgnoreCase(".mid"))
            return true;
        else if(s.equalsIgnoreCase(".mp3"))
            return true;
        else
            return false;
    }
    
    private void setProperties() {
        font_medium_Height= medium_Font.getHeight();
        font_small_Height= small_Font.getHeight();
        font_medium_Width= medium_Font.charWidth('w');
        font_small_Width= small_Font.charWidth('w');
        
        visibleTopIndex=0;
        visibleBottomIndex_START=(HEIGHT - 4* font_medium_Height)/font_small_Height;
        visibleBottomIndex=visibleBottomIndex_START;
        currentDir="/";
        
        try {
            folderImage = Image.createImage("/Image/folder.png");
            fileImage = Image.createImage("/Image/file.png");
            downArrowImage = Image.createImage("/Image/down.png");
            upArrowImage = Image.createImage("/Image/up.png");
            bg = Image.createImage("/Image/back.png");
        } catch(Exception e){ System.out.println(e.toString()); }
    }
}

⌨️ 快捷键说明

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