Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JScrollPane is erasing the Graphics Image but JPanel doesn't

Posted on 1999-07-28
4
Medium Priority
?
1,023 Views
Last Modified: 2012-06-27
When I draw a line inside a JPanel (which is
itself is contained within a JScrollPane), the
line is drawn then quickly painted over!!

However, when I eliminate the use of JScrollPane
and simply use JPanel, the problem doesn't happen.
Based on the DebugGraphics class output (included
at the bottom), someone is repainting the background
over the entire JFrame.  Very odd!!!!!!
{Graphics(12-1) Filling rect: java.awt.Rectangle[x=0,y=0,width=189,height=170]}


Another very related question worth 10 points:
If I move the window (i.e., JFrame) to the edge
of my display screen the image appears. Then, when
I drag or move another unrelated window over a portion
of the Graphics Image, the repainting of the image
is considerably flickering  even though I didn't
turn off double buffering.
This flickering does not happen when I only use
JPanel.


-------- CapacityScrollPane.java ------------
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;

public class CapacityScrollPane extends JScrollPane
{
    private JPanel mainPanel = new JPanel();

    public CapacityScrollPane()
    {
        this.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION);

        // Add the main panel to the scroll pane.
        this.getViewport().add(mainPanel);
    }

/*
    // Only applicable for JPanel.
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setColor(Color.magenta);
        g.fillArc(100,100,150,140,30,300);

    } // public void paintComponent
*/

    // Only applicable for JScrollPane.
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics j = mainPanel.getGraphics();

        j.setColor(Color.magenta);
        j.fillArc(100,100,50,50,30,180);
        j.setColor(Color.black);
        j.drawArc(100,100,50,50,30,90);

        j.dispose();

    } // public void paintComponent


    public static void main(String args[])
    {
        final JFrame f = new JFrame();
        f.getContentPane().add(new CapacityScrollPane());

        f.setBounds(400,400,200,200);
        f.setTitle("Capacity");
        f.setVisible(true);

        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        f.addWindowListener(new WindowAdapter()
                            {
                                public void windowClosed(WindowEvent e)
                                {
                                    System.exit(0);
                                }});
    }
}
---------------------------------------------


............................
... many lines deleted .....
............................
Graphics(1-1) Drawing line: from (191, 1) to (191, 172)
Graphics(1-1) Drawing line: from (1, 172) to (191, 172)
Graphics(1-1) Setting color: javax.swing.plaf.ColorUIResource[r=204,g=204,b=204]
Graphics(1-1) Drawing line: from (190, 2) to (190, 2)
Graphics(1-1) Drawing line: from (1, 171) to (1, 171)
Graphics(1-1) Translating by: java.awt.Point[x=0,y=0]
Graphics(7-1) Setting color: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
Graphics(7-1) Setting font: javax.swing.plaf.FontUIResource[family=dialog,name=Dialog,style=plain,size=12]
Graphics(10-1) Setting color: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
Graphics(10-1) Setting font: javax.swing.plaf.FontUIResource[family=dialog,name=Dialog,style=plain,size=12]
Graphics(12-1) Setting color: javax.swing.plaf.ColorUIResource[r=204,g=204,b=204]
Graphics(12-1) Filling rect: java.awt.Rectangle[x=0,y=0,width=189,height=170]

0
Comment
Question by:bustany
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:mwibbels
ID: 1247299
The problem with your code is that you try to draw on the JPanel from the JScrollPanes's paintComponent. Instead you should redefine the paintComponent of the JPanel.
The reason that the drawing is painted over is that order of drawing components in your program is:

JScrollPane.paintComponent()   <- here you draw in the JPanel
JScrollPane.paintChildren()
  calls JPanel.paintComponent() <- here the default behaviour of the JPanel is to fill its space with the background color, thus erasing your drawing

Redefine your code as:



import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;

public class CapacityScrollPane extends JScrollPane
{
      private JPanel mainPanel = new JPanel() {

                           // ovverride paintComponent of the JPanel
            public void paintComponent(Graphics g)
            {
                  super.paintComponent(g);

                  g.setColor(Color.magenta);
                  g.fillArc(100,100,150,140,30,300);

            } // public void paintComponent

      };

      public CapacityScrollPane()
      {
            this.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION);

            // Add the main panel to the scroll pane.
            this.getViewport().add(mainPanel);
      }

      public static void main(String args[])
      {
            final JFrame f = new JFrame();
            f.getContentPane().add(new CapacityScrollPane());

            f.setBounds(400,400,200,200);
            f.setTitle("Capacity");
            f.setVisible(true);

            f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            f.addWindowListener(new WindowAdapter()
                                          {
                                                public void windowClosed(WindowEvent e)
                                                {
                                                      System.exit(0);
                                                }});
      }
}

0
 

Author Comment

by:bustany
ID: 1247300
Thank you, mwibbels.  Your answer did fix
the problem and made it clearer.

However, I'm still very confused how the
image did get painted and remained on the screen
when the JFrame is moved to the edge of the
screen (i.e., beyond the edge) or when another
window overlapped on top of a portion of the
image.  In both of these cases, shouldn't the
image never have remained since it should have
been painted over the the JPanel's paintComponent?

Again, thank you.

Mohammed
0
 
LVL 2

Accepted Solution

by:
mwibbels earned 320 total points
ID: 1247301
The reason is that the  JScrollPane and JPanel only redraws the part that was erased by an overlapping window, whilst 'your' paintComponent method  redraws its Arc completely. The part of the arc that is drawn outside of the area that needs redrawing remains on the screen, the part that is inside the redraw area does get overwritten.
0
 

Author Comment

by:bustany
ID: 1247302
I increased the points by 30 since he answered part two
of the question
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question