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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:04:36+00:00 2026-06-17T15:04:36+00:00

I’m trying to move the boat on the x-axis (without the keyboard yet). How

  • 0

I’m trying to move the boat on the x-axis (without the keyboard yet). How can I relate the movement/ animation to the boat.png and not to any other image?

public class Mama extends Applet implements Runnable {

    int width, height;
    int x = 200;
    int y = 200;
    int dx = 1;
    Image img1, img2, img3, img4;

    @Override
    public void init(){
        setSize(627, 373);
        Thread t = new Thread(this);
        img1 = getImage(getCodeBase(),"Background.png");
        img2 = getImage(getCodeBase(), "boat.png");
        img3 = getImage(getCodeBase(), "LeftPalm.png");
        img4 = getImage(getCodeBase(), "RightPalm.png");

    }

    @Override
    public void paint(Graphics g){
        g.drawImage(img1, 0, 0, this);
        g.drawImage(img2, 200, 200, this);
        g.drawImage(img3, 40, 100, this);
        g.drawImage(img4, 450, 130, this);
    }

    @Override
    public void run() {
        while(true){

            x += dx;
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                System.out.println("Thread generates an error.");
            }
        }
    }
} 
  • 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-17T15:04:37+00:00Added an answer on June 17, 2026 at 3:04 pm

    There are a few things that stand out…

    The “Problem”

    As has already been stated, you need to supply variable arguments to the image drawing process. g.drawImage(img2, x, y, this);, this will allow you define where the image should be painted.

    While you’ve implemented Runnable, you’ve actually not started any threads to call it. This means, nothing is actually changing the variables.

    In you start method, you should be calling something like new Thread(this).start().

    Recommendations

    Although you’ve tagged the question as Swing, you’re using AWT components. This isn’t recommended (in fact applets are generally discouraged as they are troublesome – IMHO). The other problem, as you are bound to find out shortly, is they are not double buffered, this generally leads to flickering when performing animation, which isn’t desirable.

    As a side note, it is also discouraged to override the paint method of top level containers like Applet. Top level containers tend to contain a number additional components, by overriding the paint method like this, you destroy this setup. Also, top level containers don’t tend to be double buffered either.

    The example below uses a JFrame, but it wouldn’t take much to convert it to use a JApplet (just drop the AnimationPanel on to it. This is another reason why extending from top level containers is generally discouraged 😉

    enter image description here

    public class AnimatedBoat {
    
        public static void main(String[] args) {
            new AnimatedBoat();
        }
    
        public AnimatedBoat() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new AnimationPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class AnimationPane extends JPanel {
    
            private BufferedImage boat;
            private int xPos = 0;
            private int direction = 1;
    
            public AnimationPane() {
                try {
                    boat = ImageIO.read(new File("boat.png"));
                    Timer timer = new Timer(40, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            xPos += direction;
                            if (xPos + boat.getWidth() > getWidth()) {
                                xPos = getWidth() - boat.getWidth();
                                direction *= -1;
                            } else if (xPos < 0) {
                                xPos = 0;
                                direction *= -1;
                            }
                            repaint();
                        }
    
                    });
                    timer.setRepeats(true);
                    timer.setCoalesce(true);
                    timer.start();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return boat == null ? super.getPreferredSize() : new Dimension(boat.getWidth() * 4, boat.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                int y = getHeight() - boat.getHeight();
                g.drawImage(boat, xPos, y, this);
    
            }
    
        }
    
    }
    
    • 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 am trying to understand how to use SyndicationItem to display feed which is
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 find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.