Path: blob/master/test/jdk/java/awt/JAWT/MyMacCanvas.java
41149 views
/*1* Copyright (c) 2021, 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*/2223import java.awt.*;24import java.awt.event.*;25import javax.swing.*;2627public class MyMacCanvas extends Canvas {2829static {30try {31System.loadLibrary("mylib");32} catch (Throwable t) {33System.out.println("Test failed!!");34t.printStackTrace();35System.exit(1);36}37}3839public void addNotify() {40super.addNotify();41addNativeCoreAnimationLayer();42}4344public native void addNativeCoreAnimationLayer();4546static JAWTFrame f;47public static void main(String[] args) {48try {49Robot robot = new Robot();50EventQueue.invokeLater(new Runnable() {51public void run() {52f = new JAWTFrame("JAWTExample");53f.setBackground(Color.white);54f.setLayout(new BorderLayout(10, 20));55f.setLocation(50, 50);56((JComponent) f.getContentPane()).setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.cyan));57f.addNotify();58f.pack();59f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);60f.setVisible(true);61}62});63robot.delay(5000);64Color col1 = new Color(0, 0, 0);65Color col2 = robot.getPixelColor(f.getX()+50, f.getY()+50);66if (col1.equals(col2)) {67System.out.println("Test passed!");68} else {69System.out.println("col1 " + col1 + " col2 " + col2);70throw new RuntimeException("Color of JAWT canvas is wrong or " +71"it was not rendered. " + "Check that other windows " +72"do not block the test frame.");73}74System.exit(0);75} catch (Throwable t) {76System.out.println("Test failed!");77t.printStackTrace();78System.exit(1);79}80}8182private static class JAWTFrame extends JFrame {83public JAWTFrame(final String title) {84super(title);85}8687public void addNotify() {88super.addNotify(); // ensures native component hierarchy is setup8990final Component layerBackedCanvas = new MyMacCanvas();91layerBackedCanvas.setPreferredSize(new Dimension(400, 200));92add(layerBackedCanvas, BorderLayout.CENTER);9394invalidate();95}96}97}9899100