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

The Archive Base Latest Questions

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

I got some interesting ideas and criticism from this , this and this post

  • 0

I got some interesting ideas and criticism from this, this and this post (see last post for the code of the GUI in question). Nevertheless, I’m still quite confused about some things. Mainly, what is the least expensive way of displaying user-introduces graphics?

More specifically, I used a paintComponent() method from JPanel class by making an object of this class in the MouseDragged() method together with paintComponent(getGraphics()) method (AuxClass2 and AuxClass1 accordingly).

Apparently, using getGraphics() and paintComponent() instead of repaint() are bad ideas, I suspect something to do with memory use. Also calling the AuxClass2 every time the user drags the mouse is also a bad idea.

Also JPanel vs Canvas (i.e. swing vs awt) is a bit confusing. What is used and when?

I’ve been trying to find a workarounds, but have not found one, especially for the getGraphics() method: how else can the graphics be added to the panel?

  • 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:05:27+00:00Added an answer on June 11, 2026 at 9:05 am

    I’ve been trying to find a workarounds, but have not found one,
    especially for the getGraphics() method: how else can the graphics be
    added to the panel?

    You remember what needs to be painted as a variable and use that in paintComponent().
    For example, what you seemed to be trying to do in your other question would look like:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PaintRectangle extends JPanel {
    
        private Point mouseLocation;
    
        public PaintRectangle() {
            setPreferredSize(new Dimension(500, 500));
    
            MouseAdapter listener = new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    updateMouseRectangle(e);
                }
    
                private void updateMouseRectangle(MouseEvent e) {
                    mouseLocation = e.getPoint();
                    repaint();
                }
    
                @Override
                public void mouseDragged(MouseEvent e) {
                    updateMouseRectangle(e);
                }
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    mouseLocation = null;
                    repaint();
                }
            };
            addMouseListener(listener);
            addMouseMotionListener(listener);
        }
    
        private Rectangle getRectangle() {
            if(mouseLocation != null) {
                return new Rectangle(mouseLocation.x - 5, mouseLocation.y - 5, 10, 10);
            }
            else {
                return null;
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rectangle = getRectangle();
            if(rectangle != null) {
                Graphics2D gg = (Graphics2D) g;
                gg.setColor(Color.BLUE);
                gg.fill(rectangle);
                gg.setColor(Color.BLACK);
                gg.draw(rectangle);
            }
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(new PaintRectangle());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    See also http://docs.oracle.com/javase/tutorial/uiswing/painting/

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

Sidebar

Related Questions

(I asked this question in another way , and got some interesting responses but
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
I recently asked this question https://softwareengineering.stackexchange.com/questions/129076/go-instead-of-c-c-with-cgo and got some very interesting input. However there's
I got some quite strange errors compiling code under gcc. It tells me that
i got some legacy code using: nonblocking socket, select for timeout, read (2) and
I got an interesting time-travel problem today, using the following code: for (int i
Hey everybody, got an interesting question I think. I've got a Silverlight3 application which
I just got some crazy ideas for analyzing the Twitter social graph (i.e., representing
I've got some YouTube embedding code (I will paste only code which is causing
I finally got some parts working here: http://jsfiddle.net/trXBr/5/ but when I put the code

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.