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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:05:58+00:00 2026-06-01T00:05:58+00:00

I’m working on a Java version of MS Paint. You can see what it

  • 0

I’m working on a Java version of MS Paint. You can see what it looks like so far here. (images are far too tall and many to embed in a question)

It uses a JScrollPane to move a subclass of Canvas around. If you don’t resize the Window, it operates just fine. If you make the window smaller, at first glance it appears to work just the same.

However, if you scroll around, it becomes apparent that the application is rendering the same “viewport”, just moved. If you keep scrolling, it becomes more obvious that it overlaps everything else.

So basically, it’s rendering the wrong viewport. Resizing the window updates the viewport to be correct. If you try to draw on a grey area, it draws it just fine, you just can’t see it.

I’ve tried doing repaint() on the canvas any time the scrollbars are moved. It didn’t change anything.

What should I do to fix this?

This is the code for the frame: (argument img is the image it will paint on. You have to do setVisible(true) yourself as well)

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;


public class JPaint extends JFrame {
    JPaintPanel panel;
    public JPaint(BufferedImage img) {
        super("Edit your image");
        panel = new JPaintPanel(img);
        setContentPane(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Trial and error got me these numbers.
        //I have no idea how find the proper size...
        setSize(img.getWidth()+20-1, img.getHeight()+50+50+2);
    }
    //go to the panel you hold, ask it to retrieve it from the canvas
    public BufferedImage grabImage() {
        return panel.grabImage();
    }
}


class JPaintPanel extends JPanel {
    JTools toolbar;
    JCanvas canvas;
    JScrollPane scrollPane;
    public JPaintPanel(BufferedImage img) {
        super(new BorderLayout());
        toolbar = new JTools();
        canvas = new JCanvas(img);
        JScrollPane scrollPane = new JScrollPane(canvas);
        scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                canvas.repaint();
            }

        });
        scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                canvas.repaint();
            }

        });
        setPreferredSize(new Dimension(img.getWidth(),img.getHeight()+50));
        add(toolbar, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
    }
    public BufferedImage grabImage() {
        return canvas.getImage();
    }
}
class JTools extends JPanel {
    JSlider scale;

    public JTools() {
        scale= new JSlider(JSlider.HORIZONTAL,
                                    0, 400, 100);
        scale.setMajorTickSpacing(100);
        scale.setPaintTicks(true);
        scale.setPaintLabels(true);
        scale.setPreferredSize(new Dimension(300,50));
        add(scale);
    }

}
class JCanvas extends Canvas {

    BufferedImage im;
    Graphics2D g2d;
    Point old = new Point();
    Point now = new Point();

    public JCanvas(BufferedImage imIn) {
        im = imIn;
        g2d = im.createGraphics();


        setPreferredSize(new Dimension(im.getWidth(), im.getHeight()));


        setColor(Color.WHITE);
        setWidth(4);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        //g2d.drawRect(5, 5, 40, 20);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                //System.out.println(e.getPoint());
                //g2d.fillRect(e.getX()-5, e.getY()-5, 10, 10);
                old=e.getPoint();
                now=e.getPoint();
                g2d.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
                repaint();
            }
        });
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                //System.out.println(e.getPoint());
                old=e.getPoint();
                now=e.getPoint();
                g2d.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
                repaint();
            }
        });
        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                //System.out.println(e.getPoint());
                //g2d.fillRect(e.getX()-5, e.getY()-5, 10, 10);
                old=now;
                now=e.getPoint();
                g2d.drawLine((int)old.getX(), (int)old.getY(), (int)now.getX(), (int)now.getY());
                repaint();
            }
        });
    }
    public void paint(Graphics g) {
        //super.paint(g);
        update(g);
    }
    public void update(Graphics g) {
        //super.update(g);
        g.drawImage(im, 0, 0, null);
        getToolkit().sync();
    }
    public void setWidth(float w) {
        g2d.setStroke(new BasicStroke(w, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    }
    public void setColor(Color c) {
        g2d.setColor(c);
    }
    public BufferedImage getImage() {
        return im;
    }
}
  • 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-01T00:05:59+00:00Added an answer on June 1, 2026 at 12:05 am
    class JCanvas extends Canvas { 
    

    Don’t mix Swing with AWT components without good cause. Extend a JComponent instead.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
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 would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.