如何在另一个图像上绘制图像?

我有一个关于随机城市交通networking模拟的Java项目,我设法find了实现这个项目的方法,所以我把每个交叉点分成了一个基本上是扩展的JPanel类(叫做家乐福)的部分。一切工作都很顺利,直到我陷入如何绘制车辆并使它们通过道路。

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

如果这是Swing,那么在BufferedImage中绘制背景图像。 使用Graphic的drawImage(...)方法在JComponent(例如JPanel的)paintComponent方法中显示此BufferedImage,然后在同一个paintComponent方法中通过此方法绘制变化的图像。 不要忘记先调用super.paintComponent(...)方法。

请注意,这个问题在这里和其他地方都有很多问题,正如你所期望的那样,有很多这样的例子,你可以在这里find一些search。

编辑
你问:

谢谢,这是我如何绘制图像(道路)

再次,你可以为此创build一个BufferedImage,可能通过使用ImageIO.read(...) 。 然后,使用g.drawImage(...)在您的JPanel的paintComponent(Graphics g)方法重写中绘制该paintComponent(Graphics g)

例如…

 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(); } }); } } 

另一种不需要扩展组件的方法。

在这里输入图像描述

 import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; import java.net.URL; import javax.imageio.ImageIO; public class ImageOnImage { ImageOnImage(final BufferedImage bg, BufferedImage fg) { final BufferedImage scaled = new BufferedImage( fg.getWidth()/2,fg.getHeight()/2,BufferedImage.TYPE_INT_RGB); Graphics g = scaled.getGraphics(); g.drawImage(fg,0,0,scaled.getWidth(),scaled.getHeight(),null); g.dispose(); final int xMax = bg.getWidth()-scaled.getWidth(); final int yMax = bg.getHeight()-scaled.getHeight(); final JLabel label = new JLabel(new ImageIcon(bg)); ActionListener listener = new ActionListener() { Random random = new Random(); public void actionPerformed(ActionEvent ae) { Graphics g = bg.getGraphics(); int x = random.nextInt(xMax); int y = random.nextInt(yMax); g.drawImage( scaled, x, y, null ); g.dispose(); label.repaint(); } }; Timer timer = new Timer(1200, listener); timer.start(); JOptionPane.showMessageDialog(null, label); } public static void main(String[] args) throws Exception { URL url1 = new URL("http://i.stack.imgur.com/lxthA.jpg"); final BufferedImage image1 = ImageIO.read(url1); URL url2 = new URL("http://i.stack.imgur.com/OVOg3.jpg"); final BufferedImage image2 = ImageIO.read(url2); //Create the frame on the event dispatching thread SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { new ImageOnImage(image2, image1); } }); } }