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

HTML text overlap when painting view on label and scaling

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_21"
      Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
      Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.0.6002]

      A DESCRIPTION OF THE PROBLEM :
      When painting HTML text in a label via a View created from a JEditorPane, text overlap may happen if the text contains style attributes such as underlined text.

      Overlap happens if the graphic context on which the text is painted is scaled.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the test case, click on the "Zoom In" button.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Spacing around underlined words is kept intact.
      ACTUAL -
      Underlined words overlap neighboring text.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package test;

      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.*;
      import javax.swing.text.View;

      public class HtmlViewPaintOverlap implements ActionListener {
      private static final String HTML_TEXT = "<html><p><span style=\" font-family: Monospaced;\">This is some test to see if the</span> <u><span style=\" font-family: Monospaced;\">underlined</span></u> <span style=\" font-family: Monospaced;\">text is displayed</span> <span style=\" text-decoration: underline;\"><span style=\" font-family: Monospaced;\">correctly</span></span> <span style=\" font-family: Monospaced;\">in reports.</span></p>";
      private static final String ZOOM_IN = "ZOOM_IN";
      private static final String ZOOM_OUT = "ZOOM_OUT";

      private int zoomFactor = 1;
      private JLabel label;
      private JFrame frame;
      private Dimension preferredSize = new Dimension(600, 300);

      /**
      * Create the GUI and show it. For thread safety, this method should be
      * invoked from the event-dispatching thread.
      */
      private void createAndShowGUI() {
      this.frame = new JFrame("FrameDemo");
      this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JEditorPane editorPane = new JEditorPane("text/html", HTML_TEXT);
      final View view = editorPane.getUI().getRootView(editorPane);

      this.label = new JLabel() {
      @Override
      public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D) g.create();
      g2d.scale(HtmlViewPaintOverlap.this.zoomFactor,
      HtmlViewPaintOverlap.this.zoomFactor);
      Dimension size = getSize();
      view.paint(g2d, new Rectangle(0, 0, size.width, size.height));
      g2d.dispose();
      }
      };
      this.label.setPreferredSize(this.preferredSize);

      JScrollPane scrollPane = new JScrollPane(this.label);
      this.frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

      JButton zoomInButton = new JButton("Zoom In");
      zoomInButton.addActionListener(this);
      zoomInButton.setActionCommand(ZOOM_IN);
      this.frame.getContentPane().add(zoomInButton, BorderLayout.NORTH);

      JButton zoomOutButton = new JButton("Zoom Out");
      zoomOutButton.addActionListener(this);
      zoomOutButton.setActionCommand(ZOOM_OUT);
      this.frame.getContentPane().add(zoomOutButton, BorderLayout.SOUTH);

      // Display the window.
      this.frame.pack();
      this.frame.setVisible(true);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
      if (ZOOM_IN.equals(e.getActionCommand())) {
      this.zoomFactor += 1;
      }
      else if (ZOOM_OUT.equals(e.getActionCommand())) {
      this.zoomFactor -= 1;
      if (this.zoomFactor < 1) {
      this.zoomFactor = 1;
      }
      }
      this.label.revalidate();
      this.label.repaint();
      this.label.setPreferredSize(new Dimension(this.preferredSize.width
      * this.zoomFactor, this.preferredSize.height * this.zoomFactor));
      }

      public static void main(String[] args) {
      // Schedule a job for the event-dispatching thread:
      // creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      new HtmlViewPaintOverlap().createAndShowGUI();
      }
      });
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None found yet.

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: