⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pen.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 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 + -