Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4816931

JPanel: Clipping does not work when height is negative.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.1
    • client-libs
    • 2d
    • x86
    • linux



      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)
      ======================================================================

            flar Jim Graham
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: