当前位置: 技术问答>java相关
亚历士多德曾经说:“给我一个支点,我将撬动地球!”,为什么我仅仅只是点击了一下run的按钮,整个地球就不见了?
来源: 互联网 发布时间:2015-07-09
本文导语: 此Applet是最常见的地球转动的动画,但按例程处理编译后的窗口内空空如也,请大虾帮忙看一下代码,告知一下问题出在哪里,多谢? Animation.java ////////////////////////////////////////////////////////////////////////////////////////////...
此Applet是最常见的地球转动的动画,但按例程处理编译后的窗口内空空如也,请大虾帮忙看一下代码,告知一下问题出在哪里,多谢?
Animation.java
/////////////////////////////////////////////////////////////////////////////////////////////////////////
package animation;
/**
* @version 1.20 1999-04-23
* @author Cay Horstmann
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.net.URL;
public class Animation extends JApplet
implements Runnable
{ public void init()
{ addMouseListener(new MouseAdapter()
{ public void mousePressed(MouseEvent evt)
{ if (runner == null)
start();
else
stop();
}
});
try
{ imageCount = 36;
}
catch (Exception e)
{ showStatus("Error: " + e);
}
current = 0;
image = null;
loadImage();
}
public void loadImage()
{ try
{
URL imgURL = getDocumentBase();
image = getImage(imgURL,"globe.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
resize(imageWidth, imageHeight / imageCount);
}
catch (InterruptedException e)
// thrown by MediaTracker.waitFor
{ showStatus("Loading interrupted");
}
}
public void paintComponent(Graphics g)
{ if (image == null) return;
g.drawImage(image, 0, - (imageHeight / imageCount)
* current, null);
}
public void start()
{ runner = new Thread(this);
runner.start();
showStatus("Click to stop");
}
public void stop()
{ runner.interrupt();
runner = null;
showStatus("Click to restart");
}
public void run()
{ try
{ while (!Thread.interrupted())
{ repaint();
current = (current + 1) % imageCount;
Thread.sleep(200);
}
}
catch(InterruptedException e) {}
}
private Image image;
private int current;
private int imageCount;
private int imageWidth;
private int imageHeight;
private String imageName;
private Thread runner;
}
Animation.java
/////////////////////////////////////////////////////////////////////////////////////////////////////////
package animation;
/**
* @version 1.20 1999-04-23
* @author Cay Horstmann
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.net.URL;
public class Animation extends JApplet
implements Runnable
{ public void init()
{ addMouseListener(new MouseAdapter()
{ public void mousePressed(MouseEvent evt)
{ if (runner == null)
start();
else
stop();
}
});
try
{ imageCount = 36;
}
catch (Exception e)
{ showStatus("Error: " + e);
}
current = 0;
image = null;
loadImage();
}
public void loadImage()
{ try
{
URL imgURL = getDocumentBase();
image = getImage(imgURL,"globe.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
resize(imageWidth, imageHeight / imageCount);
}
catch (InterruptedException e)
// thrown by MediaTracker.waitFor
{ showStatus("Loading interrupted");
}
}
public void paintComponent(Graphics g)
{ if (image == null) return;
g.drawImage(image, 0, - (imageHeight / imageCount)
* current, null);
}
public void start()
{ runner = new Thread(this);
runner.start();
showStatus("Click to stop");
}
public void stop()
{ runner.interrupt();
runner = null;
showStatus("Click to restart");
}
public void run()
{ try
{ while (!Thread.interrupted())
{ repaint();
current = (current + 1) % imageCount;
Thread.sleep(200);
}
}
catch(InterruptedException e) {}
}
private Image image;
private int current;
private int imageCount;
private int imageWidth;
private int imageHeight;
private String imageName;
private Thread runner;
}
|
public void paintComponent(Graphics g)
改为
public void paint(Graphics g)
试试
改为
public void paint(Graphics g)
试试
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。