jdk/test/javax/swing/JTable/8133919/DrawGridLinesTest.java
changeset 43091 a03e216c92ea
parent 43076 a2d90fcf0a70
equal deleted inserted replaced
43090:65e2b10e8e80 43091:a03e216c92ea
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 import java.awt.Color;
       
    24 import java.awt.Graphics2D;
       
    25 import java.awt.image.BufferedImage;
       
    26 import javax.swing.JTable;
       
    27 import javax.swing.SwingUtilities;
       
    28 import javax.swing.table.AbstractTableModel;
       
    29 import javax.swing.table.DefaultTableCellRenderer;
       
    30 import javax.swing.table.TableModel;
       
    31 
       
    32 /*
       
    33  * @test
       
    34  * @bug 8133919
       
    35  * @summary [macosx] JTable grid lines are incorrectly positioned on HiDPI display
       
    36  * @run main DrawGridLinesTest
       
    37  */
       
    38 public class DrawGridLinesTest {
       
    39 
       
    40     private static final int WIDTH = 300;
       
    41     private static final int HEIGHT = 150;
       
    42     private static final Color GRID_COLOR = Color.BLACK;
       
    43     private static final Color TABLE_BACKGROUND_COLOR = Color.BLUE;
       
    44     private static final Color CELL_RENDERER_BACKGROUND_COLOR = Color.YELLOW;
       
    45     private static final int SCALE = 2;
       
    46 
       
    47     public static void main(String[] args) throws Exception {
       
    48         SwingUtilities.invokeAndWait(DrawGridLinesTest::checkTableGridLines);
       
    49     }
       
    50 
       
    51     private static void checkTableGridLines() {
       
    52 
       
    53         TableModel dataModel = new AbstractTableModel() {
       
    54             public int getColumnCount() {
       
    55                 return 10;
       
    56             }
       
    57 
       
    58             public int getRowCount() {
       
    59                 return 10;
       
    60             }
       
    61 
       
    62             public Object getValueAt(int row, int col) {
       
    63                 return " ";
       
    64             }
       
    65         };
       
    66 
       
    67         DefaultTableCellRenderer r = new DefaultTableCellRenderer();
       
    68         r.setOpaque(true);
       
    69         r.setBackground(CELL_RENDERER_BACKGROUND_COLOR);
       
    70 
       
    71         JTable table = new JTable(dataModel);
       
    72         table.setSize(WIDTH, HEIGHT);
       
    73         table.setDefaultRenderer(Object.class, r);
       
    74         table.setGridColor(GRID_COLOR);
       
    75         table.setShowGrid(true);
       
    76         table.setShowHorizontalLines(true);
       
    77         table.setShowVerticalLines(true);
       
    78         table.setBackground(TABLE_BACKGROUND_COLOR);
       
    79 
       
    80         checkTableGridLines(table);
       
    81     }
       
    82 
       
    83     private static void checkTableGridLines(JTable table) {
       
    84 
       
    85         int w = SCALE * WIDTH;
       
    86         int h = SCALE * HEIGHT;
       
    87 
       
    88         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
       
    89         Graphics2D g = img.createGraphics();
       
    90         g.scale(SCALE, SCALE);
       
    91         table.paint(g);
       
    92         g.dispose();
       
    93 
       
    94         int size = Math.min(w, h);
       
    95         int rgb = TABLE_BACKGROUND_COLOR.getRGB();
       
    96 
       
    97         for (int i = 0; i < size; i++) {
       
    98             if (img.getRGB(i, i) == rgb || img.getRGB(i, size - i - 1) == rgb) {
       
    99                 throw new RuntimeException("Artifacts in the table background color!");
       
   100             }
       
   101         }
       
   102     }
       
   103 }