Pham_loi
Chức vụ: 01:37:02, 27-05-2016 |
Tạo màn hình load game
bài này tôi sẽ hướng dẫn các bạn tạo màn hình loading game trong J2ME
Từ 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
https://i1.wp.com/nzoqla.bay.livefilestore.com/y1pDN__w9A2oZzcNeFugWN0OSYMc83YYIy2qi9IzazO6O0e5Kngb80RBxdkyv-7RYRc5h80iXOZDZ23t4BoplTSfSQ_2eO3E1pO/j2me2.png
Lớp Point.java
<?php
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;
}
}
?>
Copy code
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;
}
}
?>
Copy code
Lớp LoadingScreen.java
<?php
package 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();
}
}
?>
Copy code
package 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();
}
}
?>
Copy code
Lớp MainMidlet.java
các bạn thêm 1 midlet vào source package và code như sau
<?php
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) {
}
}
?>
Copy code
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) {
}
}
?>
Copy code
Nhấn F6 để chạy ứng dụng ta được kết quả :
: 0 ♥
Trực Tuyến:
Khách: 1