如何在Java 7中插入自己的图形

时间:2023-02-09 15:47:18

I am working on a game but I want to put my own graphics into Java. I am using Eclipse and Java 7. I was wondering of a library that can take care of that and help you use your own graphics.

我正在开发游戏,但我想将自己的图形放入Java中。我正在使用Eclipse和Java 7.我想知道一个可以处理它并帮助您使用自己的图形的库。

Edit: What my goal is, is to draw an image, then put in into the program as an object.

编辑:我的目标是绘制图像,然后作为对象放入程序中。

1 个解决方案

#1


2  

Taking and manipulating existing images can be done with the Java Image class.

可以使用Java Image类完成对现有图像的处理和操作。

First, declare an instance of the class (BufferedImage is a subclass of Image):

首先,声明一个类的实例(BufferedImage是Image的子类):

private BufferedImage img = null;

Then, you load the image like this:

然后,您加载图像如下:

try
{
    img = ImageIO.read( new File("MyPicture.jpg" ));
}
catch ( IOException exc )
{
    //TODO: Handle exception.
}

Finally, in the paint(Graphics) method of your code, you display the image like this:

最后,在代码的paint(Graphics)方法中,显示如下图像:

g.drawImage( img, x, y, this );

Don't forget to write some code to handle the Exception. These things help you find and solve bugs more easily.

不要忘记编写一些代码来处理异常。这些东西可以帮助您更轻松地找到并解决错误。

The official tutorial is here.

官方教程就在这里。

#1


2  

Taking and manipulating existing images can be done with the Java Image class.

可以使用Java Image类完成对现有图像的处理和操作。

First, declare an instance of the class (BufferedImage is a subclass of Image):

首先,声明一个类的实例(BufferedImage是Image的子类):

private BufferedImage img = null;

Then, you load the image like this:

然后,您加载图像如下:

try
{
    img = ImageIO.read( new File("MyPicture.jpg" ));
}
catch ( IOException exc )
{
    //TODO: Handle exception.
}

Finally, in the paint(Graphics) method of your code, you display the image like this:

最后,在代码的paint(Graphics)方法中,显示如下图像:

g.drawImage( img, x, y, this );

Don't forget to write some code to handle the Exception. These things help you find and solve bugs more easily.

不要忘记编写一些代码来处理异常。这些东西可以帮助您更轻松地找到并解决错误。

The official tutorial is here.

官方教程就在这里。