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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:49:08+00:00 2026-05-25T18:49:08+00:00

So, I am drawing a graphic in a JPanel object using Grahics2D . The

  • 0

So, I am drawing a graphic in a JPanel object using Grahics2D.
The JPanel is placed in a JScrollPane for the cases when my graphic is bigger than the window.
But after I draw my graphic the JPanel‘s size does not change and I cannot scroll to see the rest of my graphic, so I locate the lowest and most left points and set the size manually in the method that does the drawing (method is called drawTrack()).
When I switch windows or do something else that makes the JPanel to be drawn again, my graphic disappears, so I rewrote paint(), repaint(), validate(), invalidate() methods and in there I invoke the drawTrack() method to draw my graphic on every possible case of redrawing the JPanel.
The problem is that when the JPanel invokes one of the methods that do the redrawing I invoke drawTrack() in them, which after redrawing my graphic sets the size manually so that I can scroll the JScrollPane and see my whole graphic. But when I invoke setSize() method on the JPanel that makes it to be redrawn again, which means to invoke drawTrack() and so on and so on.
The scroll bars appear because the size is correct but it creates an infinite loop that redraws my graphic over and over. And if I don’t invoke the setSize() method my JPanel gets default size so it can fit in the JScrollPane‘s viewport and thus I cannot scroll it to see my graphic.
Any suggestions?

  • 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-25T18:49:09+00:00Added an answer on May 25, 2026 at 6:49 pm

    When you resize a JPanel you stimulate a repaint, and so if you change the size in a paint or paintComponent method or a method called in either of these methods, it makes sense to me that you are at risk of causing an infinite loop.

    Solution: Don’t resize anything in a paint or paintComponent method. That method is just for painting and nothing else. Instead if you want to set the size of your JPanel, override its getPreferredSize() method.

    Here’s a “colorful” example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ImageReSize extends JPanel {
       private static final int INIT_WIDTH = 400;
       private static final int MAX_WIDTH = 1200;
    
       private static final int TIMER_DELAY = 20;
       protected static final int WIDTH_STEP = 5;
       private int width = INIT_WIDTH;
       private int height = INIT_WIDTH;
       private boolean growing = true;
    
       public ImageReSize() {
          new Timer(TIMER_DELAY, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (growing && width < MAX_WIDTH) {
                   width += WIDTH_STEP;
                   height += WIDTH_STEP;
                } else {
                   growing = false;
                }
    
                if (!growing && width > INIT_WIDTH) {
                   width -= WIDTH_STEP;
                   height -= WIDTH_STEP;
                } else {
                   growing = true;
                }
                // get the parent container which is the scrollpane's viewport
                JComponent parent = (JComponent)getParent();
                parent.revalidate(); // let it relayout the changed size JPanel
                parent.repaint();  // and repaint all
             }
          }).start();
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          float x = (float)width / 10;
          float y = (float)height / 10;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          g2.setPaint(new GradientPaint(x, 0, Color.green, 0, y, Color.black, true));
          g2.fillRect(0, 0, getWidth(), getHeight());
          g2.setPaint(new GradientPaint(0, 0, Color.blue, x, y, Color.red, true));
          g2.fillOval(0, 0, width, height);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(width, height);
       }
    
       private static void createAndShowUI() {
          ImageReSize imageReSize = new ImageReSize();
          JFrame frame = new JFrame("ImageReSize");
          frame.getContentPane().add(new JScrollPane(imageReSize));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using the System.Drawing.Graphics.DrawString() method in .Net to draw text on top
I am drawing onto a JPanel using getGraphics and the drawLine and fillOval commands
I have a JPanel on which some drawing is performed using paintComponent method and
Could someone provide an example for drawing graphics without using Windows Forms? I have
I have a program that draws some vector graphics using System.Drawing and the Graphics
im trying to use the graphic veiw of QT to draw lines it's possible
How can I tell the paint method to draw background on JPanel only and
Does the rhomobile support drawing with 2-d(3-d) graphics or is the graphic capabilities limited
I am trying to build a raster drawing application using C# and so far
Does anyone know what's the best graphic drawing library for C++, I want a

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.