有两个类
BirdThread 利用线程不断画图
Cage 提供frame
程序只在开始调用了paint()
run()中的repaint()没有调用paint()
import java.awt.*;
import java.awt.event.*;public class BirdThread extends Canvas implements Runnable{
private Thread birdThread=null;
private int xdir=(1-2*(int)Math.round(Math.random()));
private int ydir=(1-2*(int)Math.round(Math.random()));
private Cage cage=null;
protected int x,y;
Image bird=Toolkit.getDefaultToolkit().getImage("bird.jpg");
public BirdThread(Cage _cage,int _x,int _y)
{
cage=_cage;
x=_x;
y=_y;//System.out.println("o");
}
public void start()
{
if(birdThread==null)
{
birdThread=new Thread(this);
birdThread.start();
}//System.out.println("start");
}
public void stop()
{
birdThread=null;
} public void run()
{
Thread currentThread=Thread.currentThread();
while(birdThread==currentThread)
{
move();
this.repaint();
try{
birdThread.sleep(120);
}catch(InterruptedException ie){
System.err.println("Thread interrupted");
}
//System.out.println("run");
}
}
public void move()
{
x+=xdir;
y+=ydir;
if(x<0||x>cage.getSize().width) xdir=-xdir;
if(y<0||y>cage.getSize().height) ydir=-ydir;
//System.out.println("move");
}
public void paint(Graphics g)
{
g.drawImage(bird,x,y,30,30,this);
//System.out.println("paint");
}}
-----------------------------
import java.awt.*;
import java.awt.event.*;public class Cage extends Frame implements ActionListener{
private Button quit=new Button("Quit");
private Button start=new Button("Start");
private Button stop=new Button("Stop");
private BirdThread birds[]=new BirdThread[20];
public Cage()
{
super("Cage with birds");
Panel buttons=new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(quit); quit.addActionListener(this);
buttons.add(start); start.addActionListener(this);
buttons.add(stop); stop.addActionListener(this);
setLayout(new BorderLayout());
add("South",buttons);
//validate();
//pack(); for(int i=0;i<birds.length;i++)
{
int x=(int)(getSize().width*Math.random());
int y=(int)(getSize().width*Math.random());
birds[i]=new BirdThread(this,x,y);
add("Center",birds[i]);
}
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==quit)
System.exit(0);
if(ae.getSource()==start)
for(int i=0;i<birds.length;i++)
{
birds[i].stop();
birds[i]=new BirdThread(this,birds[i].x,birds[i].y);
birds[i].start();
}
if(ae.getSource()==stop)
for(int i=0;i<birds.length;i++)
birds[i].stop();
}
public static void main(String args[])
{
Cage cage=new Cage();
//System.out.println(cage.toString());
}
}
Canvas; run(); repaint()paint() runnable
BirdThread 利用线程不断画图
Cage 提供frame
程序只在开始调用了paint()
run()中的repaint()没有调用paint()
import java.awt.*;
import java.awt.event.*;public class BirdThread extends Canvas implements Runnable{
private Thread birdThread=null;
private int xdir=(1-2*(int)Math.round(Math.random()));
private int ydir=(1-2*(int)Math.round(Math.random()));
private Cage cage=null;
protected int x,y;
Image bird=Toolkit.getDefaultToolkit().getImage("bird.jpg");
public BirdThread(Cage _cage,int _x,int _y)
{
cage=_cage;
x=_x;
y=_y;//System.out.println("o");
}
public void start()
{
if(birdThread==null)
{
birdThread=new Thread(this);
birdThread.start();
}//System.out.println("start");
}
public void stop()
{
birdThread=null;
} public void run()
{
Thread currentThread=Thread.currentThread();
while(birdThread==currentThread)
{
move();
this.repaint();
try{
birdThread.sleep(120);
}catch(InterruptedException ie){
System.err.println("Thread interrupted");
}
//System.out.println("run");
}
}
public void move()
{
x+=xdir;
y+=ydir;
if(x<0||x>cage.getSize().width) xdir=-xdir;
if(y<0||y>cage.getSize().height) ydir=-ydir;
//System.out.println("move");
}
public void paint(Graphics g)
{
g.drawImage(bird,x,y,30,30,this);
//System.out.println("paint");
}}
-----------------------------
import java.awt.*;
import java.awt.event.*;public class Cage extends Frame implements ActionListener{
private Button quit=new Button("Quit");
private Button start=new Button("Start");
private Button stop=new Button("Stop");
private BirdThread birds[]=new BirdThread[20];
public Cage()
{
super("Cage with birds");
Panel buttons=new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(quit); quit.addActionListener(this);
buttons.add(start); start.addActionListener(this);
buttons.add(stop); stop.addActionListener(this);
setLayout(new BorderLayout());
add("South",buttons);
//validate();
//pack(); for(int i=0;i<birds.length;i++)
{
int x=(int)(getSize().width*Math.random());
int y=(int)(getSize().width*Math.random());
birds[i]=new BirdThread(this,x,y);
add("Center",birds[i]);
}
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==quit)
System.exit(0);
if(ae.getSource()==start)
for(int i=0;i<birds.length;i++)
{
birds[i].stop();
birds[i]=new BirdThread(this,birds[i].x,birds[i].y);
birds[i].start();
}
if(ae.getSource()==stop)
for(int i=0;i<birds.length;i++)
birds[i].stop();
}
public static void main(String args[])
{
Cage cage=new Cage();
//System.out.println(cage.toString());
}
}
Canvas; run(); repaint()paint() runnable
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货