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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:08:20+00:00 2026-06-14T06:08:20+00:00

So I’m creating a side scroller and I’m trying to draw an image at

  • 0

So I’m creating a side scroller and I’m trying to draw an image at the point of another image.

I have my background image which is 5000 x 500 and lets say I want to draw an image that’s 25×25 at 500, 50 of the background image. How would I do that?

So far I’ve tried:

Coins c = new Coins(img.getWidth(this) - 4500, img.getHeight(this) - 250);

But this just draws it at 500, 50 of the frame so it “moves” across the image as I scroll to the right. I want, after scroll once to the right, the coin image to draw at 500,50 of the image still so 495, 50 of the frame.

I could also use getGraphics of the background image and draw the smaller image onto it, but I think because I set the point it’s being draw at during the creation of the object, I can’t do that.

The character doesn’t move, besides up and down, just the background scrolls so I want the coin to move as the background does.

PICTURES:
(can’t embed it because i don’t have 10 rep points)
http://dl.dropbox.com/u/47632315/char.png http://dl.dropbox.com/u/47632315/coin.png http://dl.dropbox.com/u/47632315/sideScrollerBG.png

And the SSCCE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.awt.image.ImageObserver;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SideScroller extends JPanel implements KeyListener {

    public static void main(String args[]) throws Exception {
        SideScroller f = new SideScroller("Side Scroller");
    }

    JFrame f = new JFrame();
    int x = 0;
    int y = 0;
    int k = 10;
    int j = 50;
    int characterY = 351;
    boolean canJump = true;
    boolean keyPressed[] = new boolean[3];

    Image img;
    Image character;

    Coins c = new Coins(x + 500, y + 350);

    public SideScroller(String s) throws MalformedURLException, IOException {
        img = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/sideScrollerBG.png"));
        character = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/char.png"));

        f.setTitle(s);
        // don't use 'magic numbers' when descriptive constants are defined!
        //f.setDefaultCloseOperation(3);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        // Better to ask when loaded.
        //setPreferredSize(new Dimension(1000, 500));
        setPreferredSize(new Dimension(1000, img.getHeight(this)));
        f.add(this);
        f.pack();
        // usually harmful, rarely necessary.
//      f.setLayout(null);
        // Looks like a splash screen
        //f.setLocationRelativeTo(null);
        // located by platform default, best.
        f.setLocationByPlatform(true);
        f.addKeyListener(this);
        getKeys();

        // should be done last
        f.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, x, y, this);
        g.drawImage(character, 50, characterY, this);
        c.paint(g, this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_ESCAPE) {
            System.exit(0);
        }
        if (keyCode == KeyEvent.VK_RIGHT) {
            keyPressed[0] = true;
        }
        if (keyCode == KeyEvent.VK_LEFT) {
            keyPressed[1] = true;
        }
        if (canJump) {
            if (keyCode == KeyEvent.VK_SPACE) {
                // better to use a javax.swing.Timer here
                new Thread(new Runnable() {
                    public void run() {
                        canJump = false;
                        for (double i = 0; i <= 5.1; i += .2) {
                            characterY = (int) (k * i * i - j * i + 351);
                            f.repaint();
                            System.out.println(characterY);
                            System.out.println(canJump);
                            try {
                                Thread.sleep(25);
                            } catch (InterruptedException e) {
                                System.err.println(e);
                            }
                        }
                        repaint();
                        canJump = true;
                    }
                }).start();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            keyPressed[0] = false;
        }
        if (keyCode == KeyEvent.VK_LEFT) {
            keyPressed[1] = false;
        }
    }

    public void getKeys() {
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    if (keyPressed[0]) {
                        x -= 5;
                        repaint();
                    }
                    if (keyPressed[1]) {
                        if (x != 0) {
                            x += 5;
                            repaint();
                        }
                    }
                    try {
                        Thread.sleep(25);
                    } catch (InterruptedException e) {
                        System.err.println(e);
                    }
                }
            }
        }).start();
    }
}

class Coins extends pickUpObject {

    Image coin; 

    public Coins(int x, int y) throws MalformedURLException, IOException{
        super(x, y);
        super.x = x;
        super.y = y;
        coin = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/coin.png"));
    }

    public void paint(Graphics g, ImageObserver o){
        g.drawImage(coin, x, y, o);
    }
}

abstract class pickUpObject {

    int x, y;

    public pickUpObject(int x, int y){
    }

    public abstract void paint(Graphics g, ImageObserver o);
}
  • 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-14T06:08:21+00:00Added an answer on June 14, 2026 at 6:08 am

    One way is to stamp the Graphics out to a Graphics2D then translate it.

        // here we 'translate' the graphics 2D object to a separate location  
        g2d.translate(x,0);
    

    Screenhsot

    In this image, the character has moved to the right and is jumping. The coin has shifted left according to the distance the character has moved right.

    enter image description here

    Full code

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.image.ImageObserver;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class SideScroller extends JPanel implements KeyListener {
    
        public static void main(String args[]) throws Exception {
            SideScroller f = new SideScroller("Side Scroller");
        }
    
        JFrame f = new JFrame();
        int x = 0;
        int y = 0;
        int k = 10;
        int j = 50;
        int characterY = 351;
        boolean canJump = true;
        boolean keyPressed[] = new boolean[3];
    
        Image img;
        Image character;
    
        Coins c = new Coins(x + 500, y + 350);
    
        public SideScroller(String s) throws MalformedURLException, IOException {
            img = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/sideScrollerBG.png"));
            character = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/char.png"));
    
            f.setTitle(s);
            // don't use 'magic numbers' when descriptive constants are defined!
            //f.setDefaultCloseOperation(3);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setResizable(false);
            // Better to ask when loaded.
            //setPreferredSize(new Dimension(1000, 500));
            setPreferredSize(new Dimension(1000, img.getHeight(this)));
            f.add(this);
            f.pack();
            // usually harmful, rarely necessary.
    //      f.setLayout(null);
            // Looks like a splash screen
            //f.setLocationRelativeTo(null);
            // located by platform default, best.
            f.setLocationByPlatform(true);
            f.addKeyListener(this);
            getKeys();
    
            // should be done last
            f.setVisible(true);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, x, y, this);
            g.drawImage(character, 50, characterY, this);
            Graphics2D g2d = (Graphics2D)g;
            // here we 'translate' the graphics object to a separate location  
            g2d.translate(x,0);
            c.paint(g2d, this);
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
            if (keyCode == KeyEvent.VK_RIGHT) {
                keyPressed[0] = true;
            }
            if (keyCode == KeyEvent.VK_LEFT) {
                keyPressed[1] = true;
            }
            if (canJump) {
                if (keyCode == KeyEvent.VK_SPACE) {
                    // better to use a javax.swing.Timer here
                    new Thread(new Runnable() {
                        public void run() {
                            canJump = false;
                            for (double i = 0; i <= 5.1; i += .2) {
                                characterY = (int) (k * i * i - j * i + 351);
                                f.repaint();
                                System.out.println(characterY);
                                System.out.println(canJump);
                                try {
                                    Thread.sleep(25);
                                } catch (InterruptedException e) {
                                    System.err.println(e);
                                }
                            }
                            repaint();
                            canJump = true;
                        }
                    }).start();
                }
            }
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_RIGHT) {
                keyPressed[0] = false;
            }
            if (keyCode == KeyEvent.VK_LEFT) {
                keyPressed[1] = false;
            }
        }
    
        public void getKeys() {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        if (keyPressed[0]) {
                            x -= 5;
                            repaint();
                        }
                        if (keyPressed[1]) {
                            if (x != 0) {
                                x += 5;
                                repaint();
                            }
                        }
                        try {
                            Thread.sleep(25);
                        } catch (InterruptedException e) {
                            System.err.println(e);
                        }
                    }
                }
            }).start();
        }
    }
    
    class Coins extends pickUpObject {
    
        Image coin; 
    
        public Coins(int x, int y) throws MalformedURLException, IOException{
            super(x, y);
            super.x = x;
            super.y = y;
            coin = ImageIO.read(new URL("http://dl.dropbox.com/u/47632315/coin.png"));
        }
    
        public void paint(Graphics g, ImageObserver o){
            g.drawImage(coin, x, y, o);
        }
    }
    
    abstract class pickUpObject {
    
        int x, y;
    
        public pickUpObject(int x, int y){
        }
    
        public abstract void paint(Graphics g, ImageObserver o);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.