画布开发

CanvasCanvas绘图
画布属于低级界面
  Canvas的结构以及paint函数;showNotify,hideNotify函数
  设置画布全屏:setFullScreenMode(booleanmode)
  得到高度,宽度:getHeight,getWidth

在画布上绘制
  设置绘图颜色:Graphics.setColor:格式0xRRGGBB或者传入数字
  设置划线样式:Graphics.setStrokeStyle
  SOLID:实线;DOTTED:虚线
  画直线:Graphics.drawLine
  画矩形/实心矩形:Graphics.drawRect/fillRect
  画圆角矩形/圆角实心矩形:Graphics.drawRoundRect/fillRoundRect
  画弧线/填充弧线:Graphics.drawArc/fillArc
  填充三角形:Graphics.fillTriangle
  强制重画:Graphics.repaint

在画布上绘制:字符串和图片
  修改字体:Graphics.setFont()
  画字符串:Graphics.drawString()
  确定定位点: LEFT,HCENTER,RIGHT | BOTTOM,TOP,BASELINE
  通过font来得知字符串所占的宽度
  例如:将字画在屏幕正中心
画图片:
  Graphics.drawImage
  Graphics.drawRegion

按键和指针事件
按键事件
相应函数
  protected voidkeyPressed(int keyCode) :按下某个键
  protected voidkeyReleased(int keyCode) :释放某个键
  protected voidkeyRepeated(int keyCode):一直按某个键
  键的代码由常量决定
  特殊键被认为是游戏键,需要转为游戏键才能进行判断
  getGameAction(keyCode)
  getKeyCode(action)
  映射关系: 上下左右和手机上的上下左右键一致,GAME_A,B,C,D分别对应1,3,7,9键
package prj;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MIDlet2 extends MIDlet {
	private MyCanvas mc = new MyCanvas();
	private Display dis;
	protected void startApp() throws MIDletStateChangeException {
		dis = Display.getDisplay(this);
		mc.setTitle("按键画布");
		dis.setCurrent(mc);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	class MyCanvas extends Canvas{
		private Image img;
		private int x;
		private int y;
		private int TRANS = Sprite.TRANS_NONE;
		public MyCanvas(){
			try{
				img = Image.createImage("/img.png");
				x = 0;
				y = 0;
			}catch(Exception ex){}
		}
		public void paint(Graphics g){
			//画布清空
			g.setColor(0,0,255);
			g.fillRect(0,0,this.getWidth(),this.getHeight());
			g.drawRegion(img,0,0,img.getWidth(),img.getHeight(),TRANS,x,y,Graphics.LEFT|Graphics.TOP);
		}
		protected  void keyRepeated(int keyCode) {
			int action = this.getGameAction(keyCode);
			switch(action){
			case Canvas.UP:			y -= 5; 	break;
			case Canvas.DOWN:		y += 5; 	break;
			case Canvas.LEFT:		x -= 5; 	break;
			case Canvas.RIGHT:		x += 5; 	break;			
			}
			repaint();//调用paint函数重画
		}
		protected  void keyPressed(int keyCode) {
			int action = this.getGameAction(keyCode);
			if (action==Canvas.FIRE){
				switch(TRANS){
				case Sprite.TRANS_NONE:	TRANS = Sprite.TRANS_ROT90;	break;
				case Sprite.TRANS_ROT90:	TRANS = Sprite.TRANS_ROT180;	break;
				case Sprite.TRANS_ROT180:	TRANS = Sprite.TRANS_ROT270;	break;
				case Sprite.TRANS_ROT270:	TRANS = Sprite.TRANS_NONE;	break;
				}
			}
			repaint();//调用paint函数重画

		}
	}
	

}

指针事件
利用hasPointerEvents和hasPointerMotionEvents判断是否支持指针
  相应函数
  protected voidpointerDragged(intx,inty)
  protected voidpointerPressed(intx,inty)
  protected voidpointerReleased(intx,inty)
案例
动画模拟
  界面上有个小红球,要求能够慢慢掉下来然后弹起来
  额外要求:增加暂停和继续的功能
package prj;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MIDlet1 extends MIDlet {
	private BallCanvas bc = new BallCanvas();
	private Display dis;
	protected void startApp() throws MIDletStateChangeException {
		dis = Display.getDisplay(this);
		dis.setCurrent(bc);
	}
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}
	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	class BallCanvas extends Canvas implements Runnable,CommandListener{
		private int left = 50;
		private int top = 50;
		private int d = 100;
		private int DIR = 1;	//1:向下,2:向上
		
		private Command cmdPause = new Command("暂停",Command.SCREEN,1);
		private Command cmdResume = new Command("继续",Command.SCREEN,1);
		
		private Thread th;
		private boolean RUN = true;
		
		public BallCanvas(){
			this.addCommand(cmdPause);
			this.setCommandListener(this);
			th = new Thread(this);
			th.start();
		}
		public void commandAction(Command c,Displayable d){
			if(c==cmdPause){//按下暂停按钮
				this.removeCommand(cmdPause);
				this.addCommand(cmdResume);
				RUN = false;
				th = null;
			}
			else if(c==cmdResume){//按下继续按钮
				this.removeCommand(cmdResume);
				this.addCommand(cmdPause);
				RUN = true;
				th = new Thread(this);
				th.start();
			}
		}
		public void paint(Graphics g){
			g.setColor(0,255,0);//绿色
			g.fillRect(0,0,this.getWidth(),this.getHeight());
			g.setColor(255,0,0);
			g.fillArc(left,top,d,d,0,360);
		}
		public void run(){
			while(RUN){
				if(DIR==1){
					top+=3;
					d--;
					if(top>=this.getHeight()-d){
						DIR = 2;
					}
				}
				if(DIR==2){
					top-=3;
					d++;
					if(top<=50){
						DIR = 1;
					}
				}
				
				repaint();//重画
				try{
					Thread.currentThread().sleep(10);
				}catch(Exception ex){}				
			}
		}
	}

}

界面上每秒钟在随机位置出现随机颜色的数字0-9,要求用户快速反应,按下按键,如果按对,加1分,错误减2分,不按减1分,初始5分,减到0分,提示用户输了。
package prj;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MIDlet2 extends MIDlet {
	private NumberCanvas nc = new NumberCanvas();
	private Display dis;
	protected void startApp() throws MIDletStateChangeException {
		dis = Display.getDisplay(this);
		dis.setCurrent(nc);
	}
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}
	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	class NumberCanvas extends Canvas implements Runnable{		
		private char rndChar;
		private Random rnd = new Random();
		public NumberCanvas(){
			new Thread(this).start();
		}

		public void paint(Graphics g){
			Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
			g.setFont(font);
			g.setColor(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256));
			int x = rnd.nextInt(this.getWidth());
			int y = rnd.nextInt(this.getHeight());
			g.drawChar(rndChar, x, y, Graphics.TOP|Graphics.LEFT);
			System.out.println(rndChar);
		}
		public void run(){
			while(true){
				int number = '0' + rnd.nextInt(10);
				//0-9之间的数字,转成字符不是0-9
				rndChar = (char)number;
				repaint();
				try{
					Thread.sleep(100);
				}catch(Exception ex){}
			}
			}
		public void keyPressed(int keyCode){
			//将keyCode和rndChar表示的数字比较,评分
		}
		}
	}

你可能感兴趣的:(thread,游戏,C++,c,360)