当前位置: 技术问答>java相关
我用了MediaTracker 后 , 为什么 paint方法被不停的调用?
来源: 互联网 发布时间:2017-03-19
本文导语: import java.awt.event.*; import java.awt.*; public class test1 { public static void main(String args[]) { new SplashWindowFrame(); } } class SplashWindowFrame extends Frame { SplashWindow sw; Image splashIm; Sp...
import java.awt.event.*;
import java.awt.*;
public class test1 {
public static void main(String args[]) {
new SplashWindowFrame();
}
}
class SplashWindowFrame extends Frame {
SplashWindow sw;
Image splashIm;
SplashWindowFrame() {
super();
/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
dispose();
System.exit(0);
}});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,
(screenDim.height - frameDim.height) / 2);
MediaTracker mt = new MediaTracker(this);
splashIm = Toolkit.getDefaultToolkit(
).getImage("test.gif");
mt.addImage(splashIm,0);
try {
mt.waitForID(0);
} catch(InterruptedException ie){}
sw = new SplashWindow(splashIm);
this.add(sw, BorderLayout.CENTER);
try {
Thread.sleep(3000);
} catch(InterruptedException ie){}
/* Show the frame */
setVisible(true);
}
}
class SplashWindow extends Panel {
Image splashIm;
SplashWindow(Image splashIm)
{
this.splashIm = splashIm;
setSize(200,200);
/* Center the window */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winDim = getBounds();
setLocation((screenDim.width - winDim.width) / 2,
(screenDim.height - winDim.height) / 2);
setVisible(true);
}
public void paint(Graphics g) {
System.out.println("paint");
if (splashIm != null) {
g.drawImage(splashIm,0,0,this);
}
}
}
import java.awt.*;
public class test1 {
public static void main(String args[]) {
new SplashWindowFrame();
}
}
class SplashWindowFrame extends Frame {
SplashWindow sw;
Image splashIm;
SplashWindowFrame() {
super();
/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
dispose();
System.exit(0);
}});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,
(screenDim.height - frameDim.height) / 2);
MediaTracker mt = new MediaTracker(this);
splashIm = Toolkit.getDefaultToolkit(
).getImage("test.gif");
mt.addImage(splashIm,0);
try {
mt.waitForID(0);
} catch(InterruptedException ie){}
sw = new SplashWindow(splashIm);
this.add(sw, BorderLayout.CENTER);
try {
Thread.sleep(3000);
} catch(InterruptedException ie){}
/* Show the frame */
setVisible(true);
}
}
class SplashWindow extends Panel {
Image splashIm;
SplashWindow(Image splashIm)
{
this.splashIm = splashIm;
setSize(200,200);
/* Center the window */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winDim = getBounds();
setLocation((screenDim.width - winDim.width) / 2,
(screenDim.height - winDim.height) / 2);
setVisible(true);
}
public void paint(Graphics g) {
System.out.println("paint");
if (splashIm != null) {
g.drawImage(splashIm,0,0,this);
}
}
}
|
可能是你的image路径没给对。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。