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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:25:11+00:00 2026-05-26T09:25:11+00:00

I have a Java application which draws a drawing. I want to give the

  • 0

I have a Java application which draws a drawing. I want to give the user the possibility to mark an area with the mouse (in order to, for example, zoom into it).
For that I use the MouseMotionListener class, and when the mouse is (clicked and then) moved, I save the location of the currently selected (it isn’t final since the user haven’t released the mouse) rectangle, and use the repaint() function. I wish to display that rectangle over the original drawing, making it similar to the Selection tool in MSPaint.

The problem is that when I call the repaint() function, the method paintComponent (Graphics page) is invoked, in which I use the method super.paintComponent(page) which erases my drawing. However, if I don’t use that method when I know the user is selecting a rectangle, I get that all the selected rectangles are “packed” one above the other, and this is an undesirable result – I wish to display the currently selected rectangle only.

I thought I should be able to save a copy of the Graphics page of the drawing and somehow restore it every time the user moves the mouse, but I could not find any documentation for helpful methods.

Thank you very much,

Ron.

Edit: Here are the relevant pieces of my code:

public class DrawingPanel extends JPanel
{
public FractalPanel()
   {
      addMouseListener (new MyListener());
      addMouseMotionListener (new MyListener());

      setBackground (Color.black);
      setPreferredSize (new Dimension(200,200));
      setFocusable(true);
   }

public void paintComponent (Graphics page)
   {
        super.paintComponent(page);
        //that's where the drawing takes place: page.setColor(Color.red), page.drawOval(..) etc
   }
   private class MyListener implements MouseListener, MouseMotionListener
   {
   ...
      public void mouseDragged (MouseEvent event) 
      {
          //saving the location of the rectangle
          isHoldingRectangle = true;
          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-05-26T09:25:11+00:00Added an answer on May 26, 2026 at 9:25 am

    I’m betting that you are getting your Graphics object via a getGraphics() call on a component, and are disatisfied since this obtains a Graphics object which does not persist. It is for this reason that you shouldn’t do this but instead just do your drawing inside of the JPanel’s paintComponent. If you do this all will be happy.

    As an aside — we’ll be able to help you better if you tell us more of the pertinent details of your problem such as how you’re getting your Graphics object and how you’re trying to draw with it, key issues here. Otherwise we’re limited to taking wild guesses about what you’re trying to do.

    e.g.,

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class MandelDraw extends JPanel {
    private static final String IMAGE_ADDR = "http://upload.wikimedia.org/" +
            "wikipedia/commons/thumb/b/b3/Mandel_zoom_07_satellite.jpg/" +
            "800px-Mandel_zoom_07_satellite.jpg";
    private static final Color DRAWING_RECT_COLOR = new Color(200, 200, 255);
    private static final Color DRAWN_RECT_COLOR = Color.blue;
    
       private BufferedImage image;
       private Rectangle rect = null;
       private boolean drawing = false;
    
       public MandelDraw() {
          try {
             image = ImageIO.read(new URL(IMAGE_ADDR));
             MyMouseAdapter mouseAdapter = new MyMouseAdapter();
             addMouseListener(mouseAdapter);
             addMouseMotionListener(mouseAdapter);
          } catch (MalformedURLException e) {
             e.printStackTrace();
             System.exit(-1);
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(-1);
          }
       }
    
       @Override
       public Dimension getPreferredSize() {
          if (image != null) {
             return new Dimension(image.getWidth(), image.getHeight());
          }
          return super.getPreferredSize();
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          if (image != null) {
             g.drawImage(image, 0, 0, null);
          }
          if (rect == null) {
             return;
          } else if (drawing) {
             g2.setColor(DRAWING_RECT_COLOR);
             g2.draw(rect);
          } else {
             g2.setColor(DRAWN_RECT_COLOR);
             g2.draw(rect);
          }
       }
    
       private class MyMouseAdapter extends MouseAdapter {
          private Point mousePress = null; 
          @Override
          public void mousePressed(MouseEvent e) {
             mousePress = e.getPoint();
          }
    
          @Override
          public void mouseDragged(MouseEvent e) {
             drawing = true;
             int x = Math.min(mousePress.x, e.getPoint().x);
             int y = Math.min(mousePress.y, e.getPoint().y);
             int width = Math.abs(mousePress.x - e.getPoint().x);
             int height = Math.abs(mousePress.y - e.getPoint().y);
    
             rect = new Rectangle(x, y, width, height);
             repaint();
          }
    
          @Override
          public void mouseReleased(MouseEvent e) {
             drawing = false;
             repaint();
          }
    
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("MandelDraw");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new MandelDraw());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java application which I want to shutdown 'nicely' when the user
I have a Java application in which user can give any executable file (.exe)
I have a Java WebStart application for which I want to specify that the
i have a java applet application in which i use rich text area .
I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have a java application which use morphia to work with mongodb. I want
I have a Java application which queries a database table which the current user
I have a Java application which is used to start a Swing user interface.
I have a Java application which requires certain software (one of them being Perl)
I have a Java Application which has to load an DLL with a few

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.