📄 changethecolorcanvas.java
字号:
package barontools.changethecolor;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ChangeTheColorCanvas extends Canvas {
ChangeTheColorMIDlet mainMIDlet;
Image buffer=null;
Graphics bg=null;
public static int SK_CONFIRM=-6;
public static int SK_CANCEL=-7;
public static int SCREEN_WIDTH=176;
public static int SCREEN_HEIGHT=208;
public static int PM_BUFFERED=0;
public static int PM_PAINT_GRAPHICS=1;
public static int PAINT_MODE=PM_BUFFERED;
private Vector componentList=new Vector();
private int PLTEPos=0;
private int PLTELength=0;
private int tRNSIndex=0;
public byte[] imgData=null;
PlatePanel platePanel;
ImageBoxPanel ibPanel;
ChooseFileDialog cfDialog;
//int []colorDisp=new int[6];
public ChangeTheColorCanvas(ChangeTheColorMIDlet ctcm) {
mainMIDlet=ctcm;
init();
updateCanvas();
}
public int getWidth(){
return SCREEN_WIDTH;
}
public int getHeight(){
return SCREEN_HEIGHT;
}
private void init(){
setFullScreenMode(true);
if (PAINT_MODE == PM_BUFFERED) {
buffer = Image.createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
bg = buffer.getGraphics();
bg.setColor(PlatePanel.COLOR_WHITE);
}
platePanel = new PlatePanel(this);
platePanel.setTitle("调色板");
platePanel.setSize(getWidth(), 56);
platePanel.yPosition=getHeight()-platePanel.getHeight()-16;
platePanel.setShow(true);
ibPanel = new ImageBoxPanel();
ibPanel.setSize(getWidth(), getHeight());
ibPanel.setShow(true);
addComponent(ibPanel);
addComponent(platePanel);
String[] imgs=new String[]{"dgrass_grass.png","earth_water.png","furniture.png"};
try{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
InputStream is=getClass().getResourceAsStream("/res/name.txt");
byte [] mydata;
byte [] buffer=new byte[1024];
int ic;
if(is!=null){
while((ic=is.read(buffer))>0){
dos.write(buffer,0,ic);
}
mydata=baos.toByteArray();
StringBuffer sb=new StringBuffer(new String(mydata));
Vector v=new Vector();
int l=sb.length(),i=0;
while(i<sb.length()){
if(sb.charAt(i)==0xd){
char [] sc=new char[i];
sb.getChars(0,i,sc,0);
String ts=new String(sc);
if(ts.endsWith(".png")){
v.addElement(ts);
}
sb.delete(0,i+2);
i=0;
}
i++;
}
imgs=new String[v.size()];
v.copyInto(imgs);
}
}catch(Exception e){
e.printStackTrace();
}
//CustomItem[] items=new CustomItem[]{new CustomItem("label","text")};//初始化
cfDialog=new ChooseFileDialog("选择图片",imgs,this);
}
private void componentPaint(Graphics g){
g.setColor(PlatePanel.COLOR_WHITE);
g.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
int size=componentList.size();
for(int i=0;i<size;i++){
BaronComponent bc=(BaronComponent)componentList.elementAt(i);
if(bc.isShow()){
bc.paint(g);
}
}
}
public void addComponent(BaronComponent bComponent){
componentList.addElement(bComponent);
}
protected void paint(Graphics g) {
/**@todo Implement this javax.microedition.lcdui.Canvas abstract method*/
if(PAINT_MODE==PM_BUFFERED&&buffer!=null){
g.drawImage(buffer, 0, 0, 20);
}else if(PAINT_MODE==PM_PAINT_GRAPHICS){
componentPaint(g);
}
}
public void keyPressed(int k) {
int myk = 0;
if (k == SK_CONFIRM || k == KEY_NUM0) {
Display.getDisplay(mainMIDlet).setCurrent(cfDialog.fileList);
}
else if (k == KEY_NUM9) {
mainMIDlet.quitApp();
}else {
try {
myk = getGameAction(k);
}
catch (Exception e) {
e.printStackTrace();
}
}
if (myk == UP || myk == DOWN || myk == LEFT || myk == RIGHT || myk == FIRE) {
compomentKeyAction(myk);
}
// switch (myk) {
// case UP:
// break;
// case DOWN:
// break;
// case LEFT:
// break;
// case RIGHT:
// break;
// case FIRE:
// break;
// }
switch (k) {
case KEY_POUND:
break;
case KEY_STAR:
platePanel.setShow(!platePanel.isShow());
break;
case KEY_NUM0:
Display.getDisplay(mainMIDlet).setCurrent(cfDialog.fileList);
break;
}
updateCanvas();
}
public void updateCanvas(){
if(PAINT_MODE==PM_BUFFERED&&bg!=null){
componentPaint(bg);
repaint();
}else if(PAINT_MODE==PM_PAINT_GRAPHICS){
repaint();
}
}
private void compomentKeyAction(int key){
int size=componentList.size();
for(int i=size-1;i>=0;i--){
BaronComponent bc=(BaronComponent)componentList.elementAt(i);
if(bc.isShow()){
bc.keyAction(key);
break;
}
}
}
byte[] pngFlag = new byte[]{(byte)0x89,(byte)0x50,(byte)0x4e,(byte)0x47,
(byte)0x0d,(byte)0x0a,(byte)0x1a,(byte)0x0a};
byte[] IHDR = null;
byte[] PLTE = null;
byte[] tRNS = null;
byte[] IDAT = null;
byte[] IEND = null;
public void flashImageData(String imgName){
DataInputStream din;
try{
din=new DataInputStream(getClass().getResourceAsStream(imgName));
imgData = new byte[din.available()];
din.readFully(imgData);
din=new DataInputStream(new ByteArrayInputStream(imgData));
din.skip(pngFlag.length);
int index = 8;
tRNSIndex=-1;
while(index<imgData.length){
int byteLen = din.readInt();//数据块数据的长度
index+=4;
byte[] dataName = new byte[4];
din.readFully(dataName);//数据块名
index+=4;
if (dataName[0] == 0x49 && dataName[1] == 0x48 &&
dataName[2] == 0x44 && dataName[3] == 0x52) { //IHDR块
IHDR = new byte[byteLen + 12];
System.arraycopy(imgData,index-8,IHDR,0,IHDR.length);
}
else if (dataName[0] == 0x50 && dataName[1] == 0x4C &&
dataName[2] == 0x54 && dataName[3] == 0x45) { //PLTE块
PLTE = new byte[byteLen + 12];
System.arraycopy(imgData,index-8,PLTE,0,PLTE.length);
PLTEPos=index-8;
PLTELength=byteLen+12;
ColorBox[] cb=new ColorBox[byteLen/3];
for(int i=0;i<byteLen/3;i++){
int tcolor=((PLTE[8+i*3]&0xff)<<16)|((PLTE[8+i*3+1]&0xff)<<8)|((PLTE[8+i*3+2]&0xff));
cb[i]=new ColorBox(tcolor);
}
platePanel.setColors(cb);
}
else if (dataName[0] == 0x74 && dataName[1] == 0x52 &&
dataName[2] == 0x4E && dataName[3] == 0x53) { //tRNS块
tRNS = new byte[byteLen + 12];
System.arraycopy(imgData,index-8,tRNS,0,tRNS.length);
for(int i=8;i<byteLen+8;i++){
if(tRNS[i]==0){
tRNSIndex=i-8;
break;
}
}
}
else if (dataName[0] == 0x49 && dataName[1] == 0x44 &&
dataName[2] == 0x41 && dataName[3] == 0x54) { //IDAT块
IDAT = new byte[byteLen + 12];
System.arraycopy(imgData,index-8,IDAT,0,IDAT.length);
}
else if (dataName[0] == 0x49 && dataName[1] == 0x45 &&
dataName[2] == 0x4E && dataName[3] == 0x44) { //IEND块
IEND = new byte[byteLen + 12];
System.arraycopy(imgData,index-8,IEND,0,IEND.length);
}
din.skip(byteLen+4);
index+=(byteLen+4);
}
din.close();
if(tRNSIndex>=0){
platePanel.setColorAt(tRNSIndex, PlatePanel.COLOR_TRANS);
}
ibPanel.setImage(Image.createImage(imgData,0,imgData.length));
}catch(Exception e){
e.printStackTrace();
}
}
public static byte[] intToBytes(int intValue)
{
long i = (intValue & 0xFFFFFFFFL);
byte[] b = new byte[4];
b[0] = (byte)((i & 0xFF000000L) >> 24);
b[1] = (byte)((i & 0x00FF0000L) >> 16);
b[2] = (byte)((i & 0x0000FF00L) >> 8);
b[3] = (byte)(i & 0x000000FFL);
return b;
}
public static int bytesToInt(byte [] d){
return ((d[0] & 0xFF) << 24) | ((d[1] & 0xFF) << 16 ) |
((d[2] & 0xFF) << 8) | (d[3] & 0xFF);
}
public static int bytesToInt(byte [] d,int index){
return ((d[index] & 0xFF) << 24) | ((d[index+1] & 0xFF) << 16 ) |
((d[index+2] & 0xFF) << 8) | (d[index+3] & 0xFF);
}
/**
* ImageChangedAction
*
* @param ibp ImageBoxPanel
*/
public void ImageChangedAction(ImageBoxPanel ibp) {
//byte[] plte=flashImageData()
}
public void changeTheColor(int colorNeedChangePos,int colorValue){
int colorNum=(PLTELength-12)/3;
if(colorNum<colorNeedChangePos){
System.out.println("you got a fucking damned color position("+colorNeedChangePos+") to change ,but we just have "+colorNum+" colors.Use your brain not ass to think.");
return ;
}
byte [] rgbColor=intToBytes(colorValue);
System.arraycopy(rgbColor,1,imgData,PLTEPos+8+colorNeedChangePos*3,3);
int crcValue = CRCUtil.checksum(imgData,PLTEPos+4,colorNum*3+4);
imgData[PLTEPos+8 + colorNum * 3 + 0] = (byte) ( (crcValue >> 24) & 0xff);
imgData[PLTEPos+8 + colorNum * 3 + 1] = (byte) ( (crcValue >> 16) & 0xff);
imgData[PLTEPos+8 + colorNum * 3 + 2] = (byte) ( (crcValue >> 8) & 0xff);
imgData[PLTEPos+8 + colorNum * 3 + 3] = (byte) ( (crcValue) & 0xff);
try{
Image img=Image.createImage(imgData,0,imgData.length);
ibPanel.setImage(img);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 改变调色板
* @param PLTE byte[] 基础调色板的数据
* @param COLOR byte[][][] 色表
* @param cIndex int 换成第集中颜色
* @return byte[]
*/
private static byte[] changeRGB(byte[] PLTE,byte[][][] COLOR,int cIndex){
byte[] newPLTE = new byte[PLTE.length];
System.arraycopy(PLTE,0,newPLTE,0,newPLTE.length);
int colorNum = (newPLTE.length-12)/3;//当前调色板的颜色
for(int i=0;i<colorNum;i++){
for(int j=0;j<COLOR[0].length;j++){
if(newPLTE[8+i*3+0] == COLOR[0][j][0] &&
newPLTE[8+i*3+1] == COLOR[0][j][1] &&
newPLTE[8+i*3+2] == COLOR[0][j][2]){
newPLTE[8+i*3+0] = COLOR[cIndex][j][0];
newPLTE[8+i*3+1] = COLOR[cIndex][j][1];
newPLTE[8+i*3+2] = COLOR[cIndex][j][2];
}
}
}
//CRC校验一下
int crcValue = CRCUtil.checksum(newPLTE,4,colorNum*3+4);
newPLTE[8+colorNum*3+0] = (byte) ((crcValue >> 24) & 0xff);
newPLTE[8+colorNum*3+1] = (byte) ((crcValue >> 16) & 0xff);
newPLTE[8+colorNum*3+2] = (byte) ((crcValue >> 8) & 0xff);
newPLTE[8+colorNum*3+3] = (byte) ((crcValue) & 0xff);
return newPLTE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -