6

我有一个关于随机城市交通网络模拟的 Java 项目,我设法找到了实现这个项目的方法,所以我将每个交叉路口分成一个基本上是扩展 JPanel 类(名为 Carrefour)的部分。 .一切都很好,直到我陷入如何绘制车辆并让它们通过道路为止。

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

4

2 回答 2

15

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

在此处输入图像描述

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);
            }
        });
    }
}
于 2012-04-07T14:31:07.987 回答
9

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

请注意,这个问题在这里和其他地方已经被问了很多次,正如您所期望的那样,您可以在这里通过一些搜索找到很多此类事情的示例。

编辑
你问:

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

同样,您将为此创建一个 BufferedImage,可能使用ImageIO.read(...). 然后,您将在 JPanel 的paintComponent(Graphics g)方法覆盖中使用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();
         }
      });
   }
}
于 2012-04-07T13:48:05.550 回答