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

  • SEARCH
  • Home
  • 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 7591243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:36:13+00:00 2026-05-30T20:36:13+00:00

I would like display different pictures in a JFrame (in a random order). I

  • 0

I would like display different pictures in a JFrame (in a random order). I am using Threads in the application as the display will need to be constantly updated.

   public class CarsMain extends JFrame implements Runnable {
       Random rand = new Random();

    //the main thread
    Thread thread;
    BufferedImage backbuffer;
    Cars cars;
    Car1 car1;
    //set map of the cars array
    private int Width = 10;
    private int Height = 100;
    int[][] map=new int[Width][Height];

    public static void main(String[] args) {
        new CarsMain();
    }    

    public CarsMain() {
        super("Cars");
        setSize(500,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        thread = new Thread(this);
        thread.start();

    }



    //thread run event
    public void run() {

        Thread current = Thread.currentThread();

        while (current == thread) {
            try { Thread.sleep(0); }
            catch(InterruptedException e) { e.printStackTrace(); }


           genmap();
        }
    }

       //Fills map[][] with random numbers (id's)
    private void genmap() {

        for (int i = 0; i<10;i++){      
            for (int j = 0; j<20; j++){     
        map[i][j] = rand.nextInt(5);

                }
            }

    }


    //JFrame paint event
    public void paint(Graphics g) {

        g.drawImage(backbuffer, 0, 0, this);
        for (int i = 0;i<600;i+=128){
            for(int j=30; j<500;j+=128){
                          //this is the part i am stuck on
                                // v this is just put here to test that display
                g.drawImage(cars.Car1.getpic(),i, j, this);
            }
        }
    }
}

and

    public class Cars{

    public  Image pic;
    public int carid;
    public Cars carList[]= new Cars[10];




    public Cars(int i){

        carid = i;

    }

    public Image getpic(){
        return pic;

    }

    public void setpic(Image pic){
        this.pic = pic;
    }

    public int getCarid(int i){
        return Carid;
    }

    public static final Cars car0 =  new car0(1);
    public static final Cars Car1 = new car1(2);
    public static final Cars Car2  = new car2(3);
    public static final Cars Car3 = new car3(4);
    public static final Cars Car4 = new car4(5);

}

(each of the car# classes extends the Cars class so i can override things later)
What i Tried to do was create a multidimensional array (genmap()) which was filled with random numbers (which works fine(also is there a way i can only run this once in the thread?)), these random numbers would correlate to the car id. Then display thee corresponding car picture to the screen using the ID, this is where I am stuck.

I have tried various ways but it usually turns out that ‘pic’ needs to be static (which it can’t be, otherwise it would only show car4’s image)

I know i could use if statements, but would really like to use the id directly, so it is easy to add new cars later on(just make the new object in the Cars class), does anybody have any idea?

  • 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-05-30T20:36:14+00:00Added an answer on May 30, 2026 at 8:36 pm

    I’m not going to go through your code in detail – the general approach should be:

    • Use a javax.swing.Timer to update the picture periodically
    • use Random.nextInt(n+1) to choose a picture from a set of n pictures
    • keep the pictures (or just their File or URL) in a List or array
      • load the chosen image from the File/URL if needed
    • tell the GUI to repaint
    • the GUI should get the current image and paint it
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to display different tooltip content for each of the areas defined
I am using the MVCContrib grid and I would like to display images depending
I would like to display textures in different colors pretty much having this texture.
I would like to display an image but have the file name different than
I would like to display a different controller when the iPhone is rotated. But
I would like to use Tomcat's error-page directive to display various different error pages
I would like to display a different view than the standard keyboard (a picker
I would like to display Yes/No in different languages according to some boolean variable.
I have a message inbox. I would like to display each message in different
Im writing an app that I would like to display different views based on

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.