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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:10:17+00:00 2026-06-14T12:10:17+00:00

I’m trying to get an image to paint on the screen using java’s Graphics2D.

  • 0

I’m trying to get an image to paint on the screen using java’s Graphics2D. Here is the code I’m using. I want to see an image move steadily across the screen. At the moment I can see the image but it does not move unless I resize the window, in which case it DOES move. I have sketched out the classes below.

 public class Tester extends JFrame {

    private static final long serialVersionUID = -3179467003801103750L;
    private Component myComponent;
    public static final int ONE_SECOND = 1000;
    public static final int FRAMES_PER_SECOND = 20;

    private Timer myTimer;

    public Tester (Component component, String title) {
        super(title);
        myComponent = component;
    }

    public void start () {

        myTimer = new Timer(ONE_SECOND / FRAMES_PER_SECOND, new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e) {
                repaint();
            }
        });
        myTimer.start();
    }

    @Override
    public void paint (Graphics pen) {
        if (myComponent != null) {
            myComponent.paint(pen);
        }
    }

}

The Component object passed to Tester is the following class:

public class LevelBoard extends Canvas implements ISavable {

    private static final long serialVersionUID = -3528519211577278934L;

    @Override
    public void paint (Graphics pen) {
        for (Sprite s : mySprites) {
            s.paint((Graphics2D) pen);
        }
    }

    protected void add (Sprite sprite) {
        mySprites.add(sprite);
    }

I have ensured that this class has only one sprite that I have added. The sprite class is roughly as follows:

public class Sprite {

    private Image myImage;
    private int myX, myY;

    public Sprite () {
        URL path = getClass().getResource("/images/Bowser.png");
        ImageIcon img = new ImageIcon(path);
        myImage = img.getImage();

    }

    public void update () {
        myX += 5;
        myY += 5;
    }

    public void paint (Graphics2D pen) {
        update();
        pen.drawImage(myImage, myX, myY,null);
    }

However, I see only a stationary image of bowser on the screen. He does not move unless the window is resized. I know that the paint(Graphics2D pen) method in the Sprite class is being called at particular intervals (because of the Timer in the Tester class). However, even though the x and y positions are being incremented by 5 each time. The sprite does not move. Why not? How do I fix it? I’m just trying to test some other features of my program at the moment so I really just need to get this up and running. I don’t really care how.

  • 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-14T12:10:18+00:00Added an answer on June 14, 2026 at 12:10 pm

    Your code is full of problems:

    1. Don’t override JFrame.paint(), especially if not calling super. Set a ContentPane and override its paintComponent(). While it may seem convenient, it is usually a bad design and unnecessary.
    2. Don’t override JComponent.paint(), but rather override JComponent.paintComponent() (and call super)
    3. Use a JLabel to display an image. It’s much simpler.
    4. Don’t mix AWT(Canvas) and Swing (JFrame). Stick to Swing.

    Here is a simple example showing a Bowser moving around the frame. (It’s funny when you reduce the frame size and hit the image with the frame border ;-))

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Random;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestAnimation2 {
        private static final int NB_OF_IMAGES_PER_SECOND = 50;
        private static final int WIDTH = 800;
        private static final int HEIGHT = 600;
        private Random random = new Random();
    
        private double dx;
        private double dy;
    
        private double x = WIDTH / 2;
        private double y = HEIGHT / 2;
    
        protected void initUI() throws MalformedURLException {
            final JFrame frame = new JFrame(TestAnimation2.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(null);
            final JLabel label = new JLabel(new ImageIcon(new URL("http://www.lemondedemario.fr/images/dossier/bowser/bowser.png")));
            label.setSize(label.getPreferredSize());
            frame.setMinimumSize(label.getPreferredSize());
            frame.add(label);
            frame.setSize(WIDTH, HEIGHT);
            dx = getNextSpeed();
            dy = getNextSpeed();
            Timer t = new Timer(1000 / NB_OF_IMAGES_PER_SECOND, new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    x += dx;
                    y += dy;
                    if (x + label.getWidth() > frame.getContentPane().getWidth()) {
                        x = frame.getContentPane().getWidth() - label.getWidth();
                        dx = -getNextSpeed();
                    } else if (x < 0) {
                        x = 0;
                        dx = getNextSpeed();
                    }
                    if (y + label.getHeight() > frame.getContentPane().getHeight()) {
                        y = frame.getContentPane().getHeight() - label.getHeight();
                        dy = -getNextSpeed();
                    } else if (y < 0) {
                        y = 0;
                        dy = getNextSpeed();
                    }
                    label.setLocation((int) x, (int) y);
    
                }
            });
            frame.setVisible(true);
            t.start();
        }
    
        private double getNextSpeed() {
            return 2 * Math.PI * (0.5 + random.nextDouble());
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        new TestAnimation2().initUI();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.