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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:21:15+00:00 2026-06-03T13:21:15+00:00

I am trying to create a JFrame that displays an image from a file

  • 0

I am trying to create a JFrame that displays an image from a file path onto a particular position on the JFrame. At a later time (when a button is clicked), I want the image to move positions, say, 50 pixles to the left. If a layout manager is necessary, I want to use the null layout, as this is a project for myself and I am not quite ready to learn how to write my own layout manager.

So far, I have managed to display a BufferedImage in a frame, but I do not know how to specify its position.

Is using a BufferedImage even the correct approach? What is the best way to go about doing this?

Update: I tried to follow your suggestion of using mouselistener and it resulted in this:

class ImgComponent extends JComponent  implements ChangeListener, MouseListener  {

MovableImage mi;

public ImgComponent(MovableImage mi) {
    this.mi = mi;
    mi.addListener(this);
      mi.addListener1(this);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(mi.i, mi.getX(), mi.getY(), null);
}

@Override
public void stateChanged(ChangeEvent e) {
    repaint();

}
@Override
public void mouseClicked(MouseEvent e) {
        mi.setPos(100, 100);
        System.out.println("yay");
}

}

But unfortinely, the mouseClicked event never triggers. I just want that damn image to move, lol.

  • 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-03T13:21:17+00:00Added an answer on June 3, 2026 at 1:21 pm

    Here’s a complete example that uses the model/view/controller pattern. (Just dump all snippets after each other in a single .java file.)

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.util.*;
    import java.util.List;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    
    // A class encapsulating an image and a x-coordinate (a "model")
    class MovableImage {
        Image i = new ImageIcon("duke.png").getImage();
        private int x = 0;
    
        // Observers that are interested in movements.
        List<ChangeListener> listeners = new ArrayList<ChangeListener>();
    
        public void addListener(ChangeListener cl) {
            listeners.add(cl);
        }
    
        public int getX() {
            return x;
        }
    
        public void incrementX() {
            x += 10;
    
            // Notify those interested.
            for (ChangeListener cl : listeners)
                cl.stateChanged(null);
        }
    }
    

     

    // A graphical component displaying the model.
    // Object of this class are interested in movement because when the image moves,
    // this component needs to be repainted.
    class ImgComponent extends JComponent implements ChangeListener {
    
        // The movable image to present.
        MovableImage mi;
    
        public ImgComponent(MovableImage mi) {
            this.mi = mi;
            mi.addListener(this);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(mi.i, mi.getX(), 10, null);
        }
    
        // This method is called from MovableImage when the position changes.
        @Override
        public void stateChanged(ChangeEvent e) {
            repaint();
        }
    }
    

     

    // Main class.
    public class FrameTestBase extends JFrame {
    
        public static void main(String args[]) {
    
            // Create the "model".
            final MovableImage mi = new MovableImage();
    
            FrameTestBase t = new FrameTestBase();
            t.setLayout(new BorderLayout());
    
            // Add a component presenting the model.
            t.add(new ImgComponent(mi), BorderLayout.CENTER);
    
            // Create a button which increments x when clicked on.
            t.add(new JButton(new AbstractAction("Move right") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    mi.incrementX();
                }
            }), BorderLayout.SOUTH);
    
            // Show it.
            t.setDefaultCloseOperation(EXIT_ON_CLOSE);
            t.setSize(400, 400);
            t.setVisible(true);
        }
    }
    

    enter image description here


    Regarding your edit:

    You need to add the mouse listener as well. In the constructor:

    public ImgComponent(MovableImage mi) {
        this.mi = mi;
        mi.addListener(this);
        mi.addListener1(this);
    }
    

    add the following line at the bottom:

        addMouseListener(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a (child) JFrame which slides out from underneath one
I'm trying to create a button that changes color of the background and then
Basically, I am trying to create an app, that displays images. filename variable is
I'm trying to create a JPanel that displays webpages. I've gotten to the stage
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a background-image slideshow and am getting this error... This is the
For homework, I'm trying to create a CustomButton that has a frame and in
Two questions. First question is I'm trying to create a simple form that when
I am trying to create a program that allows you to create your own
I am trying to create a GUI that allows someone to set up an

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.