📄 mycanvas.java
字号:
//colorbugsCanvas
import javax.microedition.lcdui.*;
import java.util.Random;
public class MyCanvas extends Canvas{
totathread mythread;
private fish balls[]; //fish objlist
private area areas[];
private int targArea = 4;
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 fish[12]; // 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 fish(tempx,tempy,tempvx,tempvy);
}
areas = new area[9];
areas[0] = new area(scWidth/4 *100,scHeight/4 *100,255,0,0);
areas[1] = new area(scWidth/4 *100 *2 ,scHeight/4 *100,255,125,225);
areas[2] = new area(scWidth/4 *100 *3 ,scHeight/4 *100,125,0,255);
areas[3] = new area(scWidth/4 *100,scHeight/4 *100*2,0,0,255);
areas[4] = new area(scWidth/4 *100*2,scHeight/4 *100*2,0,255,255);
areas[5] = new area(scWidth/4 *100*3,scHeight/4 *100*2,0,255,125);
areas[6] = new area(scWidth/4 *100,scHeight/4 *100*3,0,255,0);
areas[7] = new area(scWidth/4 *100*2,scHeight/4 *100*3,255,255,0);
areas[8] = new area(scWidth/4 *100*3,scHeight/4 *100*3,255,125,0);
chnageArea();
}
public void updatestage(){
if (onoff){
move();
repaint();
}
}
////move
public void move(){
for (int i = 0; i < balls.length;i++) {
balls[i].nvx = (gx - balls[i].x) /50;
balls[i].nvy = (gy - balls[i].y) /50;
//speedup
if ((balls[i].vx < 12 || balls[i].vy < -12) && (balls[i].vy < 12 || balls[i].vy < -12)) {
balls[i].vx = (balls[i].vx/10)* (11+Math.abs(rnd.nextInt()%(5)));
balls[i].vy = (balls[i].vy/10)* (11+Math.abs(rnd.nextInt()%(5)));
}
//slowdown
if (balls[i].vx > 100 || balls[i].vx < -100 || balls[i].vy > 100 || balls[i].vy < -100) {
balls[i].vx = (balls[i].vx/10)* 8;
balls[i].vy = (balls[i].vy/10)* 8;
}
if (dist(i)< 25000) {
balls[i].r = balls[i].r +( areas[targArea].r - balls[i].r)/10;
balls[i].g = balls[i].g +( areas[targArea].g - balls[i].g)/10;
balls[i].b = balls[i].b +( areas[targArea].b - balls[i].b)/10;
}
balls[i].vx += balls[i].nvx;
balls[i].vy += balls[i].nvy;
balls[i].x += balls[i].vx;
balls[i].y += balls[i].vy;
}
}
//distance
public int dist(int i) {
return (balls[i].x-gx) * (balls[i].x-gx) + (balls[i].y-gy) * (balls[i].y-gy);
}
public void chnageArea(){
for(int i = 0; i < areas.length;i++){
if (i == targArea){
areas[i].setmorph(1600);
}else{
areas[i].setmorph(400);
}
}
}
// draw
public void paint(Graphics g) {
g.setColor(0,0,0);
g.fillRect(0, 0, scWidth, scHeight);
for(int i = 0; i < areas.length;i++){ //偙偙偱area 僋儔僗傪draw丅
areas[i].update();
int xx = (areas[i].x/100);
int yy = (areas[i].y/100);
// if (i == targArea){
g.setColor(areas[i].r,areas[i].g,areas[i].b);
int tempw = areas[i].w/100;
g.drawRect(xx-(tempw/2),yy-(tempw/2),tempw,tempw);
// }else{
// g.setColor(areas[i].dr,areas[i].dg ,areas[i].db);
// g.drawRect(xx-2,yy-2,4,4);
// }
}
for(int i = 0; i < balls.length;i++){ //偙偙偱fish 僋儔僗傪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.fillRect(xx-1,yy-1,2,2);
g.drawLine(xx, yy,(xx + balls[i].vx/100), (yy + balls[i].vy/100));
}
}
public void keyPressed(int keyCode){
switch(keyCode){
case KEY_NUM1: gy = areas[0].y;
gx = areas[0].x;
targArea = 0;
chnageArea();
break;
case KEY_NUM2: gy = areas[1].y;
gx = areas[1].x;
targArea = 1;
chnageArea();
break;
case KEY_NUM3: gy = areas[2].y;
gx = areas[2].x;
targArea = 2;
chnageArea();
break;
case KEY_NUM4: gy = areas[3].y;
gx = areas[3].x;
targArea = 3;
chnageArea();
break;
case KEY_NUM5: gy = areas[4].y;
gx = areas[4].x;
targArea = 4;
chnageArea();
break;
case KEY_NUM6: gy = areas[5].y;
gx = areas[5].x;
targArea = 5;
chnageArea();
break;
case KEY_NUM7: gy = areas[6].y;
gx = areas[6].x;
targArea = 6;
chnageArea();
break;
case KEY_NUM8: gy = areas[7].y;
gx = areas[7].x;
targArea = 7;
chnageArea();
break;
case KEY_NUM9: gy = areas[8].y;
gx = areas[8].x;
targArea = 8;
chnageArea();
break;
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -