博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

J2ME游戏开发之几何图形的绘制

Posted on 2011-08-17 19:33  zhangweia  阅读(233)  评论(0编辑  收藏  举报

1. 方法

a. 绘制空心矩形:

       drawRect(int x, int y , int width, int height);

b. 绘制实心矩形:

      fillRect(int x, int y , int width, int height);

c. 绘制椭圆

2. code

package com.sliw.graphics;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class GeometryCanvas extends Canvas{

    int startY = 10;
    int height = 20;
    int tip = 10;
    
    protected void paint(Graphics g) {
        // TODO Auto-generated method stub
        clear(g);
        
        /** 绘制空心的矩形 */
        drawBlankRect(g);    
        startY = startY + height + tip;
        
        /** 绘制实心的矩形**/
        drawFillRect(g);
        startY = startY + height + tip;
        
        /** 绘制空心椭圆  **/
        drawBlankOval(g);
        startY = startY + height + tip;
        
        /** 绘制实心椭圆 **/
        drawFillOval(g);
        startY = startY + height + tip;
        
        /** 绘制类似进度条 **/
        drawBlankAndFillOval(g);
        startY = startY + height + tip;
        
        
        /** 绘制圆**/
        drawCycle(g);
    }

    private void clear(Graphics g){
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    
    /**
     * drawRect(int x, int y , int width, int height);
     * @param g
     */
    private void drawBlankRect(Graphics g){
        
        g.setColor(255,0,0);    
        g.drawRect(10, startY, 100, height);
        
    }
    
    
    private void drawFillRect(Graphics g){
        
        g.setColor(0,255,0);
        g.fillRect(10, startY, 100, height);
    }
    
    
    /**
     * drawRoundRect(int x , int y , int width, int height, int arcWidth , int arcHeight);
     *         arcWidth 是左右弧衔接起来凑成的弧的宽度
     *         arcHeight 则是上下弧衔接起来凑成的弧的高度
     * @param g
     */
    private void drawBlankOval(Graphics g){
        g.setColor(0,0,255);    
        g.drawRoundRect(10, startY, 100, height, 20, 20);
    }
    
    private void drawFillOval(Graphics g){
        g.setColor(0,0,255);    
        g.fillRoundRect(10, startY, 100, height, 20, 20);
    }
    
    /**
     * 通过叠加的方式
     * @param g
     */
    private void drawBlankAndFillOval(Graphics g){
        g.setColor(0,0,255);    
        g.drawRoundRect(10, startY, 100, height, 20, 20);
        g.fillRoundRect(10, startY, 20, 20, 20, 20);
        g.fillRect(20, startY, 60, 20);
    }
    
    /**
     *   
     */
    private void drawCycle(Graphics g){
        g.setColor(150,150,150);
        g.drawRoundRect(10, startY, 60, 60, 60, 60);    
    }
}