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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:31:06+00:00 2026-05-16T02:31:06+00:00

I’m using drawLine() and friends to paint a graph onto a JPanel . There

  • 0

I’m using drawLine() and friends to paint a graph onto a JPanel. There are tens of thousands of points to graph, so it takes 3-5 seconds. I want to have a viewport, like the blue one illustrated below, over the graph.

graph http://cl.ly/1qvN/content

I would like this viewport to be updatable, as I have a MouseMotionListener on the JPanel that the graph is on. The problem is, if I redraw the viewport (the square) as it is now, (using drawRect()), I have to redraw the entire graph, which isn’t desirable.

I’ve read some about GlassPanes, which may be useful for solving this problem, but they evidently are a part of JFrames, and not JPanels. (I have this panel along with other components inside of a JFrame already.)

What would be a nice way to handle this, staying in Swing and being efficient?

For what it’s worth, I’m actually coding in Clojure, but that shouldn’t change anything here.

Thank you very much!

Isaac

  • 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-16T02:31:07+00:00Added an answer on May 16, 2026 at 2:31 am

    My approach to this sort of problem was to consider the volatility of the data. Unless the chart is ticking along in real time, the only thing that will change from paint to paint is the position of the blue box. One way to minimize the paint time is to not repeat work you have already performed. For example:

    protected void paintComponent(Graphics g) {
        // paint chart
        if (dirty || buffer == null) {
            buffer = new BufferedImage(getWidth(), getHeight(),
                                       BufferedImage.TYPE_ING_ARGB);
            // Paint the chart onto the buffered image
            dirty = false;
        }
        b.drawImage(0, 0, buffer);
        // Draw blue box
        ...
    }
    

    The very first call to paintComponent initializes the buffer and does the heavy lifting of painting the graph. Thereafter, the buffer is just painted. If you need to handle the case where the display is redrawn to show a different part of the data, you just need to invalidate the buffer.

    public void invalidateBuffer() {
        dirty = true;
        repaint();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.