Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/ScrollPane/bug8077409Test.java
47490 views
/*1* Copyright (c) 2015, 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*/2223/* @test24@bug 807740925@summary Drawing deviates when validate() is invoked on java.awt.ScrollPane26@author [email protected]27@run main bug8077409Test28*/293031import java.awt.*;32import java.awt.event.*;3334public class bug8077409Test extends Frame {35ScrollPane pane;36MyCanvas myCanvas;3738class MyCanvas extends Canvas {39public Dimension getPreferredSize() {40return new Dimension(400, 800);41}4243public void paint(Graphics g) {44g.setColor(Color.BLACK);45g.drawLine(0, 0, 399, 0);46g.setColor(Color.RED);47g.drawLine(0, 1, 399, 1);48g.setColor(Color.BLUE);49g.drawLine(0, 2, 399, 2);50g.setColor(Color.GREEN);51g.drawLine(0, 3, 399, 3);52}5354}5556public bug8077409Test() {57super();58setLayout(new BorderLayout());59pane = new ScrollPane();6061myCanvas = new MyCanvas();62pane.add(myCanvas);6364add(pane, BorderLayout.CENTER);65setSize(320, 480);6667}6869@Override70protected void processKeyEvent(KeyEvent e) {71super.processKeyEvent(e);7273}7475public static void main(String[] args) throws AWTException, InterruptedException {76final bug8077409Test obj = new bug8077409Test();77obj.setVisible(true);78Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {79@Override80public void eventDispatched(AWTEvent e) {81KeyEvent keyEvent = (KeyEvent) e;82if(keyEvent.getID() == KeyEvent.KEY_RELEASED) {83if (keyEvent.getKeyCode() == KeyEvent.VK_1) {84System.out.println(obj.pane.toString());85System.out.println("obj.myCanvas.pos: " + obj.myCanvas.getBounds());86System.out.println(obj.myCanvas.toString());87} else if (keyEvent.getKeyCode() == KeyEvent.VK_2) {88obj.repaint();89} else if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {90Point scrollPosition = obj.pane.getScrollPosition();91scrollPosition.translate(0, 1);92obj.pane.setScrollPosition(scrollPosition);93} else if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {94Point scrollPosition = obj.pane.getScrollPosition();95scrollPosition.translate(0, -1);96obj.pane.setScrollPosition(scrollPosition);97} else if (keyEvent.getKeyCode() == KeyEvent.VK_SPACE) {98obj.pane.validate();99}100}101}102}, AWTEvent.KEY_EVENT_MASK);103Point scrollPosition = obj.pane.getScrollPosition();104scrollPosition.translate(0, 1);105obj.pane.setScrollPosition(scrollPosition);106107int y = obj.pane.getComponent(0).getLocation().y;108obj.pane.validate();109if(y != obj.pane.getComponent(0).getLocation().y){110throw new RuntimeException("Wrong position of component in ScrollPane");111}112}113114}115116