Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:28:31+00:00 2026-06-01T18:28:31+00:00

I have a Java project that’s about traffic network simulation in a random city,

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:28:33+00:00Added an answer on June 1, 2026 at 6:28 pm

    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:

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

    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(...).

    For example…

    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();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java project that needs a addon interface. I was thinking about
I have a Java application that is managed using Maven. The project involves the
I have a Java project that has just about every class and package depending
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a Java project that I build using an Ant script. I am
Say I have a GUI Java project of something that simulates an ATM machine,
I have recently just created Java project using Eclipse that requires 2 JAR files
i have a java ee project which has a text file that uses a
I have got a project that is using jni to connect java wrapper and
I have a project in Java that I am stepping through and when I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.