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

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

问题描述

我有一个关于随机城市交通网络模拟的 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 的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.

编辑
你问:

谢谢,这就是我绘制第一个图像(道路)的方式

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

同样,您可以为此创建一个 BufferedImage,可能是使用 ImageIO.read(...).然后,您将使用 g.drawImage(...) 在 JPanel 的 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天全站免登陆