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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:42:49+00:00 2026-06-11T09:42:49+00:00

I’m making a program that has an image that you scroll around on, and

  • 0

I’m making a program that has an image that you scroll around on, and I can’t figure out how to update the image if a button is pressed (For example: Adds a Green Ellipse to the image.) It already draws the image into the JScrollPane and you can scroll around, but when you click a button it doesn’t refresh the image. (more details in code)
Here is the code:

public class PegMaster extends JPanel implements ActionListener {

    //Note: not complete code
    public PegBox[] pegbox = new PegBox[9];

    public static Dimension size = new Dimension(520, 500);

    public BufferedImage canvas;
    public Graphics2D g2d;
    public JScrollPane scroller;
    JPanel panel;
    private Canvas window;

    JScrollPane pictureScrollPane;

    public PegMaster() {
        JButton button = new JButton("test");
        button.addActionListener(this);
        add(button);

        canvas = new BufferedImage((int)size.getWidth()-30, 75 * GUESSES, BufferedImage.TYPE_INT_RGB);
        g2d = canvas.createGraphics();
        for(int i = 0;i<=pegbox.length-1;i++) {
           pegbox[i] = new PegBox(i, g2d);
        }
        window = new Canvas(new ImageIcon(toImage(canvas)), 1);
        //Class Canvas is a Scrollable JLabel to draw to (the image)
        pictureScrollPane = new JScrollPane(window);
        pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10, (int)size.getHeight()-20));
        pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
        add(pictureScrollPane);

        //adds the scrollpane, but can't update the image in it
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
                //just adds the scrollpane
            }
        });
    }

    public void paint(Graphics g) {
        super.paint(g);

        for(int i = 0;i<=pegbox.length-1;i++) {
            //pegbox[i] = new PegBox(i);
            pegbox[i].draw(g2d);
        }
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   
        //tried re-making the scrollpane, didn't work.
        //window = new Canvas(new ImageIcon(toImage(canvas)), 1);
        //pictureScrollPane = new JScrollPane(window);
        //pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10 (int)size.getHeight()-20));
        //pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
        //tried imageupdate: pictureScrollPane.imageUpdate(canvas, 0, 0, 0 (int)size.getWidth()-10, (int)size.getHeight()-20);
        //remove(pictureScrollPane);
        //tried this: pictureScrollPane.revalidate();
        repaint();
    }
}
  • 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-11T09:42:50+00:00Added an answer on June 11, 2026 at 9:42 am

    Try this class which displays an Image. This can be added to a JScrollPane

    public class ImagePanel extends JPanel {
    
        public Image img;
    
        public ImagePanel(Image img){
            this.img = img;
        }
    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img, 0, 0, this);
        }
    
    }
    

    Now add this class to the JScrollPane. To update it, just change the image reference and call the repaint() method on the component

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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 want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.