±Û¾´ÀÌ :Ãʺ¸ÀÚ 2000³â 5¿ù 08ÀÏ 12:47:29
import java.awt.*;
import java.awt.event.*;
class MovePenguin extends Frame implements ActionListener, Runnable{
final int pace=2;
Image bgImg=getToolkit().getImage("images/jordan.jpg");
Image penguin[]=new Image[3];
int currentImg=0;
Point point=new Point();
boolean isMoving=false;
MovePenguin(String title){
super(title);
penguin[0]=getToolkit().getImage("images/penguin1.gif");
penguin[1]=getToolkit().getImage("images/penguin2.gif");
penguin[2]=getToolkit().getImage("images/penguin3.gif");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
dispose();
System.exit(0);
}
});
Button button=new Button("Action");
button.addActionListener(this);
add(button, "South");
doLayout();
setSize(300,500);
setVisible(true);
}
public static void main(String args[]){
new MovePenguin("MovePenguin");
}
public void actionPerformed(ActionEvent ae){
String cmd=ae.getActionCommand();
if(cmd.equals("Action")){
new Thread(this).start();
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
Dimension dim=getSize();
g.drawImage(bgImg,0,0,dim.width, dim.height, this);
if(isMoving)
g.drawImage(penguin[currentImg],point.x, point.y, this);
}
public synchronized void run(){
isMoving=true;
Dimension dim=getSize();
point.x=-penguin[0].getWidth(this);
//¿©±â¿¡¼ point.x-=penguin[0].getWidth(this);
// ÇÑ °Í°úÀÇ Â÷ÀÌÁ¡Àº ¹«¾ùÀÎÁö¿ä?
// ±³Àç¿¡¼´Â À§ÀÇ ¹æ¹ý´ë·Î ³ª¿Ô´Âµ¥
//Àú´Â ¾Æ·¡¹æ¹ýó·³ Çߴµ¥ Action¹öưÀ» µÎ¹ø ´©¸£´Ï µÎ¹øÂ°·Î ³ª¿À´Â ÆëÄýÀº
// ȸéÀÇ ¿À¸¥ÂÊ ³¡ºÎºÐ¿¡¼ Æ¢¾î³ª¿Í¼ ±Ý¹æ µé¾î°¡ ¹ö¸®´õ±º¿ä.
//¹°·Ð Ã¥¿¡¼ ³ª¿Â´ë·Î Çϸé È¸é ¿ÞÂʺÎÅÍ ³ª¿Í¼ ¿À¸¥ÂÊÀ¸·Î µé¾î°©´Ï´Ù.
//ÀÌ µÎ°¡ÁöÀÇ Â÷ÀÌÁ¡Àº ¹«¾ùÀԴϱî?
point.y=dim.height/2;
while(point.x<dim.width){
try{
Thread.currentThread().sleep(50);
}catch(InterruptedException ie){}
point.x=+pace;
currentImg=(++currentImg)%3;
repaint();
}
isMoving=false;
}
}