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