public class Node { private int x; private int y; Image img; public Node(){ } public Image getImg() { return img; } public void setImg(Image img) { this.img = img; } public Node(int x, int y){ this.x=x; this.y=y; } public int getX() { return x; } public void setX(int x) { this.x = x; } 给这段代码加入图片
时间: 2024-02-14 18:04:01 浏览: 135
你可以使用 ImageIO 类加载图片,然后将其赋值给 Node 类中的 Image 类型的 img 属性。具体的代码实现可以参考下面的示例:
```
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Node {
private int x;
private int y;
private Image img;
public Node() {
}
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public static void main(String[] args) {
Node node = new Node(10, 20);
try {
Image img = ImageIO.read(new File("path/to/image.jpg"));
node.setImg(img);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
其中,我们使用 ImageIO 类的 read 方法从文件系统中加载图片,并将其赋值给 Node 类中的 img 属性。你需要将 "path/to/image.jpg" 替换为实际的图片路径。
阅读全文