如何在另一张图像上绘制图像? [英] How to draw an image over another image?

查看:120
本文介绍了如何在另一张图像上绘制图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于随机城市中的交通网络模拟的Java项目,我已经设法找到实现这个项目的方法,所以我将每个交叉点划分为一个基本上是扩展JPanel类的部分(名为Carrefour) )...一切顺利,直到我遇到如何绘制车辆并让它们通过道路。

I have a Java project that's about traffic network simulation in a random city, I've managed to figure out a way to implement this project, so I divided each intersection into a section which is basically an extended JPanel class (named Carrefour)...everything works well until I got stuck with how to draw vehicles and make them pass through roads.

所以我的问题是如何绘制图像(车辆图像) )在另一个图像(道路)上?

So my problem is how to draw an image (vehicle image) over an another image (road)?

推荐答案

如果这是Swing,则在BufferedImage中绘制背景图像。使用Graphic的<​​code> drawImage(...)方法在JComponent(例如JPanel的)paintComponent方法中显示此BufferedImage,然后在同一paintComponent方法中绘制更改的图像。不要忘记先调用 super.paintComponent(...)方法。

If this is Swing, then draw the background image in a BufferedImage. Display this BufferedImage in a JComponent's (such as a JPanel's) paintComponent method using Graphic's drawImage(...) method, and then draw the changing images over this in the same paintComponent method. Don't forget to call the super.paintComponent(...) method first though.

请注意这个问题已在这里和其他地方被提出过很多问题,正如你所料,有很多这类事情的例子你可以在这里找到一些搜索。

Please note that this question has been asked quite a bit here and elsewhere, and as you would expect, there are lots of examples of this sort of thing that you can find here with a bit of searching.

编辑

您问:

Edit
You ask:


谢谢,这就是我的看法绘制firt图像(道路)

Thanks, this is how I draw the firt image (road)

再次,您可以为此创建一个BufferedImage,可能使用 ImageIO .read(...)。然后你使用 g.drawImage(...) paintComponent(Graphics g)方法覆盖中绘制它>。

Again, you would create a BufferedImage for this, likely by using ImageIO.read(...). Then you'd draw this in your JPanel's paintComponent(Graphics g) method override using g.drawImage(...).

例如......

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class IntersectionImagePanel extends JPanel {
   private static final String INTERSECTION_LINK = "http://www.weinerlawoffice.com/" +
        "accident-diagram.jpg";
   private BufferedImage intersectionImage;

   public IntersectionImagePanel() {
      URL imageUrl;
      try {
         imageUrl = new URL(INTERSECTION_LINK);
         intersectionImage = ImageIO.read(imageUrl );
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (intersectionImage != null) {
         g.drawImage(intersectionImage, 0, 0, this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (intersectionImage != null) {
         int width = intersectionImage.getWidth();
         int height = intersectionImage.getHeight();
         return new Dimension(width , height );
      }
      return super.getPreferredSize();
   }

   private static void createAndShowGui() {
      IntersectionImagePanel mainPanel = new IntersectionImagePanel();

      JFrame frame = new JFrame("IntersectionImage");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

这篇关于如何在另一张图像上绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆