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

How to set background image in Java?(如何在 Java 中设置背景图像?)

本文介绍了如何在 Java 中设置背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 开发一个简单的平台游戏,并使用 BlueJ 作为 IDE.现在,我在游戏中使用多边形和简单形状绘制了玩家/敌人精灵、平台和其他项目.最终我希望用实际图像替换它们.

I am developing a simple platform game using Java using BlueJ as the IDE. Right now I have player/enemy sprites, platforms and other items in the game drawn using polygons and simple shapes. Eventually I hope to replace them with actual images.

现在我想知道将图像(URL 或来自本地源)设置为我的游戏窗口/画布的背景"的最简单解决方案是什么?

For now I would like to know what is the simplest solution to setting an image (either URL or from local source) as the 'background' of my game window/canvas?

如果它不是很长或不太复杂,我将不胜感激,因为我的编程技能不是很好,我想让我的程序尽可能简单.请提供带有注释的示例代码来详细说明它们的功能,如果它在它自己的类中,如何调用它在其他类中使用的相关方法.

I would appreciate it if it isn't something long or complex as my programming skills aren't very good and I want to keep my program as simple as possible. Kindly provide example codes with comments to elaborate on their function, and also if it's in its own class, how to call on relevant methods used by it on other classes.

非常感谢.

推荐答案

答案会因应用程序或小程序是否使用而略有不同AWT 或 摇摆.

The answer will vary slightly depending on whether the application or applet is using AWT or Swing.

(基本上JAppletJFrameJ开头的类就是Swing,AppletFrame 是 AWT.)

(Basically, classes that start with J such as JApplet and JFrame are Swing, and Applet and Frame are AWT.)

在任何一种情况下,基本步骤都是:

In either case, the basic steps would be:

  1. 将图像绘制或加载到 Image 对象中.
  2. 在你要绘制背景的组件的painting事件中绘制背景图片.
  1. Draw or load an image into a Image object.
  2. Draw the background image in the painting event of the Component you want to draw the background in.

第 1 步. 加载图像可以使用 Toolkit 类或 ImageIO 类.

Step 1. Loading the image can be either by using the Toolkit class or by the ImageIO class.

Toolkit.createImage 方法可用于从 String 中指定的位置加载 Image:

The Toolkit.createImage method can be used to load an Image from a location specified in a String:

Image img = Toolkit.getDefaultToolkit().createImage("background.jpg");

同样可以使用ImageIO:

Image img = ImageIO.read(new File("background.jpg");

第 2 步. 需要覆盖应该获取背景的 Component 的绘制方法并将 Image 绘制到组件上.

Step 2. The painting method for the Component that should get the background will need to be overridden and paint the Image onto the component.

对于 AWT,覆盖的方法是 paint 方法,并使用 drawImage 方法图形传递给 paint 方法的对象:

For AWT, the method to override is the paint method, and use the drawImage method of the Graphics object that is handed into the paint method:

public void paint(Graphics g)
{
    // Draw the previously loaded image to Component.
    g.drawImage(img, 0, 0, null);

    // Draw sprites, and other things.
    // ....
}

对于 Swing,覆盖的方法是 paintComponent 方法的 JComponent,并像在 AWT 中所做的那样绘制 Image.

For Swing, the method to override is the paintComponent method of the JComponent, and draw the Image as with what was done in AWT.

public void paintComponent(Graphics g)
{
    // Draw the previously loaded image to Component.
    g.drawImage(img, 0, 0, null);

    // Draw sprites, and other things.
    // ....
}

简单组件示例

这是一个 Panel,它在实例化时加载一个图像文件,并在其自身上绘制该图像:

Here's a Panel which loads an image file when instantiated, and draws that image on itself:

class BackgroundPanel extends Panel
{
    // The Image to store the background image in.
    Image img;
    public BackgroundPanel()
    {
        // Loads the background image and stores in img object.
        img = Toolkit.getDefaultToolkit().createImage("background.jpg");
    }

    public void paint(Graphics g)
    {
        // Draws the img to the BackgroundPanel.
        g.drawImage(img, 0, 0, null);
    }
}

有关绘画的更多信息:

  • AWT 和 Swing 中的绘画
  • 课程:执行自定义绘画,来自 Java 教程 可能会有所帮助.
  • Painting in AWT and Swing
  • Lesson: Performing Custom Painting from The Java Tutorials may be of help.

这篇关于如何在 Java 中设置背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Java 中设置背景图像?

基础教程推荐