Bài giảng Lập trình J2ME cho thiết bị di động - Phần 2 - Trịnh Thị Vân Anh

ppt 34 trang hoanguyen 4020
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình J2ME cho thiết bị di động - Phần 2 - Trịnh Thị Vân Anh", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptbai_giang_lap_trinh_j2me_cho_thiet_bi_di_dong_phan_2_trinh_t.ppt

Nội dung text: Bài giảng Lập trình J2ME cho thiết bị di động - Phần 2 - Trịnh Thị Vân Anh

  1. LẬP TRÌNH J2ME CHO THIẾT BỊ DI ĐỘNG PHẦN 2 Giáo viên : Trịnh Thị Vân Anh Hà nội, 8-2007
  2. 3.Các thành phần giao diện ở mức thấp của ứng dụng MIDP ◼ Các hàm API cấp cao cho ta tạo ra giao diện các ứng dụng theo chuẩn, các hàm API cấp thấp cho ta thể hiện các ý tưởng của mình. Canvas và Graphics là 2 lớp chính của các hàm API cấp thấp. Bạn làm tất cả các công việc bằng tay. Canvas là một khung vẽ mà người phát triển vẽ lên thiết bị trình bày và xử lý sự kiện. Lớp Graphics cung cấp các công cụ vẽ như drawRoundRect() và drawString() ◼ Lớp Canvas:cung cấp một khung vẽ tạo giao diện tùy biến người dùng. Đa số các phương thức trong lớp này để xử lý sự kiện, vẽ ảnh và chuỗi lên thiết bị hiển thị. Trong phần này sẽ bao gồm các mục: • Hệ thống tọa độ • Tạo đối tượng Canvas • Vẽ lên trên đối tượng Canvas • Xử lý các sự kiện hành động • Xử lý các sự kiện phím nhấn • Xử lý sự kiện hành động của Game • Xử lý sự kiện con trỏ 2
  3. Hệ thống trục tọa độ, tạo một đối tượng Canvas ◼ Hệ tọa độ cho lớp Canvas: tâm tọa độ là điểm trái trên của thiết bị. Trị x tăng dần về phải, trị y tăng dần khi xuống dưới. Độ dày bút vẽ là một điểm ảnh. ◼ Các phương thức sau đây sẽ giúp xác định chiều rộng và chiều cao của canvas: • int getWidth(): xác định chiều rộng của canvas • int getHeight (): xác định chiều cao của canvas ◼ Đầu tiên tạo ra một lớp thừa kế từ lớp Canvas class TestCanvas extends Canvas implements CommandListener { private Command cmdExit; display = Display.getDisplay(this); cmdExit = new Command("Exit", Command.EXIT, 1); addCommand(cmdExit); setCommandListener(this); protected void paint(Graphics g) { // Draw onto the canvas g.setColor(255, 255, 255); // Set background color to white g.fillRect(0, 0, getWidth(), getHeight()); // Fill the entire canvas } } TestCanvas canvas = new TestCanvas(this); ◼ Phương thức paint của lớp Canvas cho phép bạn vẽ các hình dạng, vẽ ảnh, xuất chuỗi 3
  4. Sự kiện hành động ◼ Một Canvas có thể xử lý các Command. Chúng ta có thể xử lý các sự kiện Command trên thành phần Canvas cung cách như các thành phần khác ◼ Mã phím Trường hợp xử lý các hành động của các phím mềm, một Canvas có thể truy cập đến 12 mã phím. Những mã này được đảm bảo luôn luôn có trên bất kỳ các thiết bị MIDP nào KEY_NUM0 KEY_NUM1 KEY_NUM2 KEY_NUM3 KEY_NUM4 KEY_NUM5 KEY_NUM6 KEY_NUM7 KEY_NUM8 KEY_NUM9 KEY_STAR KEY_POUND Năm phương thức để xử lý các mã phím là: void keyPressed(int keyCode); void keyReleased(int keyCode); void keyRepeat(int keyCode); String getKeyName(int keyCode); ◼ Ví du sau: Xu ly cac phim, viet ra ma phim – KeyEvents, viet ra dung ham getKeyname() - KeyCodes 4
  5. Các hành động trong xử lý các trò chơi ◼ MIDP thường được sử dụng để tạo các trò chơi trên nền Java. Các hằng số sau đã được định nghĩa để xử lý các sự kiện có liên quan đến trò chơi trong MIDP UP DOWN LEFT RIGHT FIRE GAME_A GAME_B GAME_C GAME_D Đơn giản thì các giá trị này được ánh xạ thành các phím mũi tên chỉ hướng của thiết bị, nhưng không phải tất cả các thiết bị di động đều có những giá trị này. Nếu một thiết bị di động thiếu các phím mũi tên thì các hành động của trò chơi sẽ được ánh xạ vào các nút bấm, ví dụ phím trái được ánh xạ vào phím số 2, phím phải được ánh xạ vào phím số 5, và cứ tiếp tục như thế. 5
  6. Xác định các hành động của trò chơi ◼ Đoạn mã sau mô tả cách xác định các hành động của trò chơi để gọi các phương thức thích hợp dựa trên các hành động xảy ra protected void keyPressed(int keyCode) { int gameAction = getGameAction(keyCode); switch(gameAction) { case UP: mMessage = "UP"; break; case DOWN: mMessage = "DOWN"; break; case LEFT: mMessage = "LEFT"; break; case RIGHT: mMessage = "RIGHT"; break; case FIRE: mMessage = "FIRE"; break; case GAME_A: mMessage = "GAME_A"; break; case GAME_B: mMessage = "GAME_B"; break; case GAME_C: mMessage = "GAME_C"; break; case GAME_D: mMessage = "GAME_D"; break; default: mMessage = ""; break; } } ◼ Ví dụ: hiển thị các phím xử lý sự kiện - KeyMIDlet 6
  7. Lớp Graphics ◼ Chúng ta sử dụng đối tượng Graphics để vẽ lên một Canvas. ◼ boolean isColor(); //thiết bị có hỗ trợ hiển thị màu không? ◼ int numColors(); //gọi để xác định số màu Mặc định (DefaultColorPhone) là 4096 colors (0x1000) isColor=true; colorCount=0x1000; ◼ Các phương thức lấy về màu và thiết lập màu: void setColor(int RGB); //Đặt màu hiện thời qua giá trị RGB void setColor(int red, int green, int blue); // đặt màu, các giá trị red,green từ 0-255 int getColor(); // trả về màu hiện thời int getBlueComponent(); // trả về thành phần màu xanh da trời của màu hiện thời 0-255 int getGreenComponent();// trả về thành phần màu lục của màu hiện thời 0-255 int getRedComponent(); // trả về thành phần màu đỏ của màu hiện thời 0-255 void setGrayScale(int value); // đặt mức xám int getGrayScale(); //trả về giá trị xám từ 0-255 Ví dụ: BLACK = 0; WHITE = 0xffffff; RED = 0xf96868; GREY = 0xc6c6c6; LT_GREY = 0xe5e3e3; Hay int red = 0, green = 128, blue = 255; Sau đó đặt màu: g.setColor(WHITE); hoặc g.setColor(red, green, blue); 7
  8. Vẽ cung và hình chữ nhật ◼ Chọn nét khi vẽ đường thẳng, cung và hình chữ nhật trên thiết bị hiển thị. int getStrokeStyle(); //trả về kiểu nét vẽ void setStrokeStyle(int style); // đặt kiểu nét vẽ Hai kiểu nét vẽ được định nghĩa trong lớp Graphics là nét chấm, và nét liền g.setStrokeStyle(Graphics.DOTTED); g.setStrokeStyle(Graphics.SOLID); ◼ Vẽ cung: void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle); void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle); g.drawArc(10, 10, 100, 100, 0, 150); Đoạn mã trên yêu cầu vẽ một cung, cung này được bao bởi một hình chữ nhật có tọa độ điểm trái trên là (10, 10), chiều rộng và chiều dài là 100, góc bắt đầu là 0, góc kết thúc là 150. Ví dụ: VeCungCanvas ◼ Vẽ hình chữ nhật void drawRect(int x, int y, int width, int height);// void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight); void fillRect(int x, int y, int width, int height); void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight); Hình chữ nhật có 4 góc là tròn thì bạn phải xác định đường kính theo chiều ngang(arcWidth) và đường kính theo chiều dọc(arcHeight). Ví dụ: VeHinhChuNhat 8
  9. Font chữ ◼ Các phương thức dựng của lớp Font: Font getFont(int face, int style, int size); Font getFont(int fontSpecifier); Font getDefaultFont(); Một số thuộc tính của lớp Font FACE_SYSTEM FACE_MONOSPACE FACE_PROPORTIONAL STYLE_PLAIN STYLE_BOLD STYLE_ITALIC STYLE_UNDERLINED SIZE_SMALL SIZE_MEDIUM SIZE_LARGE Các tham số kiểu dáng có thể được kết hợp thông qua toán tử hay. Ví dụ Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_MEDIUM); Sau khi bạn có một tham chiếu đến một đối tượng Font, bạn có thể truy vấn nó để xác định thông tin của các thuộc tich của nó. Ví dụ: FontChuDonGian 9
  10. Điểm neo ◼ Để xác định tọa độ x, y của chuỗi ký tự được hiển thị, thì điểm neo cho phép bạn chỉ ra vị trí muốn đặt tọa độ x, y trên hình chữ nhật bao quanh chuỗi ký tự Chiều ngang LEFT (Bên trái) HCENTER (Chính giữa của chiều ngang) RIGHT (Bên phải) Chiều dọc TOP (Ở trên) BASELINE (Đường thẳng cơ sở) BOTTOM (Ở dưới) Sử dụng điểm neo phải chỉ ra tọa độ x, y của hình chữ nhật bao quanh. g.drawString("developerWorks", 0, 0 , Graphics.TOP | Graphics.LEFT); 10
  11. Vẽ các chuỗi ký tự ◼ void drawChar(char character, int x, int y, int anchor); void drawChars(char[] data, int offset, int length, int x, int y, int anchor); void drawString(String str, int x, int y, int anchor); ◼ protected void paint(Graphics g) { // Get center of display int xcenter = getWidth() / 2, ycenter = getHeight() / 2; // Choose a font g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALICS, Font.SIZE_MEDIUM)); // Specify the center of the text (bounding box) using the anchor point g.drawString("developerWorks", xcenter, ycenter, Graphics.BASELINE | Graphics.HCENTER); } ◼ Ví dụ: thay doi Font chu – FontChu1 Dịch chuyển đối tượng – Ve2 11
  12. Các lớp Game trong MIDP 2 ◼ Với MIDP 2 thì có thêm 5 lớp: • GameCanvas • Sprite • Layer • LayerManager • TiledLayer ◼ Các lớp này tìm trong gói: java.microedition.lcdui.game ◼ Chúng ta không dùng trực tiếp lớp Layer, vì thực chất đây là lớp trừu tượng dùng trong các lớp Spite, LayerManager và TiledLayer. ◼ Lớp GameCanvas: không phải đợi để thực hiện keyPressed() vì ta có phương thức getKeysState(). Có kỹ thuật gọi là “double buffering”bạn có thể thực hiện vẽ ở “off- screen buffer”khi đó việc copy từ buffer tới các canvas nhanh hơn nhiều. Có thể sử dụng Graphic từ gọi hàm getGraphics(). flushGraphics() để giải phóng Graphics ◼ Ví dụ ta có việc di chuyển chữ ‘x’ dùng GameCanvas – ExampleGameCanvas 12
  13. Lớp Sprite ◼ Trong Game thì đồ hoạ làm nên thành công rất lớn. Hầu hết các đối tượng đồ hoạ được phân loại như các dạng đặc biệt của đồ hoạ gọi là các Sprite. Một Sprite có thể là bullet, monster, enemies, keys và doors và một vài cái gì đó ◼ Các Sprite được nhân nhiều lên là các graphic động, các graphic động này được tạo nên từ cùng một Sprite nhưng nhìn từ các góc khác nhau. Đây là một bộ Sprite: ◼ Sprite Constructor Có 3 hàm khởi tạo với lớp Sprite Sprite (Image image); // Tạo ra khung Sprite đơn, không động Sprite (Sprite sprite); //Tạo ra Sprite mới từ một Sprite Sprite (Image image,int frameWidth, int frameHeight); //Tạo ra Sprite động với từ 2 frame trở lên, frameWidth và frameHeight là độ rộng và chiều cao của 1 Sprite ◼ Ta có tổng độ rộng là 160 pixels, độ rộng của 1 frame là 32 pixels, chiều cao là 32pixels. Ta có frameWidth và frameHeight là giống nhau cho 1bộ Sprite (các Sprite khác thì khác nhau). ◼ Các Graphic thì bao gồm các Sprite mà độ rộng và chiều cao là hằng số, vì số các pixel thì liên quan đến số màu: nếu 1pixel là 8-bit, 16-bit, 24-bit thì 28=256, 216=65536 màu 13
  14. Sprite Collision, Display Sprite Sequence ◼ Sprite Collision: collidesWith(Image image, int x, int y, boolean pixelLevel); collidesWith(Sprite sprite, boolean pixelLevel); collidesWidth(TiledLayer tiledLayer, Boolean pixelLevel); Kiểm tra đụng độ thông qua vị trí góc trái trên của Sprite ◼ Sprite Sequence: getFrameSequenceLength();//số phần tử trong 1dãy getFrame();//chỉ số hiện thời của frame trong dãy nextFrame();// chỉ tới frame tiếp theo prevFrame();// chỉ tới frame trước setFrame(int sequenceIndex);//đặt frame hiện thời 14
  15. Sprite Transforms ◼ Sprite Transforms: biến dạng có thể tạo Sprite như quay, lấy đối xứng setTransform(int transform); TRANS_NONE 0 TRANS_MIRROR_ROT180 1 TRANS_MIRROR 2 TRANS_ROT180 3 TRANS_MIRROR_ROT270 4 TRANS_ROT90 5 TRANS_ROT270 6 TRANS_MIRROR_ROT90 7 ◼ Ví dụ: ExampleGameSprite 15
  16. Lớp LayerManager ◼ LayerManager để quản lý các lớp Graphic, thứ tự của các lớp giống như chiều thứ 3 (trục z). Thứ tự 0 là lớp gần người dùng nhất. ◼ Thêm 1 lớp : append(Layer layer); private Sprite playerSprite; private Sprite backgroundSprite; private LayerManager layerManager; Image playerImage = Image.createImage("/transparent.png"); playerSprite = new Sprite (playerImage,32,32); Image backgroundImage = Image.createImage("/background.png"); backgroundSprite = new Sprite(backgroundImage); layerManager = new LayerManager(); layerManager.append(playerSprite); layerManager.append(backgroundSprite); ◼ remove(Layer layer);// loại bỏ một lớp ◼ insert(Layer layer, int index);// thêm 1 lớp ◼ paint(Graphics g, int x, int y); //muốn hiển thị 1 lớp Ví dụ : ExampleLayerManager 16
  17. LayerManager and Scrolling Background ◼ Đôi lúc màn hình nền lớn hơn màn hình hiển thị, lúc đó chúng ta phải cuộn màn hình. Có thể cuộn sang trái, sang phải, lên trên xuống dưới. ◼ Điểm bắt đầu là (50,20), ta có thể tạo ra điểm động để nó cuốn trên màn hình. if ((keyStates & LEFT_PRESSED) != 0) { if (scnX - 1 > 0) scnX ; } if ((keyStates & RIGHT_PRESSED) != 0) { if (scnX + 1 + 140 0) scnY ; } if ((keyStates & DOWN_PRESSED)!=0){ if (scnY+1+140<backgroundImage.getHeight()) scnY++; Ví dụ: CuonManHinhNen } 17
  18. Lớp TiledLayer ◼ Một TiledLayer là một lưới các ô chia ra từ 1 ảnh. ◼ Ví dụ hình bên được chia thành 6 vùng, ta chỉ ra các Tiled có 32x32 pixel. Tạo nên 1 lớp TiledLayer, mỗi 1 tile này được đánh số (bắt đầu từ 1). Đánh số từ trái sang phải rồi từ trên xuống dưới, ▪ TiledLayer(int columns, int rows, Image image, int tileWidth, int tileHeight); Có số hàng, cột và ảnh cần chia. Độ rộng và cao của tile ▪ setCell(int col, int row, int tileIndex);//đặt tile vào bức ảnh ở vị trí col,row và lấy ảnh có tileIndex (ở trên là từ 1,2, 6) ▪ getCell(int col, int row);//trả về index của cell, nếu cell là empty trả về 0 ▪ getCellHeight();//trả về chiều cao của một cell (pixel) ▪ getCellWidth(); ▪ getColumns();//trả về số cột của TileLayer ▪ getRows(); ▪ Giống như các Game khác, ta cũng gọi trực tiếp hàm paint() hay dùng LayerManager layerManager.paint(g,0,0); 18
  19. Ví dụ TiledLayer private LayerManager layerManager; private TiledLayer tiledBackground; tiledBackground = initBackground(); layerManager = new LayerManager(); layerManager.append(tiledBackground); private TiledLayer initBackground() throws Exception { Image tileImages = Image.createImage("/tiles.png"); TiledLayer tiledLayer = new TiledLayer(8,9,tileImages,32,32); int[] map = { 5, 1, 1, 4, 1, 1, 1, 1, 5, 1, 3, 1, 1, 3, 1, 1, for (int i=0; i < map.length; i++) { 5, 1, 2, 1, 1, 2, 1, 1, int column = i % 8; 5, 1, 2, 3, 1, 2, 1, 1, int row = (i - column) / 8; 5, 1, 4, 2, 1, 2, 1, 1, tiledLayer.setCell(column,row,map[i]); 5, 1, 1, 4, 1, 2, 1, 1, } 5, 1, 1, 1, 1, 4, 1, 1, return tiledLayer; 5, 1, 1, 1, 1, 1, 1, 1, } 5, 1, 1, 1, 1, 1, 1, 1, }; • Ví dụ : ExampleTiledLayer 19
  20. Animated Cells (1) ◼ Animated Cell là tập hợp động của các Tile tĩnh. Các Animated Tile là các chỉ số âm. int createAnimatedTile(int staticTileIndex); Tạo ra Animated Tile trả về chỉ số âm (-1,-2 ) setAnimatedtile(int animatedTileIndex, int staticTileIndex); kết hợp Animated Tile với stattic tile ◼ Tạo nền: private TiledLayer initBackground() throws Exception { Image tileImages = Image.createImage("/tiles.png"); TiledLayer tiledLayer = new TiledLayer(8,9,tileImages,32,32); int[] map = { for (int i=0; i < map.length; i++) { 5, 1, 1, 4, 1, 1, 1, 1, int column = i % 8; 5, 1, 3, 1, 1, 3, 1, 1, int row = (i - column) / 8; 5, 1, 2, 1, 1, 2, 1, 1, tiledLayer.setCell(column,row,map[i]); 5, 1, 2, 3, 1, 2, 1, 1, } 5, 1, 4, 2, 1, 2, 1, 1, animatedIdx = tiledLayer.createAnimatedTile(5); 5, 1, 1, 4, 1, 2, 1, 1, tiledLayer.setCell(1,1,animatedIdx); 5, 1, 1, 1, 1, 4, 1, 1, return tiledLayer; 5, 1, 1, 1, 1, 1, 1, 1, } 5, 1, 1, 1, 1, 1, 1, 1}; } 20
  21. Animated Cells (2) public ExampleGameCanvas() throws Exception { private boolean switchTile; super(true); private int animatedIdx; width = getWidth(); if (switchTile) { height = getHeight(); currentX = width / 2; tiledBackground.setAnimatedTile(animatedIdx,3); currentY = height / 2; } else { delay = 20; tiledBackground = initBackground(); tiledBackground.setAnimatedTile(animatedIdx,4); } layerManager = new LayerManager(); switchTile = !switchTile; layerManager.append(tiledBackground); layerManager.paint(g,0,0); } . public void run() { animatedIdx = tiledLayer.createAnimatedTile(5); Graphics g = getGraphics(); tiledLayer.setCell(1,1,animatedIdx); while (isPlay == true) { input(); Ví dụ: ExampleTiledLayerAnimated drawScreen(g); try { Thread.sleep(delay); } catch (InterruptedException ie) {} }} 21
  22. Ví dụ : Animation (1) ◼ Tạo Sprite tĩnh: AnimationSprite.java import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; public class AnimationSprite extends Sprite { public AnimationSprite(Image image, int frameWidth, int frameHeight) { super(image, frameWidth, frameHeight); } } ◼ Chạy Animation: public void start() { • Draw frame running = true; private void drawDisplay(Graphics g) Thread t = new Thread(this); { t.start(); } // Animated sprite, show next frame in sequence spSpiral.nextFrame(); public void run() { lmgr.paint(g, 0, 0); // Paint layers Graphics g = getGraphics(); flushGraphics(); while (running) { } drawDisplay(g); Try { Thread.sleep(150); } catch (InterruptedException ie) { System.out.println("Thread exception"); } }} 22
  23. Ví dụ : Animation (2), AnimationCanvas.java import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; public class AnimationCanvas extends GameCanvas implements Runnable { private static final int FRAME_WIDTH = 57; private static final int FRAME_HEIGHT = 53; private AnimationSprite spSpiral; // Animated sprite private LayerManager lmgr; // Manage layers private boolean running = false; // Thread running? public AnimationCanvas() { super(true); Try { spSpiral = new AnimationSprite(Image.createImage("/spiral.png"), FRAME_WIDTH, FRAME_HEIGHT); spSpiral.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2); spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2); 23
  24. Ví dụ : Animation (3) lmgr = new LayerManager(); private void drawDisplay(Graphics g) lmgr.append(spSpiral); } { catch (Exception e) { spSpiral.nextFrame(); System.out.println("Unable to read PNG image"); } lmgr.paint(g, 0, 0); } flushGraphics(); public void start() { } public void stop() running = true; { Thread t = new Thread(this); running = false; t.start(); } } public void run() { } Graphics g = getGraphics(); while (running) { drawDisplay(g); Try { Thread.sleep(150); } catch (InterruptedException ie) { System.out.println("Thread exception"); } } } 24
  25. Ví dụ : Animation (4), Animation.java import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Animation extends MIDlet implements CommandListener { private Display display; // Reference to display private AnimationCanvas canvas; // Game canvas private Command cmExit; // Exit command public Animation() { public void destroyApp(boolean unconditional) { display = Display.getDisplay(this); canvas.stop(); } cmExit = new Command("Exit", Command.EXIT, 1); public void commandAction(Command c, Displayable s) { if ((canvas = new AnimationCanvas()) != null) { if (c == cmExit) { canvas.addCommand(cmExit); destroyApp(true); canvas.setCommandListener(this); }} notifyDestroyed(); public void startApp() { } } } if (canvas != null) { display.setCurrent(canvas); canvas.start(); } } public void pauseApp() {} 25
  26. Ví dụ: Collisions, AppleSprite.java, CubeSprite.java và StarSprite.java import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.*; public class AppleSprite extends Sprite{ public class CubeSprite extends Sprite{ public AppleSprite(Image image) { public CubeSprite(Image image){ super(image); // Sprite constructor super(image); // Sprite constructor setRefPixelPosition(146, 35); // Set location on canvas // Set location on canvas }} setRefPixelPosition(120, 116);} import javax.microedition.lcdui.game.*; } import javax.microedition.lcdui.*; public class StarSprite extends Sprite { public StarSprite(Image image) { super(image); // Sprite constructor setRefPixelPosition(5, 65); // Set location on canvas }} 26
  27. Ví dụ: Collisions, AnimatedSprite.java import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; public class AnimatedSprite extends Sprite { public AnimatedSprite(Image image, int frameWidth, int frameHeight) { super(image, frameWidth, frameHeight); // Call sprite constructor }} ◼ Move Sprite: MoveLeft(), moveRight(int w), moveUp(), moveDown(int h) ◼ Khi muốn Move Sprite, ta phải lưu lại vị trí hiện thời saveXY() trong trường hợp Sprite đụng độ với Sprite khác, sau đó trả về vị trí trước đó restoreXY() private void saveXY() {// Save last position previous_x = x; previous_y = y;} public void restoreXY() { x = previous_x; y = previous_y; setPosition(x, y); } 27
  28. Ví dụ: Collisions, Di chuyển Sprite:ManSprite.java (1) import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; public class ManSprite extends Sprite{ private int x = 0, y = 0, // Current x/y previous_x, previous_y; // Last x/y private static final int MAN_WIDTH = 25; // Width in pixels private static final int MAN_HEIGHT = 25; // Height in pixels public ManSprite(Image image){ // Call sprite constructor super(image);} public void moveLeft() { if (x > 0) { // If the man will not hit the left edge saveXY(); // If less than 3 from left, set to zero, // otherwise, subtract 3 from current location x = (x < 3 ? 0 : x - 3); setPosition(x, y); }} 28
  29. Ví dụ: Collisions, Di chuyển Sprite:ManSprite.java (2) public void moveRight(int w) { if ((x + MAN_WIDTH) w) ? (w - MAN_WIDTH) : x + 3); setPosition(x, y); } } public void moveUp() { if (y > 0) {// If the man will not hit the top edge saveXY(); // If less than 3 from top, set to zero, // otherwise, subtract 3 from current location. y = (y < 3 ? 0 : y - 3); setPosition(x, y); } } 29
  30. Ví dụ: Collisions, Di chuyển Sprite:ManSprite.java (3) public void moveDown(int h){ if ((y + MAN_HEIGHT) h) ? (h - MAN_WIDTH) : y + 3); setPosition(x, y); }} private void saveXY() {// Save last position previous_x = x; previous_y = y; } public void restoreXY() { x = previous_x; y = previous_y; setPosition(x, y); } } 30
  31. Ví dụ: Collisions, CollisionCanvas.java (1) ◼ Trả về phím ấn: private void checkForKeys() { • Trả về true nếu có đụng độ int keyState = getKeyStates(); private boolean checkForCollision() { if ((keyState & LEFT_PRESSED) != 0) { if (spMan.collidesWith(spSpiral, true) || spMan.moveLeft(); spMan.collidesWith(spApple, true) || } spMan.collidesWith(spCube, true) || else if ((keyState & RIGHT_PRESSED) != 0) { spMan.collidesWith(spStar, true)) { spMan.moveRight(canvas_width); // Upon collision, restore the last x/y position } spMan.restoreXY(); else if ((keyState & UP_PRESSED) != 0) { return true; spMan.moveUp(); } } else else if ((keyState & DOWN_PRESSED) != 0) { return false; spMan.moveDown(canvas_height); } }} 31
  32. Ví dụ: Collisions, CollisionCanvas.java (2) ◼ Lớp CollisionCanvas public class CollisionCanvas extends GameCanvas implements Runnable { private AnimatedSprite spSpiral; // Animated sprite private static final int FRAME_WIDTH = 57; // Width of 1 frame private static final int FRAME_HEIGHT = 53; // Height of 1 frame private int canvas_width, canvas_height; // Save canvas info private ManSprite spMan; // Man (moveable) private AppleSprite spApple; // Apple (stationary) private CubeSprite spCube; // Cube " private StarSprite spStar; // Star " private LayerManager lmgr; // Manage all layers private boolean running = false; // Thread running? private Collisions midlet; // Reference to main midlet 32
  33. Ví dụ: Collisions, CollisionCanvas.java (3) public CollisionCanvas(Collisions midlet) { // Gamecanvas constructor super(true); // Create and add to layer this.midlet = midlet; manager lmgr = new LayerManager(); Try {// Nonanimated sprites lmgr.append(spSpiral); spMan = new ManSprite(Image.createImage("/man.png")); lmgr.append(spMan); spApple = new AppleSprite(Image.createImage("/apple.png")); lmgr.append(spApple); spCube = new CubeSprite(Image.createImage("/cube.png")); lmgr.append(spCube); spStar = new StarSprite(Image.createImage("/star.png")); lmgr.append(spStar); // Animated sprite spSpiral = new AnimatedSprite(Image.createImage("/spiral.png"), FRAME_WIDTH, FRAME_HEIGHT); // Change the reference pixel to the middle of sprite spSpiral.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2); // Center the sprite on the canvas // (center of sprite is now in center of display) spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2); 33
  34. Ví dụ: Collisions, Collisions.java public class Collisions extends MIDlet implements CommandListener { protected Display display; // Reference to display private CollisionCanvas canvas; // Game canvas private Command cmExit; // Exit command public Collisions() { display = Display.getDisplay(this); if ((canvas = new CollisionCanvas(this)) != null) { // Create game canvas and exit command cmExit = new Command("Exit", Command.EXIT, 1); canvas.addCommand(cmExit); canvas.setCommandListener(this); }} public void startApp() { if (canvas != null) { display.setCurrent(canvas); canvas.start(); } } 34