?? pen.java
字號:
package org.gggeye.easymf.ui;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
/**
* 圖形繪制包裝類
*
* 跟PPC Android Pen 有異曲同工之妙
*
* @author wuhua
* <a href="http://wuhua.3geye.net">我的博客</a>
*
*/
public class Pen {
Graphics graphics;
int top;
int left;
Font bakFont;
int bakStyle;
int bakColor;
int bakClipX = 0;
int bakClipY = 0;
int bakClipWidth = 0;
int bakClipHeight = 0;
public Pen(Graphics graphics) {
this(graphics, 0, 0);
}
public Pen(Graphics graphics, int top, int left) {
this.graphics = graphics;
this.top = top;
this.left = left;
}
public final void save() {
this.bakColor = graphics.getColor();
this.bakStyle = graphics.getStrokeStyle();
this.bakFont = graphics.getFont();
}
public final void reset() {
graphics.setColor(this.bakColor);
graphics.setStrokeStyle(this.bakStyle);
graphics.setFont(this.bakFont);
}
public final void saveClip() {
bakClipX = graphics.getClipX();
bakClipY = graphics.getClipY();
bakClipWidth = graphics.getClipWidth();
bakClipHeight = graphics.getClipHeight();
}
public final void resetClip() {
graphics.setClip(bakClipX, bakClipY,
bakClipWidth, bakClipHeight);
}
/**
* 構造函數
* @param graphics Graphics
*/
public Graphics getGraphics() {
return graphics;
}
/**
* 設置左邊界和頂邊界
* @param x int
* @param y int
*/
public void setOffset(int x, int y) {
left = x;
top = y;
}
public int getColor() {
return graphics.getColor();
}
public void setColor(int i) {
graphics.setColor(i);
}
/**
* 該方法用于設置繪制線弧形,矩形等時指定繪制模式是SLLID或DOTTED.
* @param i int
*/
public void setStrokeStyle(int i) {
graphics.setStrokeStyle(i);
}
public void setFont(Font font) {
graphics.setFont(font);
}
public Font getFont() {
return graphics.getFont();
}
/**
* 獲取剪貼區X軸偏移量
* @return int
*/
public int getClipX() {
return graphics.getClipX() - left;
}
/**
* 獲取剪貼區Y軸偏移量
* @return int
*/
public int getClipY() {
return graphics.getClipY() - top;
}
/**
* 獲取剪貼區寬度
* @return int
*/
public int getClipWidth() {
return graphics.getClipWidth();
}
/**
* 獲取剪貼區高度
* @return int
*/
public int getClipHeight() {
return graphics.getClipHeight();
}
/**
*該方法指定的矩形區域與當前的剪貼區交叉,形成并集的新的剪貼區.
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void clipRect(int x, int y, int width, int height) {
graphics.clipRect(x + left, y + top, width, height);
}
/**
* 設置剪貼區
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void setClip(int x, int y, int width, int height) {
graphics.setClip(x + left, y + top, width, height);
}
/**
* 繪制直線
* @param x int
* @param y int
* @param x2 int
* @param y2 int
*/
public void drawLine(int x, int y, int x2, int y2) {
graphics.drawLine(x + left, y + top, x2 + left, y2 + top);
}
/**
* 填充矩形框
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void fillRect(int x, int y, int width, int height) {
graphics.fillRect(x + left, y + top, width, height);
}
/**
* 繪制矩形,通過多種顏色進行繪制
* @param colors int[]
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void drawRects(int[] colors, int x, int y,
int width, int height) {
int cof = 0;
for (int k = 0; k < colors.length; k++) {
cof++;
this.setColor(colors[k]);
this.drawRect(x - cof,
y - cof,
width + (cof << 1),
height + (cof << 1));
}
}
public void drawRects(int x, int y,
int width, int height) {
// int[] colors = new int[] {0xB0E2FF, 0x4876FF, 0x1E90FF};
int[] colors = new int[] {0xCEF};
drawRects(colors, x, y, width, height);
}
/**
* 填充,指定區域
* @param color int
* @param x int
* @param y int
* @param width int
* @param height int
* @param size int
*/
public void fillRect(int color, int x, int y, int width, int height,
int size) {
// graphics.setColor(color);
// //if(size == 0){
// graphics.fillRect(x + left, y + top, width, height);
// if(size != 0){
// int h = height/size;
// for(int i=1; i<size; i++){
// graphics.fillRect(0, y + h*i, width, h);
// }
// graphics.fillRect(0, y + top, width, h);
// }
}
/**
* 繪制矩形
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void drawRect(int x, int y, int width, int height) {
graphics.drawRect(x + left, y + top, width, height);
}
/**
* 填充弧形.
* @param x int
* @param y int
* @param width int
* @param height int
* @param startAngle int
* @param arcAngle int
*/
public void fillArc(int x, int y, int width, int height, int startAngle,
int arcAngle) {
graphics.fillArc(x + left, y + top, width, height, startAngle, arcAngle);
}
/**
* 繪制弧形
* @param x int
* @param y int
* @param width int
* @param height int
* @param startAngle int
* @param arcAngle int
*/
public void drawArc(int x, int y, int width, int height, int startAngle,
int arcAngle) {
graphics.drawArc(x + left, y + top, width, height, startAngle, arcAngle);
}
/**
* 繪制字符串
* @param s String
* @param x int
* @param y int
* @param anchor int
*/
public void drawChars(char[] c, int os, int len, int x, int y, int anchor) {
graphics.drawChars(c, os, len, x + left, y + top, anchor);
}
public void drawChar(char c, int _x, int _y, int _anchor){
graphics.drawChar(c, _x + left, _y + top, _anchor);
}
public void drawString(String s, int x, int y, int anchor) {
graphics.drawString(s, x + left, y + top, anchor);
}
public void drawSubstring(String s, int x, int y, int k, int i1, int anchor) {
graphics.drawSubstring(s, x, y, k + left, i1 + top, anchor);
}
public void drawImage(Image image, int x, int y, int anchor) {
graphics.drawImage(image, x + left, y + top, anchor);
}
public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
graphics.fillTriangle(x1 + left, y1 + top, x2 + left, y2 + top,
x3 + left,
y3 + top);
}
public final void drawStrings(String[] values, Font font, int left, int top,
int fontColor) {
if(values == null)
return;
this.save();
this.setFont(font);
this.setColor(fontColor);
int x = left;
int y = top;
for (int j = 0; j < values.length; j++) {
drawString(values[j],x, y, 20);
x = 0;
y += font.getHeight();
}
this.reset();
}
/**
* 繪制漸變色選擇條
*
* @param g --
* @param color
* @param x
* @param y
* @param width
* @param height
*/
public final void drawShadeRect(int color, int x , int y, int width, int height){
int[] rgb = getShadeColor(color, width);
int h = y+height;
for(int i =y ; i < h; i+=2){
graphics.drawRGB(rgb, 0, width, x, i, width,
2, true);
}
}
/**
* 獲取顏色漸變RGB數組,
* 為了獲取這個數據,而又跟CLDC1.0兼容,導致項目增加了一個Float類
* 導致程序變大
* @param width
* @return
*/
public final static int[] getShadeColor(int color , int width){
int[] rgb;
int shadeWidth = width;
int nRgbData = shadeWidth * 4;
rgb = new int[nRgbData];
int alpha = -127;
for (int i = 0; i < shadeWidth; i++)
{
alpha = -127 + i;
//主要算法在這里。
int col = color | (128 - alpha << 24);
rgb[i] = col;
rgb[i + shadeWidth ] = col;
rgb[i + shadeWidth * 2] = col;
rgb[i + shadeWidth * 3] = col;
}
return rgb;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -