📄 mycanvas.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class mycanvas extends Canvas
implements MouseListener,Runnable
{ public int num=0,eatnum=0;
Vector v1; //用v1存放全部movecircle对象
Thread t1;
int status=0; //当status=1时,movecircle对象才显示
datacanvas dc1;
int firststep,step=60; //firststep是卡通的起始位置,step是卡通的长度
mycanvas(datacanvas a)
{ dc1=a;
v1=new Vector();
t1=new Thread(this);
t1.start();
setBackground(Color.lightGray);
}
/* public void getimage(Image im)
{ im1=im;
}*/
public void paint(Graphics g)
{
g.drawRect(0,0,getWidth()-1,getHeight()-1);
g.fill3DRect(firststep,360,step,40,true);
if(status==1)
{for(int i=0;i<v1.size();i++)
{
movecircle c1=(movecircle)v1.elementAt(i);
c1.draw(g);
}
}
// g.drawString("all :"+Thread.currentThread().activeCount()+" name:"+Thread.currentThread().getName(),200,200);
g.setColor(Color.black);
}
public void run()
{ while(true){
try{
for(int i=0;i<num;i++){
movecircle c1=(movecircle)v1.elementAt(i);
if(c1.x<firststep+step & c1.x>firststep & c1.y>360) //在x轴的(firststep,firststep+step)区域把球吃掉
{ c1.stop(); //thread stop
v1.removeElementAt(i);
i=i-1;
num--;
eatnum++;
}
}
dc1.setmessage(num,eatnum);
repaint();
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
/* public void run()
{ String str;
while(true){
str=Thread.currentThread().getName(); //拿出当前的线程名称
try{
for(int i=0;i<thnum;i++){
if(str.equalsIgnoreCase("th"+i))
{
movecircle c1=(movecircle)v1.elementAt(i);
if(c1.x<firststep+step & c1.x>firststep & c1.y>360) //在x轴的(firststep,firststep+step)区域把球吃掉
{ v1.removeElementAt(i);
i=i-1;
num--;
eatnum++;
t3[i].stop();
}
else
{c1.move();}
}
}
// t3[2].stop();
repaint();
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
*/
public void mousePressed(MouseEvent e)
{ if(num<30) //球的数目<30
{num=num+1;
double a=Math.floor(Math.random()*10); //算出随机数0=<a,b<=10
double b=Math.floor(Math.random()*10);
movecircle c2=new movecircle(e.getX(),e.getY(),(int)(Math.pow(-1,a)*a),(int)(Math.pow(-1,b)*b),10);
v1.addElement(c2);
c2.start(); //thread start
repaint();
}
status=1;
}
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void setstep(int first,int a)
{
if(a<8)
firststep=first;//-(500-384)/2;
else if(a<11)
firststep=first+20;
else
firststep=first+50;
}
public void threadstop()
{ for(int i=0;i<num;i++){
movecircle c1=(movecircle)v1.elementAt(i);
c1.stop();
}
v1.removeAllElements();
}
public void initball()
{ num=0;
eatnum=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -