您的位置:首页 > 其它

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

2011-08-17 19:33 369 查看
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);
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: