Sie sind auf Seite 1von 7

Import java.util.Vector; Import javax.microedition.lcdui.

*; Class PhotoFrame extends Canvas implements Runnable( Private int style; Private int speed; Private Vector images; Private imt index; Private int imageX; Private int imageY; Private int imagewidth,imageHeight; Private Thread thread; Private Image image; Private Image bimage; Private long paintTime; Private long statsTime; Int frameCount; Int frameRate; PhotoFrame() { Image=Image.createImage(getwidth(),getHeight()); setStyle(0); setSpeed(0); } Void setImages(Vector images) { This.images=images; If(images.size()>0) { Image image=(Image)images.elementAt(0); imageWidth=image.getWidth(); imageHeight=image.getheight(); } Else { imageWidth=0; inageHeight=0; } Index=0; imageX=(getWidth()-imageWidth)/2; imageY=(getHeight()-imageHeight)/2; genFrame(style,imageX,imageY,imageWidth,imageHeight); }

Void next() { If(images==null||++index>=images.size()) { Index=0; } } Void previous() {if(images!=null&&--index<0) { Index=images.size()-1; } Else { index=0; } } Void reset() { Images=null; Thread=null; } Protected void keyPressed(int keyCode) {int action=getGameAction(keyCode); Switch(action) { Case RIGHT: If(thread==nu;;) { Next(); Repaint(); } Break; Case LEFT: If (thread==null) { Previous(); Repaint(); } Break; Case FIRE:

If(thread==null) {thread=new Thread(this); Thread.start(); } Else { Synchronized(this) {this.notify(); } Thread=null; } Break; } } Protected void keyRepeated(int keyCode) { keyPressed(keyCode); } Void setSpeed(int speed) { keyPressed(keyCode); } voidsetSpeed(int speed) { this.speed=speed; statsTime=0; } Int getSpeed() { return speed; } Void setStyle(int style) { This.style=style; genFrame(style,imageX,imageY,imageWidth,IMAGEhEIGHT); } Int getStyle() { Return style; } Protected void showNotify() {if(images!=null&&images.size()>1)

{ Thread=new Thread(this); Thread.start(); } } Protected void hideNotify() { Thread=null; } Boolean intersectClip(Graphics g,int x,int y,int w,int h) { Int cx=g.getClipX(); If(x+w<=cx) Return false; Int cw=g.getClipWidth(); If(x>cx+cw) Return false; Int cy=g.getClipY(); If(y+h<=cy) Return false; Int ch=g.getClipHeight(); If(y>cy+ch) Return false; Return true; } Public void run() { Thread mythread=Thread.currentThread(); Long scheduled=System.currentTimeMillis(); statsTime=scheduled; paintTime=scheduled; frameCount=0; frameRate=0; while(thread==mythread) { Scheduled+=speeds[speed]; Long delta=scheduled-paintTime;

If(delta>0) { synchronised(this) { Try { This.wat(delta); ) Catch(InterruptedException e) { } } } Next(); Repaint(); serviceRepaints(); } } Protected vodi paint(Graphics g) {paintTime=System.currentTimeMIllis(); If(image!=null) { Int cx=0,cy=0.cw=0,ch=0; If((cx=g.getClipX())<imageX|| (cx+(cw=g.getClipY())<imageY||(cx+(cw=g.getClipWidth()))>(imageX+imageWidth)|| (cy+(ch=g.getClipHeight())>(imageY+imageHeight)) { g.drawImage(image,0,0,Gra[hics.LEFT|Graphics.TOP); if(frameRate>0) { g.fillRect(0,getHeight(),60,20); g.drawString(FPS=+frameRate,0,getHeight(),Graphics.BOTTOM|Graphics.LEFT); } } If(images!=null&&index<images.size()&&intersectsClip(g,imageX,imageY,imageWidth,imageHeight)) { g.drawImage((Image)images.elementAt(index),imageX,imageY,Graphics.LEFT|Gra[hics.TOP); } frameCount++; int delta=(int)(paintTime-statsTime); if(delta>10&&delta<10000)

{ frameRate=((frameCount*1000+500)/delta); frameCount=0; statsTime=paintTime; repaint(); } } } Private void genFrame(int style,int x,int y,int width,int height) { Graphics g=image.getGraphics(); g.setColor(0xffffff); g.fillRect(0,0,image.getWidth()+1,image.getHeight()+1); g.translate(x,y); paintBorder(g,style,width,height); } Private void paintBorder(Graphicsg,int style,int w,int h) { If(style==1) {g.setGrayScale(128); g.drawRect(-1,-1,w+1,h+1); g.drawRect(-2,-2,w+3,h+3); } If(style==2) { If(bimage==null) Bimage=genBorder(); Int bw=bimage.getWidth(); Int bh=bimage.getHeight(); Int I; g.setGrayScale(128); g.drawRect(-1,-1w+1,h+1); g.drawRect(-bw-2,-bh-2,w+bw*2+3,h+bh*2+3); g.drawImage(bimage,-1,-1,Graphics.BOTTOM|Graphics.RIGHT); g.drawImage(biage,-1,h+1,Graphics.TOP|Graphics.RIGHT); g.drawImage(bimage,w+1,-1,Graphics.BOTTOM|Graphics.LEFT); g.drawImage(bimage,w+1,h+1,Graphics.LEFT); for(i=((h%bh)/2);i<h-bh;i+=bh) { g.drawImage(bimage,-1,i,Graphics.RIGHT|Graphics.TOP); g.drawImage(bimage,w+1,i,Graphics.LEFT|Graphics.TOP);

for(i=((w%bw)/2);i<w-bw;i+=bw) {g.drawImage(bimage,i,-1, Graphics.LEFT|Graphics.BOTTOM); {g.drawImage(bimage,i,h+1, Graphics.LEFT|Graphics.TOP); } } } Private Image genBorder() { Image image=Image.createImage(5,5); g.setColor(255,255,255); g.fillRect(0,0,5,5); g.setColor(128,0,255); g.drawLine(2,1,2,3); g.drawLine(2,3,3,2); return image; } Private final static int speeds[]={999999999,500,250,100,0}; }

Das könnte Ihnen auch gefallen