📄 mycanvas.java
字号:
//star main canvas
import javax.microedition.lcdui.*;
import java.util.Random;
import java.io.IOException;
public class MyCanvas extends Canvas{
totathread mythread;
private ml wps[]; //stars
private final int heartpos[][]={
{-20, -2},{-21, -7},{-13, -21}, {-19, -12}, {12, -21}, {-4, -15}, {-1, -11}, {-17, -17}, {-8, -18}, {2, -15}, {7, -18}, {15, -17}, {18, -12}, {20, -7}, {-16, 2}, {-13, 7}, {-9, 11}, {-5, 15},{-1, 19},{3, 15},{7, 11}, {11, 7},{15, 2},{19, -2}
};
private final int umbrella[][]={
{-8,2},{-15,2},{-10,-13},{-23,2},{12,-8},{3,-18},{-1,-23},{-18,-3},{-5,-18},{8,-13},{-14,-8},{17,-3},{21,2},{8,2},{0,2},{-1,-18},{-8,26},{-4,29},{0,26},{0,21},{0,16},{0,11},{0,7},{15,2}
};
private final int scWidth = getWidth();
private final int scHeight= getHeight();
Random rnd =new Random();
private Image bkg;
public int bkgflag;
boolean onoff= true;
//constructor
MyCanvas() {
try{
bkg = Image.createImage("/night01.png");
} catch(IOException e){}
// making thread obj
bkgflag =0;
mythread = new totathread(this);
makeObjects();
mythread.start();
}
protected void showNotify(){
onoff = true;
/* Start Addition 17-03-2003 for external events */
bkgflag = 0;
/* End Addition 17-03-2003 */
}
protected void hideNotify(){
onoff = false;
}
public void makeObjects() {
wps = new ml[24];
for(int i = 0; i< wps.length;i++){
wps[i] = new ml();
int tempx = Math.abs(rnd.nextInt())%(scWidth*100);
int tempy = 300 + Math.abs(rnd.nextInt())%((scHeight-45)*100);
int tempNum = Math.abs(rnd.nextInt())%2;
wps[i].init(tempx,tempy,tempNum);
}
}
public void updatestage(){
if (onoff){
repaint();
}
}
//// stars to move
public void move(){
int tempNum = Math.abs(rnd.nextInt())%12;
for(int i = 0; i< wps.length;i++){
int tempx = 0;
int tempy = 0;
//wps[i].init(tempx,tempy,tempNum);
if (tempNum == 5){
tempx = heartpos[i][0]* 100 + (scWidth/2)*100;
tempy = heartpos[i][1]*100 + (scHeight/3)*100;
}else if(tempNum==10){
tempx = umbrella[i][0]* 100 + (scWidth/2)*100;
tempy = umbrella[i][1]*100 + (scHeight/3)*100;
}else{
tempx = Math.abs(rnd.nextInt())%(scWidth*100);
tempy = 300 + Math.abs(rnd.nextInt())%((scHeight-45)*100);
}
wps[i].setmorph(tempx,tempy);
}
}
// draw
public void paint(Graphics g) {
g.setColor(0,0,0); // back color
g.fillRect(0, 0, scWidth, scHeight-35);
if (bkgflag == 0){
int offx = (scWidth - 128)/2;
g.setColor(0,0,0);
g.fillRect(0, 0, scWidth, scHeight);
g.drawImage(bkg,0+offx,scHeight -35,Graphics.LEFT | Graphics.TOP);
bkgflag =1;
}
//---drawing star here-----//
for(int i = 0; i< wps.length;i++){
wps[i].update();
if (wps[i].imgNum == 0){
g.setColor(255,255,255);
g.drawLine(wps[i].x/100 ,wps[i].y/100,wps[i].x/100,wps[i].y/100);
wps[i].imgNum = Math.abs(rnd.nextInt())%2;
}else if(wps[i].imgNum ==1){
g.setColor(145,185,200);
g.drawLine(wps[i].x/100 -1,wps[i].y/100,wps[i].x/100 +1,wps[i].y/100);
g.drawLine(wps[i].x/100 ,wps[i].y/100 -1,wps[i].x/100 ,wps[i].y/100+1);
wps[i].imgNum = Math.abs(rnd.nextInt())%2;
}else if(wps[i].imgNum ==2){
g.setColor(145,185,200);
g.drawLine(wps[i].x/100 -2,wps[i].y/100,wps[i].x/100 +2,wps[i].y/100);
g.drawLine(wps[i].x/100 ,wps[i].y/100 -2,wps[i].x/100 ,wps[i].y/100+2);
wps[i].imgNum = Math.abs(rnd.nextInt())%3;
}
}
}
public void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case LEFT:break;
case RIGHT:break;
case UP:break;
case FIRE:
move();
break;
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -