Java 2D 图形:无法覆盖图像
我试图掌握java 2d图形
我基本上得到了一个带有背景图像的JPanel,如下所示:
public MapFrame(Plotting pl){
this.pl =pl;
this.setPreferredSize(new Dimension(984,884));
this.setBorder(BorderFactory.createEtchedBorder());
try {
getFileImage("stars.jpg");
}
catch (Exception ex) {
}
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, null);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(0x756b48));
g2d.drawLine(0,0,0,100);
}
private void getFileImage(String filePath) throws InterruptedException, IOException {
FileInputStream in = new FileInputStream(filePath);
byte [] b=new byte[in.available()];
in.read(b);
in.close();
bg=Toolkit.getDefaultToolkit().createImage(b);
MediaTracker mt=new MediaTracker(this);
mt.addImage(bg,0);
mt.waitForAll();
}
在绘图组件中,我想在从某些xml获得的各个xy点处循环覆盖12x12像素的小图像。
似乎无法将图像覆盖在我的第一个图像上,
我在这里有点迷失,v 生锈了
任何帮助都会 b gr8
Im trying to get to grips wth java 2d graphics
Ive basically got a JPanel with a backgrounfd image in it like so:
public MapFrame(Plotting pl){
this.pl =pl;
this.setPreferredSize(new Dimension(984,884));
this.setBorder(BorderFactory.createEtchedBorder());
try {
getFileImage("stars.jpg");
}
catch (Exception ex) {
}
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, null);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(0x756b48));
g2d.drawLine(0,0,0,100);
}
private void getFileImage(String filePath) throws InterruptedException, IOException {
FileInputStream in = new FileInputStream(filePath);
byte [] b=new byte[in.available()];
in.read(b);
in.close();
bg=Toolkit.getDefaultToolkit().createImage(b);
MediaTracker mt=new MediaTracker(this);
mt.addImage(bg,0);
mt.waitForAll();
}
In paint component I want to overlay small images 12x12 pixels in a loop at various xy points that ill get from some xml.
Cant seem to get an image to overlay over my first one
Im a bit lost here and v rusty
Any help would b gr8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您已经这样做了,请更清楚地指定您的 XML 是如何解析的。然后,您还需要加载“12x12”图像。
SomeXMLObject
是一个包含从 XML 中提取的x
和y
变量的结构。如果您在背景之后调用 g.drawImage(...) :它将在背景之后绘制,从而覆盖。如果您愿意,请确保加载 png-24 图像以启用半透明区域。
Please specify clearer how your XML is parsed, if you already did that. Then, you'd also need to load the "12x12" image.
SomeXMLObject
is a structure containingx
andy
variables, extracted from your XML.If you call g.drawImage(...) after the background: it will be painted after the background, and thus overlay. Be sure you load a png-24 image, to enable translucency-areas, if you'd want that.
如果您想在不同位置绘制图像,只需针对不同坐标多次调用 Graphics.drawImage(Image, int, int, ImageObserver) 即可(如上一个答案所示)。
至于加载图像,我建议使用
ImageIO.read
方法而不是自己执行。If you want to paint an image at various locations, it is as simple as calling
Graphics.drawImage(Image, int, int, ImageObserver)
multiple times for different coordinates (as shown in the previous answer).As for loading images, I'd recommend using one of the
ImageIO.read
methods instead of doing it yourself.您可能想使用 ImageIO 库< /a> 加载您的图像。如果您有一个图像文件名,则加载它所需要做的就是
这比上面的代码要容易一些。
之后,就像其他人所说的那样,您可以使用 g.drawImage(bimg,x,y,this); 来实际绘制图像。
You probably want to use use the ImageIO library to load your image. If you have an image filename all you need to do to load it is
That's a little easier then the code you have above.
After that, like other people said you can use the
g.drawImage(bimg,x,y,this);
to actually draw the images.哦,亲爱的,
我的资源文件名格式错误,
我真是个驴子,
我想虽然伙计们,但我认为都是很好的建议
Oh Dear
Id formatted the filenames of my resources wrong
what a donkey I am
All good advice I think though guys