【问题标题】:How to set a background image in a Java Applet?如何在 Java Applet 中设置背景图像?
【发布时间】:2014-01-23 17:59:28
【问题描述】:

如何在 Java Applet 中设置背景图像?

我有一个 .gif 动画图像,我希望它成为我的 Java Applet 中的背景。我该怎么做?

【问题讨论】:

标签: java image background applet


【解决方案1】:

这里有很多可能的答案。

这是其中之一。

假设您有一个扩展 Applet 的 BcgImageApplet 类: 为了给你的小程序设置背景图片,你应该使用这个:

public class BcgImageApplet extends Applet  {

    Image I;

    //Irelevant code avoided.

    public void init() {
        I=getImage(getCodeBase(),”your_picture.jpg”);
    }

    //Irelevant code avoided.

}

简而言之 - 在您的 init() 方法中,您必须将图像设置为您的 Applet。

【讨论】:

  • 没问题,如果您有更多解决方案/说明,我只是发布。
【解决方案2】:

以下可能是一个解决方案:

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;    
     }
}

您可以在此处找到更多信息:Background Images in Java Applets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2012-08-04
    • 2013-03-07
    • 2021-08-07
    • 2023-03-04
    • 2018-01-08
    • 1970-01-01
    相关资源
    最近更新 更多