Java 2D 图形:无法覆盖图像

发布于 2024-08-18 08:16:31 字数 1158 浏览 5 评论 0原文

我试图掌握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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-08-25 08:16:31
public void paintComponent(Graphics g) {
    g.drawImage(bg, 0, 0, null);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(0x756b48));
    g2d.drawLine(0,0,0,100);

    for(SomeXMLObject o : yourXMLSource) {
        g.drawImage(yourImage, o.x, o.y, null);
    }
}

如果您已经这样做了,请更清楚地指定您的 XML 是如何解析的。然后,您还需要加载“12x12”图像。 SomeXMLObject 是一个包含从 XML 中提取的 xy 变量的结构。

如果您在背景之后调用 g.drawImage(...) :它将在背景之后绘制,从而覆盖。如果您愿意,请确保加载 png-24 图像以启用半透明区域。

public void paintComponent(Graphics g) {
    g.drawImage(bg, 0, 0, null);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(0x756b48));
    g2d.drawLine(0,0,0,100);

    for(SomeXMLObject o : yourXMLSource) {
        g.drawImage(yourImage, o.x, o.y, null);
    }
}

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 containing x and y 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.

独行侠 2024-08-25 08:16:31

如果您想在不同位置绘制图像,只需针对不同坐标多次调用 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.

绮烟 2024-08-25 08:16:31

您可能想使用 ImageIO 库< /a> 加载您的图像。如果您有一个图像文件名,则加载它所需要做的就是

BufferedImage bimg = ImageIO.load(new File(filename));

这比上面的代码要容易一些。

之后,就像其他人所说的那样,您可以使用 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

BufferedImage bimg = ImageIO.load(new File(filename));

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.

一个人练习一个人 2024-08-25 08:16:31

哦,亲爱的,

我的资源文件名格式错误,

我真是个驴子,

我想虽然伙计们,但我认为都是很好的建议

Oh Dear

Id formatted the filenames of my resources wrong

what a donkey I am

All good advice I think though guys

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文