如何在 Java Applet 中设置背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21315928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a background image in a Java Applet?
提问by Tom Doyle
How would I set a background-image in a Java Applet?
如何在 Java Applet 中设置背景图像?
I have an animated .gif image, and I want it to be the background in my Java Applet. How would I do this?
我有一个动画 .gif 图像,我希望它成为我的 Java Applet 中的背景。我该怎么做?
采纳答案by Benedikt Bock
The Following could be a solution:
以下可能是一个解决方案:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.IOException.*;
public class BackgroundApplet extends Applet {
Image backGround;
public void init() {
// set the size of the applet to the size of the background image.
// Resizing the applet may cause distortion of the image.
setSize(300, 300);
// Set the image name to the background you want. Assumes the image
// is in the same directory as the class file is
backGround = getImage(getCodeBase(), "save.GIF");
BackGroundPanel bgp = new BackGroundPanel();
bgp.setLayout(new FlowLayout());
bgp.setBackGroundImage(backGround);
// Add the components you want in the Applet to the Panel
bgp.add(new Button("Button 1"));
bgp.add(new TextField("isn't this cool?"));
bgp.add(new Button("Useless Button 2"));
// set the layout of the applet to Border Layout
setLayout(new BorderLayout());
// now adding the panel, adds to the center
// (by default in Border Layout) of the applet
add(bgp);
}
}
class BackGroundPanel extends Panel {
Image backGround;
BackGroundPanel() {
super();
}
public void paint(Graphics g) {
// get the size of this panel (which is the size of the applet),
// and draw the image
g.drawImage(getBackGroundImage(), 0, 0,
(int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
}
public void setBackGroundImage(Image backGround) {
this.backGround = backGround;
}
private Image getBackGroundImage() {
return backGround;
}
}
You'll find further Information here: Background Images in Java Applets
您将在此处找到更多信息:Java Applets 中的背景图像
回答by Bosko Mijin
Here is many possible answers.
这里有很多可能的答案。
Here is the one of them.
这是其中之一。
Suppose that you have a Class BcgImageApplet which extends Applet: In order to set background image to your applet you should use this:
假设您有一个扩展 Applet 的类 BcgImageApplet:为了将背景图像设置为您的小程序,您应该使用以下命令:
public class BcgImageApplet extends Applet {
Image I;
//Irelevant code avoided.
public void init() {
I=getImage(getCodeBase(),”your_picture.jpg”);
}
//Irelevant code avoided.
}
In short - in your init() method you have to set image to your Applet.
简而言之 - 在您的 init() 方法中,您必须将图像设置为您的 Applet。