J2ME – Tạo màn hình load game
15.07.2014/14:50
bài này tôi sẽ hướng dẫn các bạn tạo màn hình loading game trong J2METừ Netbeans các bạn tạo mới Project J2ME, chọn cấu hình CLDC 1.0 , MIDP 2.0 , chọn device là DefaultcldcMsaPhone1 ta xây dựng 3 lớp Point.java ,LoadingScreen.Java, và Midlet : MainMidlet.java
1. Lớp Point.java
package Zstar;
/**
*
* @author zstar
*/
public class Point {
private int X = 0;
private int Y = 0;
public Point() {
}
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public int getX() {
return this.X;
}
public int getY() {
return this.Y;
}
}
2. Lớp LoadingScreen.javapackage Zstar;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
/**
* @author ZSTAR
*/
public class LoadingScreen extends GameCanvas {
private Graphics g;
private String logo = "/Images/logo.png";
private String text1 = "Game tiếng việt trên điện thoại";
private String text2 = "Đang tải...";
private Image imageLogo;
private Point root = new Point(30, 50);
private Font fontNormal;
private Font fontBold;
//thanh loading
private int colorBorder = 0x000000;
private int colorLoading = 0xcc3301;
private int loadingLength = 150;
private int loadingHeight = 5;
private int loadingCurrent = 0;
private int h = 10;
private MainMidlet mainMidlet;
public LoadingScreen(MainMidlet main) {
super(false);
this.mainMidlet = main;
this.g = this.getGraphics();
this.fontNormal = g.getFont();
this.fontBold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
setFullScreenMode(true);
try {
imageLogo = Image.createImage(logo);
} catch (IOException ex) {
ex.printStackTrace();
}
this.root = new Point(getWidth() / 2 - imageLogo.getWidth() / 2, root.getY());
}
public void start() {
clearScreen();
draw();
draw_next_loading();
}
public void draw() {
g.setColor(0x000000);
g.drawImage(imageLogo, root.getX(), root.getY(), Graphics.TOP | Graphics.LEFT);
g.drawString(text1, getWidth() / 2 - fontNormal.stringWidth(text1) / 2, root.getY() + imageLogo.getHeight(), Graphics.TOP | Graphics.LEFT);
g.setFont(fontBold);
g.drawString(text2, getWidth() / 2 - fontBold.stringWidth(text2) / 2, root.getY() + imageLogo.getHeight() + 40, Graphics.TOP | Graphics.LEFT);
g.setFont(fontNormal);
//draw loading
g.setColor(colorBorder);
g.drawRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
g.setColor(colorLoading);
g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingCurrent, loadingHeight);
g.setColor(colorBorder);
this.flushGraphics();
}
private void draw_next_loading() {
clearLoading();
//timer.schedule(task, interval,interval);
for (int i = 0; i < loadingLength / h; i++) {
if (loadingCurrent < loadingLength) {
loadingCurrent += h;
//draw loading
g.setColor(colorBorder);
g.drawRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
g.setColor(colorLoading);
g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingCurrent, loadingHeight);
g.setColor(colorBorder);
this.flushGraphics();
try {
Thread.sleep(100);
} catch (Exception e) {
}
} else {
loadingCurrent = 0;
break;
}
}
}
private void clearLoading() {
g.setColor(0xffffff);
g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
this.flushGraphics();
}
public void clearScreen() {
g.setColor(0xffffff); // white
g.fillRect(0, 0, getWidth(), getHeight());
this.flushGraphics();
}
}
3. Lớp MainMidlet.javacác bạn thêm 1 midlet vào source package và code như sau
package Zstar;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
/**
* @author ZSTAR
*/
public class MainMidlet extends MIDlet {
private LoadingScreen loadingScreen;
public void startApp() {
loadingScreen=new LoadingScreen(this);
Display.getDisplay(this).setCurrent(loadingScreen);
loadingScreen.start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Nhấn F6 để chạy ứng dụng ta được kết quả :Tag:
Bạn đến từ:
Tool tiện ích admin,KhoGame360,Blog thủ thuật,kenh380
,truyen23h
,tai hinh nen naruto,kenhpro - wap hay,vmt - wap hay, Trần Phú Hiền Blog
,truyen23h
,tai hinh nen naruto,kenhpro - wap hay,vmt - wap hay, Trần Phú Hiền Blog