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

📄 msgboard.java

📁 《神州》RPG游戏引擎
💻 JAVA
字号:
//绘制对话框的类

import javax.microedition.lcdui.*;
import java.util.*;


public class MsgBoard {
    private String msgStr;
    private int boardW;
    private int boardH;
    private int boardRows;
    private int leftMargin;
    private int topMargin;
    private Font myFont;
    private Graphics g;
    private Vector msgVC;
    private int msgPoint;
    
    
    private String[] msgArr;
    private int maxRow;
    private int showRows;
    private int currentRow;    
    private boolean drawUpArrow;
    private boolean drawDownArrow;
    private boolean drawClose;
    private Image arrow;
    private boolean isClosed;
    public boolean visible;
    
    public int getBoardRows() {
        return boardRows;
    }
    public void setBoardRows(int boardRows) {
        this.boardRows = boardRows;
        this.boardH=boardRows*myFont.getHeight()+10;        
    }
    
    public String[] getMsgArr() {
        return msgArr;
    }
    
    public MsgBoard(Canvas c,Graphics g)
    {
        myFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
        this.g=g;
        isClosed=true;
        
        boardRows=2; //设置显示2行文字
        leftMargin=10; //对话框左边距
        topMargin=c.getHeight()/2-myFont.getHeight()*boardRows/2-5; //对话框上边距
        boardW=c.getWidth()-leftMargin*2; //对话框宽度
    }
    public void scrollUp()
    {       
        currentRow-=boardRows;
        if(currentRow<0)currentRow=0;
    }
    public void scrollDown()
    {        
        if(currentRow+showRows-1<maxRow)currentRow+=boardRows;     
        if(currentRow>maxRow)currentRow=maxRow;
    }
    public boolean isEnd()
    {
        if(currentRow+showRows-1>=maxRow)
        {
            return true;
        }
        else
        {
            return false;    
        }        
    }
    public void init()
    {
        int charW=myFont.stringWidth("字"); //一个字宽度
        //int rowLen=(boardW-5)/charW;  //一行显示几个字
        boardH=myFont.getHeight()*boardRows+10;
        Msg msg=null;
        if(msgPoint>=0 && msgVC!=null && msgPoint<msgVC.size())
        {
            //System.out.println("取对话");
        msg=(Msg)msgVC.elementAt(msgPoint);
        }
        if(msg!=null)
        {
        msgStr=msg.getOwner()+":"+msg.getMsg();
        msgArr=Tools.splitString(msgStr,boardW-8);
        maxRow=msgArr.length-1;
        }
        arrow=Tools.getImage("/pic/arrow.png");
        
        showRows=boardRows;
        isClosed=false;
    }
    public void close()
    {
        isClosed=true;
    }
    public void show()
    {
        if(isClosed)return;
        
        g.setColor(0x618E3F);        
        g.drawRect(leftMargin,topMargin,boardW,boardH); //对话外框
        g.setColor(0xDAE6D1);
        g.fillRect(leftMargin+1,topMargin+1,boardW-1,boardH-1);  //对话框背景色
        
        //画滚动槽
        g.setColor(0xFFFFFF);
        int barx1=leftMargin+boardW-3;
        int bary1=topMargin+5;
        int barx2=barx1;
        int bary2=topMargin+boardH-5;
        g.drawLine(barx1,bary1,barx2,bary2);
        
        int barRows=maxRow+1;  //预定义滚动槽行数,不是一屏行数的整数倍则后面需要补行数
        int barStep=barRows/showRows;  //滚动屏数
        if(showRows*barStep<barRows)barStep++;  //屏数不足补屏数
        barRows=barStep*showRows; //重新计算滚动槽总行数
        
        int len=(bary2-bary1)*showRows/barRows;  //滚动条长度
        int toplen=(bary2-bary1)*currentRow/barRows;
        g.setColor(0x88B767);
        g.drawLine(barx1,bary1+toplen,barx2,bary1+toplen+len);
        
        g.setColor(0x074C0C);
        g.setFont(myFont);
        
        for(int i=currentRow;i<currentRow+showRows;i++)
        {
            if(msgArr!=null && i<msgArr.length)
            {
                g.drawString(msgArr[i],leftMargin+5,topMargin+5+myFont.getHeight()*(i-currentRow),Style.LT);
            }        
        }
        if(currentRow>0)drawUpArrow=true;
        if(currentRow+showRows-1<maxRow)
        {
            drawDownArrow=true;
            drawClose=false;
        }
        if(currentRow==0)drawUpArrow=false;
        if(currentRow+showRows-1>=maxRow)
        {
            drawDownArrow=false;
            drawClose=true;
        }
        
        if(drawUpArrow)
        {
            int x1=leftMargin+boardW/2;
            int y1=topMargin;            
            //g.drawRegion(arrow,0,0,11,6,Sprite.TRANS_NONE,x1,y1,CC);
            //g.drawImage(arrow_up,x1,y1,CC);
            Tools.drawRegion(g,arrow,0,0,11,6,x1,y1,Style.CC);
        }
        if(drawDownArrow)
        {
            int x1=leftMargin+boardW/2;
            int y1=topMargin+boardH;           
            //g.drawRegion(arrow,11,0,11,6,Sprite.TRANS_NONE,x1,y1,CC);
            //g.drawImage(arrow_down,x1,y1,CC);
            Tools.drawRegion(g,arrow,11,0,11,6,x1,y1,Style.CC);
        }
        if(drawClose)
        {
            int x1=leftMargin+boardW/2;
            int y1=topMargin+boardH;
            //g.drawImage(arrow_close,x1,y1,CC);
            Tools.drawRegion(g,arrow,22,0,9,9,x1,y1,Style.CC);
            //g.drawRegion(arrow,22,0,9,9,Sprite.TRANS_NONE,x1,y1,CC);
        }
    }
    
    public void nextMsg()
    {
    	msgPoint++;
    	if(msgPoint>msgVC.size()-1)
    	{
    		msgPoint=msgVC.size()-1;
    	}
    	setMsgStr(((Msg)msgVC.elementAt(msgPoint)).getMsg());
    	currentRow=0;
    	init();
    }
    
    

    public int getBoardH() {
        return boardH;
    }
    public int getBoardW() {
        return boardW;
    }
    public int getLeftMargin() {
        return leftMargin;
    }
    public String getMsgStr() {
        return msgStr;
    }
    public int getTopMargin() {
        return topMargin;
    }   
    public void setBoardW(int boardW) {
        this.boardW = boardW;
    }
    public void setLeftMargin(int leftMargin) {
        this.leftMargin = leftMargin;
    }
    public void setMsgStr(String msgStr) {
        this.msgStr = msgStr;
    }
    public void setTopMargin(int topMargin) {
        this.topMargin = topMargin;
    }

	public int getMsgPoint() {
		return msgPoint;
	}

	public void setMsgPoint(int msgPoint) {
		this.msgPoint = msgPoint;
	}

	public Vector getMsgVC() {
		return msgVC;
	}

	public void setMsgVC(Vector msgVC) {
		this.msgVC = msgVC;
	}
}

⌨️ 快捷键说明

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