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

📄 gamecanvas.java

📁 J2ME的源码!我以前学习J2ME的源码哈!非常适合初学者
💻 JAVA
字号:
package gameCanvas;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class gameCanvas extends GameCanvas implements Runnable {

	private boolean is;

	private int X, Y;

	private int Width;

	private int height;

	protected gameCanvas() {
		super(true);
		Width = getWidth();
		height = getHeight();

		X = Width / 2;
		Y = height / 2;
		start();
	}

	public void start() {
		is = true;
		Thread t = new Thread(this);
		t.start();
	}

	public void stop() {
		is = false;
	}

	public void run() {
		Graphics g = getGraphics();
		while (is) {

			input();
			draw(g);
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {

			}

		}

	}

	private void input() {
		int keyStates = getKeyStates();

		if ((keyStates & LEFT_PRESSED) != 0) {
			X -= 5;
			if (X <= 10) {
				X = Width /2 - 10;
	
			}
		}

		// X=Math.max(10, X-5);//10为X的半径

		if ((keyStates & RIGHT_PRESSED) != 0) {
			// if(X+10<Width)
			X = X + 5;
			if (X >= Width - 20) {
				X = Width /2 - 10;//若需其从边框边界上移动出,写为X=Width-20; 
				
				
			}
		}
		// X=Math.min(Width-20, X+5);//20为X的直径

		if ((keyStates & UP_PRESSED) != 0) {
			// Y=Math.max(10,Y-5);
			Y -= 5;
			if (Y <= 10) {
				Y = height/2 - 10;
			}
		}
		
		if ((keyStates & DOWN_PRESSED) != 0) {
			// if(Y+10<height)
			Y += 5;
			if (Y >= height - 20) {
				Y = height/2 - 10;
			}
		}
		// Y=Math.min(height-20, Y+5);//本句只是在为Y进入赋值

	}
		/*
		private void input() {
		int keyStates = getKeyStates();

		if ((keyStates & LEFT_PRESSED) != 0) {
			X -= 5;
			if (X <= 10) {
				X = Width - 20;
	
			}
		}

		// X=Math.max(10, X-5);//10为X的半径

		if ((keyStates & RIGHT_PRESSED) != 0) {
			// if(X+10<Width)
			X = X + 5;
			if (X >= Width - 20) {
				X = 0;
			}
		}
		// X=Math.min(Width-20, X+5);//20为X的直径

		if ((keyStates & UP_PRESSED) != 0) {
			// Y=Math.max(10,Y-5);
			Y -= 5;
			if (Y <= 10) {
				Y = height - 20;
			}
		}
		if ((keyStates & DOWN_PRESSED) != 0) {
			// if(Y+10<height)
			Y += 5;
			if (Y >= height - 20) {
				Y = 0;
			}
		}
		// Y=Math.min(height-20, Y+5);//本句只是在为Y进入赋值

	}

		*/


	private void draw(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, Width, height);
		g.setColor(255, 255, 255);
		g.fillArc(X, Y, 20, 20, 0, 90);//0代表初始弧度;
		flushGraphics();

	}

}

⌨️ 快捷键说明

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