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

📄 colortestcanvas.java

📁 在j2me手机上显示调整图片色板
💻 JAVA
字号:
package barontools.changethecolor;

import javax.microedition.lcdui.*;
import java.util.Vector;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ColorTestCanvas extends Canvas implements CommandListener {
  public static int colorVal=0;
  ChangeTheColorMIDlet ctm;
  Command setColor,updateColor;
  txtInput ti;
  int numBit=6;
  int pos=0;
  int[] colorDisp={0,0,0,0,0,0};//for display color;
  Vector colorList=new Vector();
  public ColorTestCanvas(ChangeTheColorMIDlet _ctm) {
    try {
      ctm=_ctm;
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    ti=new txtInput(this,"设置颜色");
    setColor=new Command("设置颜色",Command.ITEM,1);
    updateColor=new Command("刷新",Command.ITEM,1);
    // Set up this Displayable to listen to command events
    setCommandListener(this);
    // add the Exit command
    addCommand(new Command("Exit", Command.EXIT, 1));

    addCommand(setColor);

  }

  public void commandAction(Command command, Displayable displayable) {
    /** @todo Add command handling code */
    if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
      ChangeTheColorMIDlet.quitApp();
    }else if(command==setColor){
      if(getDispColor()!=0){
        ti.setString(Integer.toHexString(getDispColor()));
      }
      Display.getDisplay(ctm).setCurrent(ti);
    }else if(command==updateColor){
      repaint();
    }
  }

  protected void paint(Graphics g) {
    /** @todo Add paint codes */
    g.setColor(colorVal);
    g.fillRect(0,0,getWidth(),getHeight());

    int x=1,y=0,w=16,h=16;
    y=getHeight()-h;
    int cbw=32,cbh=32;//colorbox

    int cln=colorList.size();
    int column=getWidth()/cbw,row=getHeight()/cbh;
    int tx=0,ty=0;
    for(int i=0;i<cln;i++){
      ColorBox cb=(ColorBox)colorList.elementAt(i);
      tx=i%column*cbw;
      ty=i/column*cbh;
      g.setColor(cb.getColor());
      g.fillRect(tx,ty,cbw,cbh);
      g.setColor(0);
      g.drawRect(tx,ty,cbw-1,cbh-1);
    }

    g.setColor(255,255,255);
    g.fillRect(x-1,y-1,w*numBit+2,h+2);
    for(int i=0;i<numBit;i++){
      if(pos==i){
        g.setColor(255,0,0);
        g.fillRect(x+i*w,y,w,h);
      }
      g.setColor(0);
      g.drawString(Integer.toHexString(colorDisp[i]),x+i*w,y,20);
    }
  }
  public void keyPressed(int k){
    int myk=0;
    try{
      myk=getGameAction(k);
    }catch(Exception e){

    }
    switch (myk) {
      case UP:
        colorDisp[pos]=(colorDisp[pos]+0xf)%0x10;
        break;
      case DOWN:
        colorDisp[pos]=(colorDisp[pos]+0x1)%0x10;
        break;
      case LEFT:
        pos=(pos+numBit-1)%numBit;
        break;
      case RIGHT:
        pos=(pos+1)%numBit;
        break;
      case FIRE:
        colorList.addElement(new ColorBox(getDispColor()));
        break;
    }
    switch(k){
      case KEY_POUND:
        colorList.removeAllElements();
        break;
    }
    colorVal=getDispColor();
    repaint();
  }
  int getDispColor(){
    int t=0;
    for(int i=0;i<numBit;i++){
          t+=colorDisp[i]<<((numBit-1-i)*4);
          //System.out.println("i: "+Integer.toHexString(colorDisp[i]<<((numBit-1-i)*4)));
        }

    return t;
  }
  void setDispColor(int t){
    for(int i=0;i<numBit;i++){
        colorDisp[i]=(t>>((numBit-1-i)*4))%0x10;
        System.out.println("set i: "+Integer.toHexString(colorDisp[i]));
      }
      System.out.println(" set colorvalue: "+Integer.toHexString(colorVal));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -