如何设置背景图片在Java中? [英] How to set background image in 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?
我想AP preciate它,如果它是不是长或复杂的我的编程技巧不是很好,我想保持我的程序越简单越好。请提供例如codeS与评论阐述它们的功能,并且如果它在自己的类,如何对其他类使用它相关的方法调用。
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.
非常感谢你。
推荐答案
答案将略有不同的应用程序或applet是否正在使用的 AWT 或摇摆。
The answer will vary slightly depending on whether the application or applet is using AWT or Swing.
(基本上与Ĵ
启动类,如 JApplet的
和的JFrame
是Swing和 Applet的
和框架
是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:
- 绘制或图像加载到
图片
对象。 - 绘制背景图像中的
组件的写生活动
要绘制的背景。
- Draw or load an image into a
Image
object. - Draw the background image in the painting event of the
Component
you want to draw the background in.
第1步加载图像可以使用的 工具包
类或通过的 的ImageIO
类。
Step 1. Loading the image can be either by using the Toolkit
class or by the ImageIO
class.
的<一个href=\"http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(java.lang.String)\"><$c$c>Toolkit.createImage$c$c>方法可用于从指定的位置加载图片
A 字符串
:
Image img = Toolkit.getDefaultToolkit().createImage("background.jpg");
同样,的ImageIO
可以用:
Image img = ImageIO.read(new File("background.jpg");
第2步作为组件
绘画方法,应该让后台将需要重写和油漆的图片
上的组件。
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,要覆盖的方法是<一个href=\"http://java.sun.com/javase/6/docs/api/java/awt/Component.html#paint(java.awt.Graphics)\"><$c$c>paint$c$c>方法,以及使用<一href=\"http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20java.awt.image.ImageObserver)\"><$c$c>drawImage$c$c> 图形的方式
<被移交到油漆/ A>对象
方法:
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.
// ....
}
有关秋千,要覆盖的方法是<一个href=\"http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)\"><$c$c>paintComponent$c$c>的<方法href=\"http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html\"><$c$c>JComponent$c$c>,并绘制图片
与什么在AWT完成。
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.
// ....
}
简单组件实例
下面是一个面板
它加载实例化时的图像文件,并借鉴了本身形象:
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);
}
}
有关绘画的更多信息:
- Painting in AWT and Swing
- Lesson: Performing Custom Painting from The Java Tutorials may be of help.
这篇关于如何设置背景图片在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!