简体   繁体   English

设置背景颜色/图像

[英]Setting background color/image

graphics.setColor(Color.blue);
graphics.fillRect(0, 0, myWidth, myHeight);

Instead of setting the background to the color blue, is there a way I can show an image from a specified file? 除了将背景设置为蓝色外,还有什么方法可以显示指定文件中的图像?

ps Can the image be a GIF(and move?) ps图像可以是GIF(并移动吗?)

What about drawImage method of Graphics class? 那Graphics类的drawImage方法呢?

Here is an example http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html 这是一个示例http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

"Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP". “图像I / O内置了对GIF,PNG,JPEG,BMP和WBMP的支持”。 I think it can easily move too. 我认为它也可以轻松移动。

I don't know about GIF but you can normally show other images. 我不知道GIF,但是您通常可以显示其他图像。

In the constructor, load the image like this 在构造函数中,像这样加载图像

backGroundImage = new ImageIcon("my-image.png").getImage();

Then render it every time with Graphics . 然后每次使用Graphics渲染它。

graphics.drawImage(backGroundImage, 0, 0, myWidth, myHeight);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM