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

📄 welcometogame.java

📁 一个Java小程序
💻 JAVA
字号:
/*
 * @(#)WelcomeToGame.java	1.11 01/08/23
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	余煜辉
 */
package com.wuhua.mali;

import java.util.Timer;
import java.util.TimerTask;

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.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *此类是描述用户登陆游戏时候的欢迎界面,并在界面中采用低级事件处理方式对用户的选择进行工作
 */
public class WelcomeToGame extends Canvas implements CommandListener
{
    private Display display = null; //定义主屏幕对象
	private GameMenu gameMenu = null;
    private Timer timer ;  		   //定义一个计时器,
	private Command okCommand = null;
	//private Sprite spriteWelcome; //定义欢迎图象动画对象
	public AboutGame aboutGame = null;
    private int width;
    private int height;
    private int i;		 //控制动画变量
    private int fontHeight;		 //用于获得字体高度
	private boolean isStart = false;  //用于选种Start改变它的颜色判断
	private boolean isBack = false;
	private boolean isAbout = false;
	private int  select = 1;	//对选择的索引
    private Image [] image = {createImage("/mali/1.png"), createImage("/mali/2.png"), createImage("/mali/3.png")};
	private Image [] icon = {createImage("/icon/enter.png"), createImage("/icon/back.png"), createImage("/icon/about.png")};
	private int index = 0; //定义图象索引
    public WelcomeToGame() 
	{
        super();
		this.okCommand = new Command("OK", Command.OK, 2);
		this.addCommand(okCommand);
		this.setCommandListener(this);
		this.timer = new Timer();
		ScrollTask st = new ScrollTask();	//每个时间隔要运行程序的内部对象
		this.width = this.getWidth(); //得到屏幕的宽度
		this.height = this.getHeight(); //得到屏幕的高度
		this.i = this.height;
        Font font = Font.getDefaultFont(); //得到字体为默认
        fontHeight = font.getHeight(); //得到字体的高度,并一字体的宽度作为动画时的参考
		
		setCommandListener(this);
        this.timer.schedule(st, 100, 100);   //锁定时间进度为0.1秒,并放置这个时间隔要运行的程序。
                
    }

	public void showForm(Display display, GameMenu gameMenu)	  //显示游戏登陆窗体
	{
		this.display = display;
		this.gameMenu = gameMenu;
		this.display.setCurrent(this);
	}

	public Image createImage(String name)
	{
		Image image = null;
		try 
		{
			image = Image.createImage(name); // 注意路径
        }
        catch(Exception e) 
		{ 
			e.printStackTrace(); 
		}
		return image;
	}

    protected void paint(Graphics g)
    {
		if (index > 2)
		{
			index = 0;
		}
		g.setColor(255, 255, 255);	//设置底色为白色
        g.fillRect(0, 0, width, height); //填充整个屏幕
		g.drawImage(image[index], this.width/2, i - 15, Graphics.HCENTER | Graphics.VCENTER);
		g.setColor(255);
		g.setFont(Font.getFont(0, 1, 16));
		g.drawImage(icon[0], this.width/2 - 60, i + 60, Graphics.HCENTER | Graphics.VCENTER);  //绘制图标
		g.drawImage(icon[1], this.width/2 - 60, i + 75, Graphics.HCENTER | Graphics.VCENTER);
		g.drawImage(icon[2], this.width/2 - 60, i + 90, Graphics.HCENTER | Graphics.VCENTER);
		
	    g.drawString("Enter Game", this.width/2 - 50, i + 50, Graphics.LEFT | Graphics.TOP);   //绘制菜单
		g.drawString("Exit Game", this.width/2 - 50, i + 65, Graphics.LEFT | Graphics.TOP);
		g.drawString("About Game", this.width/2 - 50, i + 80, Graphics.LEFT | Graphics.TOP);

		g.setColor(255, 0, 0);	
		g.drawString("Menu", this.width/2 - 50, i + 30, Graphics.LEFT | Graphics.TOP);
		//g.drawString("Select", this.width/2 - 40, i + 120, Graphics.LEFT | Graphics.TOP);


	
		if (isStart == true)
		{
			g.setColor(100);
	        g.fillRect( 0, this.width/2+50, this.width, height/2-50); //填充整个屏幕
	        g.setColor(100);
			g.drawString("Enter Game", this.width/2 - 50, i + 50, Graphics.LEFT | Graphics.TOP);
			g.clipRect(0, this.width/2+50, this.width, height/2-50);
		}

		if (isBack == true)
		{
			g.setColor(0);
			g.drawString("Exit Game", this.width/2 - 50, i + 65, Graphics.LEFT | Graphics.TOP);
		}

		if (isAbout == true)
		{
			g.setColor(0);
			g.drawString("About Game", this.width/2 - 50, i + 80, Graphics.LEFT | Graphics.TOP);
		}

		/*if (isSelect == true)
		{
			g.setColor(0);
			g.drawString("Select", this.width/2 - 40, i + 120, Graphics.LEFT | Graphics.TOP);
		} */
    }
    
    public void keyPressed(int keyCode)	  //处理用户按键事件
    {
		int getKeyCode = this.getGameAction(keyCode);		//得到用户游戏按键事件并做出相应的反应
		if (getKeyCode == this.DOWN)
		{					
			switch (this.select)	 //索引加1表示菜单向下移动,即改变菜单的颜色
			{
				case 1 :  isStart = true;				
			              isBack = false;		
				  	      isAbout = false;
						  this.select++;	//加1,表示索引引到下一级菜单
					      break;
				case 2 :  isBack = true;
				          isStart = false;
						  isAbout = false;
						  this.select++;
						  break;
				case 3 :  isAbout = true;
				          isBack = false;
						  isStart = false;
						  this.select = 1;	//重新赋值为1,使它的索引回到1
						  break;
			}
			repaint();
		}

		/*if (getKeyCode == this.UP)
		{	
			switch (this.select)	 //索引加1表示菜单向下移动,即改变菜单的颜色
			{
				case 1 :  isStart = true;				
			              isBack = false;		
				  	      isAbout = false; 
						  this.select = 3;	//加1,使它的索引回到2,即菜单开头的下一级菜单				  
					      break;
				case 2 :  isBack = true;
				          isStart = false;
						  isAbout = false;
						  this.select--;	//减1,表示返回上一级菜单
						  break;
				case 3 :  isAbout = true;
						  isStart = false;
						  isBack = false;
						  this.select--;
						  break;
			}	
			repaint();
		}  */

    }  

    private class ScrollTask extends TimerTask	  //内部类对象,用于动画的计时器
    {
        public void run()
        {
            
            if (i > height / 2 - 30)   
            {
                i = i - fontHeight;
                repaint();
            } 
			else
            {
				index++;
				repaint();
                //timer.cancel();
            }
        }
    }

	public void commandAction(Command c,Displayable s)
	{
		if(c == okCommand)
		{
			if (isStart == true)
			{
				MaliCanvas m = new MaliCanvas();
				//m.reset();
				//GameThread game = new GameThread(m, display);
				//game.go();
				m.showForm(display, this);
			}
			if (isAbout == true)
			{
		        //定义关于信息,并初始化,信息内容
				String copyrightChines =  "  欢迎使用手机在线动感娱乐!\n"
			                        + "  希望我们的软件给你带来愉快!\n"
									+ "  玛莉赛跑规则是:\n"
									+ "  路程为110米,有11个栏杆\n"
									+ "  版本信息:1.0\n"
									+ "  技术支持:020-38491064\n"
									+ "  无花工作室版权所有 2004-2005。\n"
									+ "                     作者: 饶荣庆\n"
									+ "                     作者: 余煜辉\n";	
				String copyrightEnglish = "   Welcome to Innervation Disport On-Line!\n"
			                         + "   Best wish with we! \n" 
									 + "   version: 1.0\n"
									 + "   Telphone: 020-34891064\n"
									 + "   Copyright (c) 2004-2005 Wuhua Workroom, Inc. All rights reserved.\n"
									 + "                       @author 	Rao Rongqing\n"
									 + "                       @author  Yu yihui\n";  
	           
			 
					this.aboutGame = new AboutGame(copyrightChines, null);	
					this.aboutGame.showAbout(display);	  //显示关于游戏窗口
				 
				
			}
			if (isBack)
			{		
				this.display.setCurrent(gameMenu);
			}
		}
	}
}

⌨️ 快捷键说明

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