package com.book.photo;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import de.enough.polish.ui.Canvas;
public class PhotoScreen extends Canvas implements Runnable{
private Image photo = null;
private Image photoTemp= null ;
private Image bg = null;
public PhotoScreen(){
this.setFullScreenMode(true);
try {
photo = Image.createImage("/photo.png");
bg = Image.createImage("/bg.png");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
}
private int[] resizePhoto(Image src,int w,int h){
int dest[] = new int[w*h];
int srcW = src.getWidth();
int srcH = src.getHeight();
int srcData[] = new int[srcW*srcH];
src.getRGB(srcData, 0, srcW, 0, 0, srcW, srcH);
for(int i = 0;i<h;++i){
for(int j = 0; j<w ; ++j){
int srcX = j * srcW / w;
int srcY = i * srcH / h;
dest[j+i*w] = srcData[srcX + srcY * srcW];
}
}
return dest;
}
public int[] diPhoto(int[] src,int width,int height ,int angle){
int tempSrc[][] = new int [height][width];
for(int i=0; i<height ; ++i)
System.arraycopy(src, i*width, tempSrc[i], 0, width);
float increx = 1f;
float increy = (float)angle/width;
float x=0f,y=(float)height;
for(int index=0; index<width ;++index){
if( height-(int)y -1 > 0 ){
int steps = height-(int)y -1;
for(int newHeight = 0 ; newHeight < height - steps; ++newHeight){
tempSrc[newHeight][(int)x]= tempSrc[newHeight + steps][(int)x] ;
}
for(int len = steps ; len > 0 ; --len){
tempSrc[height - len][(int)x] = 0x00ffffff;
}
}
x += increx;
y -= increy;
}
for(int i=0; i<height ; ++i)
System.arraycopy(tempSrc[i], 0, src, i*width, width);
return src;
}
protected void paint(Graphics g) {
if(bg != null)
g.drawImage(bg, 0, 0, Graphics.TOP | Graphics.LEFT);
if(photo != null)
g.drawImage(photo, 0, 0, Graphics.TOP | Graphics.LEFT);
if(photoTemp != null)
g.drawImage(photoTemp, 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void run() {
int i = 5 ;
int j = 1;
while(i > 0){
int destW = (i)* photo.getWidth()/6 ;
photoTemp = Image.createRGBImage(diPhoto(resizePhoto(photo,destW,photo.getHeight()),destW,photo.getHeight(),j),
destW,photo.getHeight(),true);
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
--i;
j += 10;
}
photoTemp = null;
repaint();
}
protected void keyPressed(int keycode){
int key = getGameAction(keycode);
if (key == FIRE){
System.out.println("Press Fire");
new Thread(this).start();
}
}
}