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

RepaintManager shifts repainted region when the floating point UI scale is used

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 9
    • client-libs

      - Run the CustomSelectedComponent sample below which draws 4 letters and sets UI scale to floating point value 1.5
      - Click mouse on the second letter.
      The letter is repainted with different color.
      The resulted repainted letter is shifted to one pixel left.

      See the screenshots:
      custom-component.png - initial component
      custom-component-selected - the second letter is repainted and shifted to one pixel left

      ------------------
      import java.awt.Color;
      import java.awt.FlowLayout;
      import java.awt.Graphics;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.SwingUtilities;

      public class CustomSelectedComponent {

          private static final int W = 40;
          private static final int H = 60;
          private static float SCALE = 1.5f;

          static class CustomPanel extends JPanel {

              static final Color DRAW_COLOR = Color.PINK;
              static final Color REDRAW_COLOR = Color.MAGENTA;
              final int w;
              final int h;
              boolean redraw = false;

              public CustomPanel(int w, int h) {
                  super(new FlowLayout());
                  this.w = w;
                  this.h = h;

                  addMouseListener(new MouseAdapter() {

                      @Override
                      public void mousePressed(MouseEvent e) {
                          int x = e.getX();
                          int y = e.getY();
                          int xx = 0;
                          int yy = 0;
                          if (x < w) {
                              redraw = true;
                          } else if (x < 2 * w) {
                              redraw = true;
                              xx = w;
                              yy = 0;
                          } else {
                              redraw = false;
                          }

                          if (redraw) {
                              repaint(xx - 1, yy - 1, w + 2, h + 2);
                          } else {
                              repaint();
                          }
                      }
                  });
              }

              @Override
              public void paint(Graphics g) {
                  super.paint(g);
                  int w2 = 2 * w;
                  int h2 = 2 * h;

                  g.setColor(redraw ? REDRAW_COLOR : DRAW_COLOR);
                  g.drawLine(0, 0, w2, 0);
                  g.drawLine(0, 0, 0, h2);
                  g.drawLine(w2, 0, w2, h2);
                  g.drawLine(0, h2, w2, h2);
                  g.drawLine(w, 0, w, h2);
                  g.drawLine(0, h, w2, h);
                  paintLetter(g, 0, 0);
                  paintLetter(g, w, 0);
                  paintLetter(g, 0, h);
                  paintLetter(g, w, h);
              }

              private void paintLetter(Graphics g, int x, int y) {
                  int w_2 = w / 2;
                  int w_4 = w / 4;
                  int h_2 = h / 2;
                  g.drawLine(x + w_2, y, x, y + h);
                  g.drawLine(x + w_2, y, x + w, y + h);
                  g.drawLine(x + w_4, y + h_2, x + w_4 + w_2, y + h_2);
              }
          }

          public static void main(String[] args) {

              System.setProperty("sun.java2d.uiScale", Float.toString(SCALE));

              SwingUtilities.invokeLater(() -> {
                  JFrame frame = new JFrame();
                  frame.setSize(300, 300);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  JPanel panel = new CustomPanel(W, H);
                  frame.getContentPane().add(panel);
                  frame.setVisible(true);
              });
          }
      }
      ------------------

            alexsch Alexandr Scherbatiy
            alexsch Alexandr Scherbatiy
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: