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

📄 screen.java

📁 J2ME做的打砖块游戏,是学校的课程项目
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import java.lang.System;public class Screen extends Canvas {    private Font fontSmall;    private Font fontMedium;    private Font fontLarge;    private Font font;    public static int width;    public static int height;    private static final int TL_ANCHOR = Graphics.TOP | Graphics.LEFT;    private static final int TH_ANCHOR = Graphics.TOP | Graphics.HCENTER;    private Image buf;	public Image brick_1;	public Image brick_2;	public Image paddle_1;	public Image background;	    public static Graphics GRAPHICS;    public static final int BACKGROUND = ThreeDColor.white.getRGB();    private int lastState;    private int lastScore;    private int lastLives;    private Engine engine;    private EngineState state;	public static int fontHeight;    private MIDlet midlet;    public Screen(MIDlet midlet) {        width = getWidth();        height = getHeight();        fontSmall = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,                 Font.SIZE_SMALL);        fontMedium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,                 Font.SIZE_MEDIUM);        fontLarge = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,                 Font.SIZE_LARGE);                font = height < 150 ? fontSmall : fontMedium;        fontHeight = font.getHeight();		try{			brick_1 = Image.createImage("/j8.png");			brick_2 = Image.createImage("/j9.png");			paddle_1= Image.createImage("/j6.png");			background = Image.createImage("/background.png");			background = scale(background, width, height);		}		catch(Exception ex)		{		}        buf = Image.createImage(width, height - (fontHeight + 5));        GRAPHICS = buf.getGraphics();        state = new EngineState();        lastState = -1;        lastScore = -1;        lastLives = -1;                this.midlet = midlet;    }	public Image scale(Image src, int width, int height)	{		long start = System.currentTimeMillis();		int scanline = src.getWidth();		int srcw = src.getWidth();		int srch = src.getHeight();		int buf[] = new int[srcw*srch];		src.getRGB(buf, 0, scanline, 0, 0, srcw, srch);		int buf2[] = new int[width*height];		for (int y = 0; y < height; y++)		{			int c1 = y * width;			int c2 = (y * srch / height) * scanline;			for (int x = 0; x < width; x++)			{				buf2[c1 + x] = buf[c2 + x * srcw / width];			}		}		Image img = Image.createRGBImage(buf2, width, height, true);		long end = System.currentTimeMillis();		return img;	}    public void setEngine(Engine engine) {        this.engine = engine;        engine.getState(state);        state.menu.setMIDlet(midlet);    }        public void keyPressed(int keycode) {        if (state.state == Engine.MENU) {            engine.setLastKeyPressed(System.currentTimeMillis());            state.menu.keyPressed(getGameAction(keycode));        } else {            engine.keyPressed(keycode, getGameAction(keycode));        }    }        public void keyReleased(int keycode) {        if (state.state != Engine.MENU) {            engine.keyReleased(keycode, getGameAction(keycode));        }    }        private void paintBackground(Graphics g) {        g.setColor(BACKGROUND);        g.fillRect(0, 0, width, height);    }    private void paintTitle(Graphics g) {        engine.getState(state);        if (lastState != Engine.TITLE) {            paintBackground(GRAPHICS);                    }         g.drawImage(buf, 0, 0, TL_ANCHOR);        g.setColor(BACKGROUND);        g.fillRect(0, 0, width, height);
		g.drawImage(background, 0, 0, g.TOP | g.LEFT);        g.setFont(fontLarge);        g.setColor(ThreeDColor.red.getRGB());        g.drawString("Bricks", width / 2, height / 2, TH_ANCHOR);    }    private void paintOver(Graphics g) {                paintBackground(g);        g.setFont(fontLarge);        g.setColor(ThreeDColor.red.getRGB());        g.drawString("Game", width / 2, height / 2 - 10, TH_ANCHOR);        g.drawString("Over", width / 2, height / 2 + 10, TH_ANCHOR);
		if (Engine.internetRank.length() == 0)
		{
			g.drawString("CANNOT UPLOAD SCORE", width / 2, height / 2 + 30, TH_ANCHOR);
		}
		else
		{
			g.drawString(Engine.internetRank, width / 2, height / 2 + 30, TH_ANCHOR);
		}
		//for (int i= 0; i < 100000; i++);    }

	public void paintMenu(Graphics g)
	{        engine.getState(state);                if (lastState != Engine.MENU) {            paintBackground(GRAPHICS);        }                 g.drawImage(buf, 0, 0, TL_ANCHOR);        g.setColor(BACKGROUND);        g.fillRect(0, 0, width, height);                       g.setFont(fontLarge);        g.setColor(ThreeDColor.black.getRGB());
		g.drawImage(background, 0, 0, g.TOP | g.LEFT);        for (int i = 0; i < state.menu.getItems().length; i++) {            if (state.menu.getItems()[i].isEnabled()) {                g.setColor(ThreeDColor.purple.getRGB());            } else {                g.setColor(ThreeDColor.gray.getRGB());            }            g.drawString(state.menu.getItems()[i].getText(), width / 2,                     height / 3 + 30 + 20 * i, TH_ANCHOR);        }                g.setColor(ThreeDColor.pink.getRGB());                g.drawString(state.menu.getItems()[state.menu.getSelected()].getText(),                 width / 2, height / 3 + 30 + 20 * state.menu.getSelected(),                 TH_ANCHOR);    }	public void paintProgress(Graphics g)	{		engine.getState(state);    if(state.currentNum==0)    {			g.drawImage(buf, 0, 0, TL_ANCHOR);			g.setColor(BACKGROUND);			g.fillRect(0, 0, width, height);  	}		if ( state.currentNum >  state.totalNum)			state.currentNum = state.totalNum;		int barWidth = 80, barHeight = 15;		g.setColor(ThreeDColor.blue.getRGB());
		g.drawString("Loading......", (width - 40) / 2, height * 2 / 5 - 35, TH_ANCHOR);		g.drawRect((width - 60) / 2, height * 2 / 5, barWidth, barHeight);		g.setColor(ThreeDColor.blue.getRGB());		g.fillRect((width - 60) / 2, height * 2 / 5, state.currentNum*4, barHeight);	}

	public void paintHelp(Graphics g)
	{
		engine.getState(state);
		paintBackground(g);
		g.setFont(fontLarge);
		g.setColor(ThreeDColor.black.getRGB());
		g.drawString("This game provides three level files", 0, 0, TL_ANCHOR);
		g.drawString("which are loaded locally, you can also", 0, fontHeight, TL_ANCHOR);
		g.drawString("download level from internet by pressi", 0, 2*fontHeight, TL_ANCHOR);
		g.drawString("ng Download Level in the Menu. You", 0, 3 * fontHeight, TL_ANCHOR);
		g.drawString("have three lives, your score will be", 0, 4 * fontHeight, TL_ANCHOR);
		g.drawString("upload to server after game is over", 0, 5 * fontHeight, TL_ANCHOR);
	}	public void paintNonetwork(Graphics g)
	{
		engine.getState(state);
		paintBackground(g);
		g.setFont(fontLarge);
		g.setColor(ThreeDColor.black.getRGB());
		g.drawString("No Network Connection", width / 2, height / 2 + 10, TH_ANCHOR);

	}    public void paint(Graphics g) {        engine.getState(state);        g.setFont(font);                if (state.state == Engine.TITLE) {            paintTitle(g);        } else if (state.state == Engine.MENU) {            paintMenu(g);        } else if (state.state == Engine.OVER) {            paintOver(g);            state.menu.resetSelected();            state.menu.showResumeItem(false);		}		else if (state.state == Engine.PROGRESS)		{			paintProgress(g);		}
		else if (state.state == Engine.HELP)
		{
			paintHelp(g);
		}
		else if (state.state == Engine.NONETWORK)
		{
			paintNonetwork(g);

		}
		else
		{
			if (state.levelStarted)
			{
				paintBackground(GRAPHICS);
				g.drawImage(buf, 0, 0, TL_ANCHOR);

				if (state.state == Engine.PLAY)
				{
					g.setColor(BACKGROUND);
					g.fillRect(0, 0, width, height);

					g.setColor(ThreeDColor.black.getRGB());
					g.drawString("Score: " + state.score, width * 2 / 32,
							height - (fontHeight + 3), TL_ANCHOR);

					if (state.nextScore == -1)
						g.drawString("Next: " + "-", width * 10 / 32,
							height - (fontHeight + 3), TL_ANCHOR);
					else
						g.drawString("Next: " + state.nextScore, width * 10 / 32,
							height - (fontHeight + 3), TL_ANCHOR);
					if (state.rank == -1)
						g.drawString("Rank: " + "-", width * 16 / 32,
								height - (fontHeight + 3), TL_ANCHOR);
					else
						g.drawString("Rank: " + state.rank, width * 16 / 32,
							height - (fontHeight + 3), TL_ANCHOR);
					g.drawString("Lives: " + state.lives, width * 12 / 16,
							height - (fontHeight + 3), TL_ANCHOR);

				}
				if (state.brickCount > 0)
				{
					for (int x = 0; x < state.brickNum; x++)
					{

						if (((Brick)state.bricks[x]).pictureNum == 0)
						{
							if (state.bricks[x].strong > 0)
								state.bricks[x].paint(g, brick_1);
						}
						else if (((Brick)state.bricks[x]).pictureNum == 1)
						{
							if (state.bricks[x].strong > 0)
								state.bricks[x].paint(g, brick_2);
						}
					}
				}


				if (state.ballNumber > 0)
				{
					for (int i = 0; i < state.ballNumber; i++)
						state.ball[i].paint(g);
				}
				if (state.paddleNumber > 0)
					state.paddle.paint(g);
				if (state.capsuleNumber > 0)
				{
					for (int i = 0; i < state.capsuleNumber; i++)
					{
						if (state.capsule[i].isUsed() == false)
							state.capsule[i].paint(g);
					}
				}
			}

			lastState = state.state;
			lastScore = state.score;
			lastLives = state.lives;
		}    }}

⌨️ 快捷键说明

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