?? mycanvas.java
字號:
//waterCanvas
import javax.microedition.lcdui.*;
import java.util.Random;
public class MyCanvas extends Canvas{
totathread mythread;
private ml wps[]; //悈柺傪嶌傞揰偺儕僗僩
private drip dps[];// 棊偪偰偔傞塉偺obj
String input;
int dropcount =-1;
int gwaterlev;
int ydestb,mukiy,mukiyb,ydest;
int chikaray,chikarayb;
int chikara,realchikara,chikarab,realchikarab;
int mydistance,mydistanceb;
int tpos;
//int gx,gy;
private final int scWidth = getWidth();
private final int scHeight= getHeight();
boolean initflag =true;
boolean onoff = true; // on = update
//constructor
MyCanvas(String msg) {
input = msg;
gwaterlev = getHeight()/10 *600;
// making thread obj
mythread = new totathread(this);
makeObjects();
reflectInput(input);
mythread.start();
}
public void reflectInput(String str){
dropcount = -1;
String ps = str;
/* Start Modified 17-03-2003 4 -> ps.length() */
for(int n = 0; n <ps.length(); n++){
/* End Modified 17-03-2003 */
char pc= ps.charAt(n);
dps[n].setmyletter(pc);
}
}
public void showNotify(){
onoff = true;
}
public void hideNotify(){
onoff = false;
}
public void makeObjects() {
dps = new drip[4];
for(int i = 0; i < dps.length; i++) {
dps[i] = new drip(scWidth,scHeight);
dps[i].init(i);
}
wps = new ml[5];
for(int i = 0; i< wps.length;i++){
wps[i] = new ml();
int tempgap = (scWidth*100)/6;
int tempx = tempgap + tempgap*i;
int tempy = gwaterlev;
wps[i].init(tempx,tempy);
}
}
public void updatestage(){
if (onoff){
move();
repaint();
}
}
///birth
public void birth(){
if (dropcount >=3){
dropcount = 0;
}else{
dropcount = dropcount +1;
}
dps[dropcount].fire();
}
////move
public void move(){
/*--------------------- driping update-------------------------------*/
for(int i = 0; i < dps.length; i++) {
dps[i].move(gwaterlev);
if (dps[i].hitflag == true){
if (dps[i].y >= gwaterlev){
if(dps[i].x/100 < scWidth/6 ){
tpos = 0;}
else if (dps[i].x/100 >= scWidth/6 && dps[i].x/100 < (scWidth/6)*2){
tpos = 1;}
else if (dps[i].x/100 >= (scWidth/6)*2 && dps[i].x/100 < (scWidth/6)*3){
tpos = 2;}
else if (dps[i].x/100 >= (scWidth/6)*3 && dps[i].x/100 < (scWidth/6)*4){
tpos = 3;}
else if (dps[i].x/100 >= (scWidth/6)*4){
tpos = 4;
}
//摉偨傝敾掕屻悈傪墴偡//
wps[tpos].y =wps[tpos].y+1500;
dps[i].hitflag = false;
}
}
}
/*------------------------- water update --------------------------*/
for( int n = 0; n < wps.length; n++){
if (n== 0){
ydest = gwaterlev;
}else{
ydest = wps[n-1].y;
}
if (n== 4){
ydestb = gwaterlev;
}else{
ydestb = wps[n+1].y;
}
/*-------------right side calc-------------------*/
mydistance = (Math.abs(wps[n].y- ydest));
if (mydistance > wps[n].baneL){
chikara = (mydistance - wps[n].baneL)/wps[n].baneK;
realchikara = chikara;
if (wps[n].y - ydest > 0){
mukiy = -1;
}else{
mukiy = 1;
}
chikaray = (realchikara)*mukiy;
}else{
chikaray = 0;
}
/*-------------- leftside calc ------------------*/
mydistanceb = Math.abs(wps[n].y- ydestb);
if (mydistanceb > wps[n].baneL){
chikarab = (mydistanceb - wps[n].baneL)/wps[n].baneK;
realchikarab = (chikarab);
if (wps[n].y - ydestb > 0){
mukiyb = -1;
}else{
mukiyb = 1;
}
chikarayb = (realchikarab )*mukiyb;
}else{
chikarayb = 0;
}
wps[n].dy = wps[n].dy +chikaray + chikarayb;
wps[n].dy = wps[n].dy-(wps[n].dy /wps[n].dragC);
wps[n].y = wps[n].y + wps[n].dy;
}
}
// draw
public void paint(Graphics g) {
g.setColor(255,255,255); // back color
g.fillRect(0, 0, scWidth, scHeight);
for(int i = 0; i < dps.length; i++) {
if (dps[i].mflag == true){
if(dps[i].hitflag == false){
g.setColor(0,20,90); // under water
}else{
g.setColor(0,150,255); // above water
}
}
//g.fillRect(dps[i].x/100,dps[i].y/100,3,3);
String tempstr = String.valueOf(dps[i].myLetter);
g.drawString(tempstr,dps[i].x/100,dps[i].y/100,0);
}
g.setColor(0,50,200); // front color
g.drawLine(0,gwaterlev/100,wps[0].x/100,wps[0].y/100);
g.drawLine(wps[0].x/100,wps[0].y/100,wps[1].x/100,wps[1].y/100);
g.drawLine(wps[1].x/100 ,wps[1].y/100 ,wps[2].x/100,wps[2].y/100);
g.drawLine(wps[2].x/100,wps[2].y/100 ,wps[3].x/100,wps[3].y/100);
g.drawLine(wps[3].x/100 ,wps[3].y/100 ,wps[4].x/100,wps[4].y/100);
g.drawLine(wps[4].x/100 ,wps[4].y/100 ,scWidth,gwaterlev/100);
}
public void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case LEFT:break;
case RIGHT:break;
case UP:
break;
case FIRE: birth();
}
repaint();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -