JDK-4816931 : JPanel: Clipping does not work when height is negative.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2003-02-11
  • Updated: 2003-02-12
  • Resolved: 2003-02-12
Related Reports
Duplicate :  
Description

Name: jk109818			Date: 02/11/2003


FULL PRODUCT VERSION :
java version "1.4.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_02-b02)
Java HotSpot(TM) Client VM (build 1.4.0_02-b02, mixed mode

FULL OPERATING SYSTEM VERSION :
Redhat linux 7.1  2.4.7-10

ADDITIONAL OPERATING SYSTEMS :
Redhat linux 8.0  2.4.18

A DESCRIPTION OF THE PROBLEM :
The Clipping does not work when in custom component that
is extended from JPanel.

When setting the clip inside paintComponent(Graphics g)
with g.clipRect() the result can be a negative height.

In linux g.fillRect() is not getting clipped; however
g.drawString() is clipped.

In windows nothing gets drawn, which I assume is the proper
behavior.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. In linux
2. Start the demo application
2. Resize the height of the InternalFrame from the buttom

EXPECTED VERSUS ACTUAL BEHAVIOR :
Notice the garbage appearing below the frame.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.Shape;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Dimension;

import java.awt.event.*;

class MyPanel extends JPanel
{
   public MyPanel()
   {
      super();
      setSize(400,400);
      setPreferredSize(new Dimension(400, 400));
   }
   
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Shape baseClip = g.getClip();

      for(int i=0;i<10;i++)
      {
         g.setClip(baseClip);
         g.clipRect(0, i*40, 400, (i+1)*40);

         // with jre 1.4.1 on linux the rectangle gets drawn when the height is
negative
         System.out.println("New Clip is: "+ g.getClip());

         g.setColor(new Color(100,250-i*20,i*20));
         g.fillRect(0, i*40, 400, (i+1)*40);
      }
   }


}


class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;

    public MyInternalFrame() {
        super("Document #" + (++openFrameCount), true, true, true, true);

        //...Create the GUI and put it in the window...
        JPanel center = (JPanel) getContentPane();
        center.add(new JScrollPane(new MyPanel()));

        //...Then set the window size or call pack...
        setSize(300,300);
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
}



public class InternalFrameDemo extends JFrame
{
   JDesktopPane desktop;

   public InternalFrameDemo()
   {
      super("InternalFrameDemo");

      setBounds(50, 50, 500, 500);

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

      desktop = new JDesktopPane();
      createFrame();
      
      setContentPane(desktop);
   }

   protected void createFrame()
   {
      MyInternalFrame frame = new MyInternalFrame();
      frame.setVisible(true);
      desktop.add(frame);
   }

   public static void main(String[] args)
   {
      InternalFrameDemo frame = new InternalFrameDemo();
      frame.setVisible(true);
   }
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Check minimal height, if the height is negative abort the
paintComponent() procedure. This will make the
paintComponent() very heavy.
(Review ID: 180052) 
======================================================================

Comments
EVALUATION This looks like overflow error on the clipping equations. A lot of work was done on overflow errors on clipping rectangles in the 1.4.2 release. Since this bug is no longer reproducible on the 1.4.2 release, I am closing this bug as a duplicate of the bugs which fixed clip rect overflows. ###@###.### 2003-02-11
11-02-2003