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

📄 pagecanvas.java

📁 Java手机游戏源代码(企业公司源代码)供初学者参考。
💻 JAVA
字号:
package com.gt.mrs.ui.canvas;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.gt.mobile.BaseCanvas;
import com.gt.mobile.BaseMIDlet;
import com.gt.mobile.ImageManager;
import com.gt.mrs.mvc.context.ContextObject;
import com.gt.mrs.mvc.handler.MainHandler;
import com.gt.mrs.mvc.handler.subhandler.downloaded.DownloadedSongPage;
import com.gt.mrs.mvc.handler.subhandler.main.MainPage;
import com.gt.mrs.mvc.handler.subhandler.songs.SongsPage;
import com.gt.mrs.mvc.renderor.Renderor;
import com.gt.mrs.mvc.uimodel.LineItem;
import com.gt.mrs.mvc.uimodel.MediaInfoItem;
import com.gt.mrs.mvc.uimodel.Page;
import com.gt.mrs.mvc.uimodel.PageItem;
import com.gt.mrs.mvc.uimodel.TextIconItem;
import com.gt.mrs.ui.RenderConfig;
import com.gt.mrs.ui.uiitem.LineUiItem;
import com.gt.mrs.ui.uiitem.MediaInfoUiItem;
import com.gt.mrs.ui.uiitem.PageUiItem;
import com.gt.mrs.ui.uiitem.ScrollPaneItem;
import com.gt.mrs.ui.uiitem.TextIconUiItem;

public class PageCanvas extends BaseCanvas implements Renderor{

	private String title=null;
	private int titleInnerV=3;
	private int bottomInnerV=3;
	private ScrollPaneItem scrollPaneItem=null;
	private Font font=RenderConfig.font_default;
	private int titleHeight=0;
	private int bottomHeight=0;
	
	private Page page;
	private ContextObject context;
	private int colorBg=0x000000;
	private int colorFg=0xffffff;
	
	private int colorLine=0xdddddd;//for temp;
	
	private static Image iconBg=null;
	private static Image iconBottomLeft=null;
	private static Image iconBottomRight=null;
	
	private PageCanvasListener listener;
	
	static{
		iconBg=ImageManager.getPngImage("bgIcon");
		iconBottomLeft=ImageManager.getPngImage("bottomLeft");
		iconBottomRight=ImageManager.getPngImage("bottomRight");
	}
	
	public PageCanvas(){
		super();
		titleHeight=titleInnerV*2+font.getHeight();
		bottomHeight=bottomInnerV*2+iconBottomRight.getHeight();
		scrollPaneItem=new ScrollPaneItem(super.getCanvasWidth(),super.getCanvasHeight()-titleHeight-bottomHeight);
		scrollPaneItem.setPosition(0,titleHeight);
	}
	
	public void render(){
		if(page!=null){
			render(page,context);
		}
	}
	
	public void render(Page page, ContextObject context){
		this.page=page;
		this.context=context;
		this.title=page.getTitle();
		scrollPaneItem.removeAllItems();
		scrollPaneItem.resetIndexPoint();
		Vector items=page.getItems();
		for(int i=0;i<items.size();i++){
			PageItem pageItem=null;
			pageItem=(PageItem)items.elementAt(i);
			PageUiItem pageUiItem=null;
			if(pageItem instanceof TextIconItem){
				pageUiItem=new TextIconUiItem(pageItem);
			}
			else if(pageItem instanceof LineItem){
				pageUiItem=new LineUiItem(pageItem);
				pageUiItem.setHeight(10);
			}
			else if(pageItem instanceof MediaInfoItem){
				pageUiItem=new MediaInfoUiItem(pageItem);
			}
			else{
				pageUiItem=new PageUiItem(pageItem);
			}
			pageUiItem.setSize(scrollPaneItem.getWidth(), pageUiItem.getHeight());
			scrollPaneItem.addUiItem(pageUiItem);
		}
		System.out.println("PageCanvas renderPage>>>"+scrollPaneItem.size());
	}
	
	public Page getPage(){
		return page;
	}
	
	public void setListener(PageCanvasListener listener){
		this.listener=listener;
	}
	
	private void showPage(String name){
		if(listener!=null){
			listener.showPage(name);
		}
	}

	private void drawTitleBottom(Graphics g){
		int bottomY=super.getCanvasHeight()-bottomHeight;
		g.setColor(colorBg);
		g.fillRect(0, 0, super.getCanvasWidth(), titleHeight);
		g.fillRect(0,bottomY , super.getCanvasWidth(), bottomHeight);
		g.setColor(colorFg);
		g.setFont(font);
		if(title!=null){
			g.drawString(title, (super.getCanvasWidth()-g.getFont().stringWidth(title))/2,titleInnerV, 0);
		}	
		//temp
		g.drawImage(iconBottomLeft, 0, bottomY+bottomInnerV, 0);
		g.drawImage(iconBottomRight, super.getCanvasWidth()-iconBottomRight.getWidth(), bottomY+bottomInnerV, 0);
		
		//
		g.setColor(colorLine);
		g.drawLine(0, titleHeight-1, super.getCanvasWidth(), titleHeight-1);
		g.drawLine(0, bottomY, super.getCanvasWidth(), bottomY);
	}
	
	protected void draw(Graphics g) {
		g.setColor(colorBg);
		g.fillRect(0, 0, super.getCanvasWidth(), super.getCanvasHeight());
		drawTitleBottom(g);
		g.setFont(font);
		
		if(iconBg!=null){
			g.drawImage(iconBg, (super.getCanvasWidth()-iconBg.getWidth())/2,
					(super.getCanvasHeight()-iconBg.getHeight())/2, 0);
		}
		scrollPaneItem.draw(g);
		if(iconBg!=null){
			g.drawImage(iconBg, (super.getCanvasWidth()-iconBg.getWidth())/2,
					(super.getCanvasHeight()-iconBg.getHeight())/2, 0);
		}
	}

	protected void handleKeyPress(int keyCode) {
		switch(keyCode){
		case Canvas.KEY_NUM2:
			scrollPaneItem.moveUp();
			break;
		case Canvas.KEY_NUM8:
			scrollPaneItem.moveDown();
			break;
		case Canvas.KEY_NUM5:
			//notyet//just for test
			if(page instanceof MainPage){
				showPage(MainHandler.DownloadedMenu);
			}
			else if(page instanceof DownloadedSongPage){
				showPage(MainHandler.SongsMenu);
			}
			else if(page instanceof SongsPage){
				showPage(MainHandler.PlayPage);
			}
			break;
		case Canvas.KEY_NUM3:
			BaseMIDlet.exit();
			break;
		}
	}
	
}

⌨️ 快捷键说明

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