Path: blob/master/test/jdk/javax/swing/JTable/8133919/DrawGridLinesTest.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.awt.Color;23import java.awt.Graphics2D;24import java.awt.image.BufferedImage;25import javax.swing.JTable;26import javax.swing.SwingUtilities;27import javax.swing.table.AbstractTableModel;28import javax.swing.table.DefaultTableCellRenderer;29import javax.swing.table.TableModel;3031/*32* @test33* @bug 813391934* @summary [macosx] JTable grid lines are incorrectly positioned on HiDPI display35* @run main DrawGridLinesTest36*/37public class DrawGridLinesTest {3839private static final int WIDTH = 300;40private static final int HEIGHT = 150;41private static final Color GRID_COLOR = Color.BLACK;42private static final Color TABLE_BACKGROUND_COLOR = Color.BLUE;43private static final Color CELL_RENDERER_BACKGROUND_COLOR = Color.YELLOW;44private static final int SCALE = 2;4546public static void main(String[] args) throws Exception {47SwingUtilities.invokeAndWait(DrawGridLinesTest::checkTableGridLines);48}4950private static void checkTableGridLines() {5152TableModel dataModel = new AbstractTableModel() {53public int getColumnCount() {54return 10;55}5657public int getRowCount() {58return 10;59}6061public Object getValueAt(int row, int col) {62return " ";63}64};6566DefaultTableCellRenderer r = new DefaultTableCellRenderer();67r.setOpaque(true);68r.setBackground(CELL_RENDERER_BACKGROUND_COLOR);6970JTable table = new JTable(dataModel);71table.setSize(WIDTH, HEIGHT);72table.setDefaultRenderer(Object.class, r);73table.setGridColor(GRID_COLOR);74table.setShowGrid(true);75table.setShowHorizontalLines(true);76table.setShowVerticalLines(true);77table.setBackground(TABLE_BACKGROUND_COLOR);7879checkTableGridLines(table);80}8182private static void checkTableGridLines(JTable table) {8384int w = SCALE * WIDTH;85int h = SCALE * HEIGHT;8687BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);88Graphics2D g = img.createGraphics();89g.scale(SCALE, SCALE);90table.paint(g);91g.dispose();9293int size = Math.min(w, h);94int rgb = TABLE_BACKGROUND_COLOR.getRGB();9596for (int i = 0; i < size; i++) {97if (img.getRGB(i, i) == rgb || img.getRGB(i, size - i - 1) == rgb) {98throw new RuntimeException("Artifacts in the table background color!");99}100}101}102}103104105