📄 mycanvas.java
字号:
//colorbugsCanvas
import javax.microedition.lcdui.*;
import java.util.Random;
public class MyCanvas extends Canvas{
totathread mythread;
private bub balls[]; //bub objlist
private int ballcount = 0;
private int center_x = getWidth() / 2;
private int center_y = getHeight() / 2;
private int scWidth = getWidth();
private int scHeight = getHeight();
private int gx = center_x *100; //target x
private int gy = center_y *100; //target y
private Random rnd =new Random();
private int tempr;
private int tempg;
private int tempb;
boolean onoff = true;
//constructor
MyCanvas() {
// making thread obj
mythread = new totathread(this);
makeObjects();
mythread.start();
}
protected void showNotify(){
onoff = true;
}
protected void hideNotify(){
onoff = false;
}
public void makeObjects() {
balls = new bub[8]; // numbers of bugs
for(int i = 0; i < balls.length; i++) {
switch(i){
case 0: tempr = 255; // yellow
tempg = 255;
tempb = 0;
break;
case 1: tempr = 0; // green
tempg = 255;
tempb = 0;
break;
case 2: tempr = 46; //water blue
tempg = 185;
tempb = 255;
break;
case 3: tempr = 0; // blue
tempg = 0;
tempb = 255;
break;
case 4: tempr = 101; // purple
tempg = 47;
tempb = 135;
break;
case 5: tempr = 255; //pink
tempg = 0;
tempb = 255;
break;
case 6: tempr = 255;
tempg = 0;
tempb = 0;
break;
case 7: tempr = 229;
tempg = 85;
tempb = 30;
break;
}
balls[i] = new bub(center_x,scHeight +10,tempr,tempg,tempb);
}
}
public void updatestage(){
if (onoff){
move();
repaint();
}
}
///birth
public void birth(){
int yseed;
if (ballcount == 0){
for(int i = 0; i < 4;i++){
if (balls[i].moveFlag ==0){
balls[i].moveFlag =1;
balls[i].ox = gx;
balls[i].oy = gy;
balls[i].x = gx;
balls[i].y = gy;
balls[i].size =100;
}
}
ballcount = 1;
}else{
for(int i =4; i < 8;i++){
if (balls[i].moveFlag ==0){
balls[i].moveFlag =1;
balls[i].ox = gx;
balls[i].oy = gy;
balls[i].x = gx ;
balls[i].y = gy ;
balls[i].size =100;
//balls[i].r =Math.abs(rnd.nextInt()%(50));
}
}
ballcount = 0;
}
}
////move
public void move(){
for (int i = 0; i < balls.length;i++) {
if (balls[i].moveFlag == 1){
if (balls[i].size < scWidth*100){
if (i < 4){
balls[i].size = balls[i].size + 50*(1+i);
}else {
balls[i].size = balls[i].size + 50*(i-3);
}
}else{
balls[i].size = 0;
balls[i].x = -400;
balls[i].moveFlag = 0;
}
}
}
}
// draw
public void paint(Graphics g) {
int tempcol;
g.setColor(0,0,0);
g.fillRect(0, 0, scWidth, scHeight);
g.setColor(220,220,220);
g.drawLine(gx/100 -2,gy/100,gx/100 +2,gy/100);
g.drawLine(gx/100 ,gy/100-2,gx/100,gy/100+2);
for(int i = 0; i < balls.length;i++){
//偙偙偱bub 僋儔僗傪draw丅
g.setColor(balls[i].r,balls[i].g,balls[i].b);
int xx = (balls[i].x/100);
int yy = (balls[i].y/100);
g.drawRect(xx- balls[i].size/200 ,yy- balls[i].size/200,balls[i].size/100,balls[i].size/100);
g.drawRect(xx- balls[i].size/200 +1 ,yy- balls[i].size/200 +1 ,balls[i].size/100 -2,balls[i].size/100-2);
// g.drawLine(xx, yy,(xx + balls[i].vx/100), (yy + balls[i].vy/100));
}
}
public void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case LEFT:
if (gx < 0){
gx = scWidth*100;
}else{
gx -= 500;}
break;
case RIGHT:
if (gx > scWidth*100){
gx = 0;
}else{
gx += 500;
}
break;
case UP:
if (gy < 0){
gy = scHeight *100;
}else{
gy -= 500;
}
break;
case DOWN:
if (gy > scHeight*100){
gy = 0;
}else{
gy += 500;
}
break;
case FIRE: birth();
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -