📄 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 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();
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[16]; // numbers of bugs
for(int i = 0; i < balls.length; i++) {
//int tempx =Math.abs(rnd.nextInt()%(scWidth-20));
//int tempy =Math.abs(rnd.nextInt()%(scHeight-20));
int tempvx =Math.abs(rnd.nextInt()%(3))+1;
int tempvy =Math.abs(rnd.nextInt()%(3))+1;
balls[i] = new bub(center_x,scHeight +10,tempvx,tempvy);
}
}
public void updatestage(){
if (onoff){
move();
repaint();
}
}
///birth
public void birth(){
int yseed;
for(int i = 0; i < balls.length;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 +i*50;
yseed = Math.abs(rnd.nextInt()%(500));
balls[i].vy =(yseed + 100)/5;
balls[i].vx =(Math.abs(rnd.nextInt()%(500)));
balls[i].size =(yseed + 100)/200;
balls[i].r =Math.abs(rnd.nextInt()%(50));
}
}
}
////move
public void move(){
for (int i = 0; i < balls.length;i++) {
if (balls[i].moveFlag == 1){
if (balls[i].vx > 0){
if(balls[i].x- balls[i].ox > balls[i].size*300){
// balls[i].vx = 0 - balls[i].vx;
balls[i].vx = -100;
}
}else{
if(balls[i].ox- balls[i].x > balls[i].size*300){
// balls[i].vx = 0 - balls[i].vx;
balls[i].vx = 100;
}
}
if (balls[i].y < 0){
balls[i].x = center_x*100;
balls[i].y = scHeight*100;
balls[i].moveFlag = 0;
}else{
balls[i].x += balls[i].vx;
balls[i].y -= balls[i].vy;
}
}
}
}
// draw
public void paint(Graphics g) {
int tempcol;
g.setColor(0,0,255);
g.fillRect(0, 0, scWidth, scHeight);
g.setColor(255,255,255);
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++){
tempcol =balls[i].r + 50;
//偙偙偱bub 僋儔僗傪draw丅
g.setColor(255,255,255);
int xx = (balls[i].x/100);
int yy = (balls[i].y/100);
g.drawRect(xx,yy,balls[i].size,balls[i].size);
// 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 + -