import java.applet.*; import java.awt.*; import java.io.*; import java.util.*; import java.net.URL; import java.awt.image.PixelGrabber; //// Applet class //// public class kisekae extends Applet implements Runnable{ boolean isStandalone = false; Thread t; int rtnMode; Graphics offscreen; // non-move sprite & fixed screen Graphics fixscreen; // fixed screen Image offimage,fiximage; public kisekaeCell kCells[]; public int TotalCells = 0; MediaTracker Mt; int paintMode; int dragMode = 0; int width,height; int KisekaeWidth,KisekaeHeight; int mouseX,mouseY,oldX,oldY; Color backColor; String statusString; int statusX,statusY; String ImageList[]; int REFRESH_RATE = 100; String versionString = "NOBORU's KISS Applet (Beta7)"; int NUM_CELLS = 256; int nowLoadImage = 0; // get parameters // public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // on create Applet // public kisekae() { } // on initiarize applet // public void init() { String s; width = 640; height = 480; this.resize(width,height); backColor = new Color(0); backColor = getParamColor("backc","white"); ImageList = new String[NUM_CELLS]; TotalCells = 0; for ( int i = 0 ; i < 256 ; i++ ) { s = getParameter("image"+Integer.toString(i)); if ( s != null ){ ImageList[TotalCells] = new String(s); TotalCells = TotalCells+1; } } paintMode = 0; statusString=""; statusX=0; statusY=0; rtnMode = 0; nowLoadImage = 0; REFRESH_RATE = 100; MakeOffScreen(); Mt = new MediaTracker(this); kCells = new kisekaeCell[NUM_CELLS]; if ( CellInit(TotalCells,ImageList) == false ) { setStatus("data error",100,100); rtnMode = -1; repaint(); } } // get applet information // public String getAppletInfo() { return "Applet Information"; } // get parameter information // public String[][] getParameterInfo() { return null; } // on applet stop // public void stop() { if (t != null) { t.stop(); t = null; } } // override update // public void update(Graphics g){ paint(g); } // on applet start // public void start() { if (t == null) { t = new Thread(this); t.start(); } } // thread routine // public void run() { while (true) { try { Thread.sleep (REFRESH_RATE); } catch (Exception exc) { }; mainControl(); } } // control routine // public void mainControl() { switch (rtnMode){ case 0: // image load start // kCells[nowLoadImage].image=getImage(kCells[nowLoadImage].url); showStatus("Load Image "+kCells[nowLoadImage].imageName); Mt.addImage(kCells[nowLoadImage].image,100); rtnMode=1; repaint(); break; case 1: // image load progless // if (Mt.checkID(100,true)){ kCells[nowLoadImage].setImageWH(); nowLoadImage = nowLoadImage + 1; if (nowLoadImage == TotalCells) { nowLoadImage = 0; rtnMode=2; paintMode = 1; REFRESH_RATE = 100; showStatus(" "); } else { rtnMode = 0; } repaint(); } break; } } // Make OffScreen buffer // public void MakeOffScreen(){ setBackground(Color.white); offimage = createImage(width,height); offscreen = offimage.getGraphics(); fiximage = createImage(width,height); fixscreen = fiximage.getGraphics(); } // draw Status // public void setStatus(String s,int x,int y){ statusString=s; statusX=x; statusY=y; repaint(); } // draw screen // public void paint(Graphics g){ switch ( paintMode ){ case 0: // message & Sprite display // fixscreen.setColor(backColor); fixscreen.fillRect(0,0,width,height); for (int i=TotalCells-1 ; i>=0; i--) { if ((kCells[i] != null) & (kCells[i].image != null)) { fixscreen.drawImage(kCells[i].getImage(),kCells[i].posX,kCells[i].posY,this); } } offscreen.drawImage(fiximage,0,0,this); g.drawImage(offimage,0,0,this); g.setColor(getForeground()); Font courierFont=new Font("Daialog",Font.BOLD,18); g.setFont(courierFont); g.drawString(versionString,10,80); g.drawString("Now Loading...please Wait",10,100); courierFont=new Font("Daialog",Font.PLAIN,14); g.setFont(courierFont); g.drawString(statusString,statusX,statusY); break; case 1: // base offscreen make // fixscreen.setColor(backColor); fixscreen.fillRect(0,0,width,height); for (int i=TotalCells-1 ; i>=0; i--) { if (kCells[i] != null) { if (kCells[i].Fixed == true){ fixscreen.drawImage(kCells[i].getImage(),kCells[i].posX,kCells[i].posY,this); } } } offscreen.drawImage(fiximage,0,0,this); g.drawImage(offimage,0,0,this); break; case 2: // base offscreen & move cell display // offscreen.drawImage(fiximage,0,0,this); for (int i=TotalCells-1 ; i>=0; i--) { if (kCells[i] != null) { if (kCells[i].Fixed == false){ offscreen.drawImage(kCells[i].getImage(),kCells[i].posX,kCells[i].posY,this); } } } g.drawImage(offimage,0,0,this); break; case 3: // remake base offscreen & move cell display // fixscreen.setColor(backColor); fixscreen.fillRect(0,0,width,height); for (int i=TotalCells-1 ; i>=0; i--) { if (kCells[i] != null) { if (kCells[i].Fixed == true){ fixscreen.drawImage(kCells[i].getImage(),kCells[i].posX,kCells[i].posY,this); } } } offscreen.drawImage(fiximage,0,0,this); for (int i=TotalCells-1 ; i>=0; i--) { if (kCells[i] != null) { if (kCells[i].Fixed == false){ offscreen.drawImage(kCells[i].getImage(),kCells[i].posX,kCells[i].posY,this); } } } g.drawImage(offimage,0,0,this); paintMode=2; break; } } //// mouse event handler //// // mouse down // public boolean mouseDown(Event e,int x,int y) { dragMode=1; mouseX = x; mouseY = y; checkDrag(); return true; } // mouse up // public boolean mouseUp(Event e,int x,int y) { dragMode=2; mouseX = x; mouseY = y; checkDrag(); return true; } // mouse drag // public boolean mouseDrag(Event e,int x,int y) { dragMode=3; mouseX = x; mouseY = y; checkDrag(); return true; } // mouse move // public boolean mouseMove(Event e,int x,int y) { return true; } // mouse enter // public boolean mouseEnter(Event e,int x,int y) { return true; } // mouse exit // public boolean mouseExit(Event e,int x,int y) { return true; } // check mouse status // public void checkDrag() { if ( rtnMode == 2 ) { switch (dragMode) { case 1: // mouse down // int x = mouseX; int y = mouseY; int moveID=0; boolean isFound = false; for ( int i=0 ; i=0; i--) { if (kCells[i] != null) { kCells[i].Fixed=true; kCells[i].setDraggable(false); } } showStatus(" "); paintMode=1; dragMode = 0; repaint(); break; case 3: // mouse drag // x = mouseX; y = mouseY; for ( int i=TotalCells-1 ; i>=0; i--) { if (kCells[i] != null) { if (kCells[i].isDraggable()){ kCells[i].translate(x-oldX,y-oldY); } } } oldX=x; oldY=y; repaint(); break; } } } // Cell Initiarize // public boolean CellInit(int n,String[] StrList) { URL myURL; int size; String nowLine; int mk,fx,nowX,nowY; String cellName; String dmyStr1,dmyStr2,dmyStr3; int nowSet = 0; for ( int i = 0 ; i < n ; i++ ) { nowLine = StrList[i]; StringTokenizer param1 = new StringTokenizer(nowLine,","); // Cell name // dmyStr1=param1.nextToken(); dmyStr1=RemoveSpace(dmyStr1); cellName=dmyStr1+".gif"; // ID number // dmyStr1=param1.nextToken(); dmyStr1=RemoveSpace(dmyStr1); mk = Integer.valueOf(dmyStr1).intValue(); // fix factor // dmyStr1=param1.nextToken(); dmyStr1=RemoveSpace(dmyStr1); fx = Integer.valueOf(dmyStr1).intValue(); // position // dmyStr1=param1.nextToken(); dmyStr1=RemoveSpace(dmyStr1); nowX = Integer.valueOf(dmyStr1).intValue(); dmyStr1=param1.nextToken(); dmyStr1=RemoveSpace(dmyStr1); nowY = Integer.valueOf(dmyStr1).intValue(); // image read // try { myURL=new URL(getDocumentBase(),cellName); } catch ( Exception e ) { setStatus(cellName+" read error",100,140); System.out.println(cellName+" read error"); return false; } // System.out.println(myURL); kCells[i]= new kisekaeCell(myURL,this); kCells[i].ID=mk; kCells[i].MoveFactor=fx; kCells[i].imageName=cellName; kCells[i].posX = nowX; kCells[i].posY = nowY; } statusString="initiarize end"; repaint(); return true; } // remove Space from String // public String RemoveSpace( String s ) { while (s.indexOf(' ') !=-1) { s=s.substring(0,s.indexOf(' ')) +s.substring(s.indexOf(' ')+1); } return s; } ////// parameter analyze ////// protected int parseNumber(String s, int defaultvalue) { int i = defaultvalue; if (s != null){ int radix,sl = s.length(); String news; if (sl>2 && s.substring(0,2).compareTo("0x") == 0){ radix = 16; news = s.substring(2,sl); } else if(sl>1 && s.substring(0,1).compareTo("#")==0){ radix = 16; news = s.substring(1,sl); } else { radix = 10; news = s; } try { i = Integer.parseInt(news,radix); } catch (Exception e) { i = defaultvalue; } } return i; } protected double parseNumber(String s, double defaultvalue){ double d = defaultvalue; if (s != null){ try { d = Double.valueOf(s).doubleValue(); } catch(Exception e){ d = defaultvalue; } } return d; } protected Color parseColor(String s){ if (s == null) return null; int sl = s.length(); int radix,i; if (sl>2 && s.substring(0,2).compareTo("0x")==0){ radix = 16; s = s.substring(2,sl); } else if (sl>1 && s.substring(0,1).compareTo("#")==0){ radix = 16; s = s.substring(1,sl); } else { radix = 10; for (int j=0; j x) && (posY + imageH > y)); } // move // public void translate(int x,int y) { posX += x; posY += y; } // check trans. color // public boolean isTransColor(int x,int y){ boolean b=((getPixel(x,y)&0xff000000)==0); return b; } // Make Pixel Buffer // public boolean MakePixelBuffer() { pb = new int[imageW*imageH]; pixelGrabber = new PixelGrabber(image,0,0,imageW,imageH,pb,0,imageW); try { pixelGrabber.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return false; } while ((pixelGrabber.status() & 128) != 0) {} return true; } // get Pixel data // public int getPixel(int i, int j) { if (inside(i,j)==false){ return 0;} if ((pixelGrabber.status() & 128) == 0) { return pb[(i-posX)+imageW*(j-posY)]; } System.err.println("image fetch aborted or errored"); return 0; } }