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 6612603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:07:05+00:00 2026-05-25T20:07:05+00:00

In Java, I have successfully displayed an image on the screen. Now I want

  • 0

In Java, I have successfully displayed an image on the screen. Now I want to make it move by running it through a for loop. The for loop runs 10 times and sleeps for 1 second each time. Instead of moving the image every second as expected, I have to wait 10 seconds, then 10 images show up.

Here’s my code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class ImageDraw extends JComponent {
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Image img1 = Toolkit.getDefaultToolkit().getImage("player.png");

        int x = 0;
        int y = 0;

        for(int i = 0;i<10;i++){
            try {
                Thread.sleep(1000);
                x+=10;
                y+=10;
                g2.drawImage(img1, x, y, this);
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.exit(0);
            }   
        }   //end for

    }   //end paint

}   //end class

How would I make it so the image looks as if it’s moving everytime it runs through the loop?

  • 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-25T20:07:06+00:00Added an answer on May 25, 2026 at 8:07 pm

    Use a Timer for this, and get rid of the Thread.sleep(). You’re running this code on the UI thread, and when you call Thread.sleep() you’re putting the UI thread to sleep, which means no updating until the entire loop is complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully done this before but now after running the program numerous times
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
I have successfully implemented this from android to a java httpservlet on google app
I am currently working on a Java EE project. I have successfully developed a
I have successfully installed Eclipse 3.5 and Java 1.5. I would like to install
I have successfully created an object loader in java that loads in vertices, indices,
I want to create an OpenOffice.org plug-in that adds a sidebar. I have successfully
trying to learn windows programming in java, want to display a image to a
I have successfully implemented a restful service that works with my own client. Now,
Hi I have one xml file successfully parsed my before. now i am change

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.