From ac3a07391c0887d3dd351d7712ba47cb029c7505 Mon Sep 17 00:00:00 2001 From: mrkam Date: Thu, 24 Mar 2011 18:54:41 +0100 Subject: 7027849: New demo for Shaped/Translucent windows feature needs to be created Reviewed-by: rupashka --- src/share/demo/jfc/TransparentRuler/README.txt | 14 ++ .../TransparentRuler/transparentruler/Ruler.java | 234 +++++++++++++++++++++ .../jfc/TransparentRuler/build.properties | 22 ++ .../demo/nbproject/jfc/TransparentRuler/build.xml | 91 ++++++++ .../TransparentRuler/nbproject/file-targets.xml | 46 ++++ .../jfc/TransparentRuler/nbproject/jdk.xml | 98 +++++++++ .../nbproject/netbeans-targets.xml | 81 +++++++ .../jfc/TransparentRuler/nbproject/project.xml | 173 +++++++++++++++ 8 files changed, 759 insertions(+) create mode 100644 src/share/demo/jfc/TransparentRuler/README.txt create mode 100644 src/share/demo/jfc/TransparentRuler/transparentruler/Ruler.java create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/build.properties create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/build.xml create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/nbproject/file-targets.xml create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/nbproject/jdk.xml create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/nbproject/netbeans-targets.xml create mode 100644 src/share/demo/nbproject/jfc/TransparentRuler/nbproject/project.xml diff --git a/src/share/demo/jfc/TransparentRuler/README.txt b/src/share/demo/jfc/TransparentRuler/README.txt new file mode 100644 index 0000000000..ddcf9de1d5 --- /dev/null +++ b/src/share/demo/jfc/TransparentRuler/README.txt @@ -0,0 +1,14 @@ + +To run the Ruler demo: + + java -jar Ruler.jar + +These instructions assume that this installation's version of the java +command is in your path. If it isn't, then you should either +specify the complete path to the java command or update your +PATH environment variable as described in the installation +instructions for the Java(TM) SE Development Kit. + +KNOWN ISSUES: +Context menu is clipped with the window shape. The issues are: +CR 7027486 JPopupMenu doesn't take window shape into account diff --git a/src/share/demo/jfc/TransparentRuler/transparentruler/Ruler.java b/src/share/demo/jfc/TransparentRuler/transparentruler/Ruler.java new file mode 100644 index 0000000000..707151f971 --- /dev/null +++ b/src/share/demo/jfc/TransparentRuler/transparentruler/Ruler.java @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package transparentruler; + + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GraphicsDevice; +import java.awt.GraphicsDevice.WindowTranslucency; +import java.awt.GraphicsEnvironment; +import java.awt.event.ActionEvent; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.geom.Path2D.Float; +import java.lang.reflect.InvocationTargetException; +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JFrame; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; + + +/** + * This sample demonstrates shaped and translucent window feature. + * @author Alexander Kouznetsov + */ +@SuppressWarnings("serial") +public class Ruler extends JFrame { + + private static final Color BACKGROUND = Color.RED; + private static final Color FOREGROUND = Color.WHITE; + private static final int OPACITY = 180; + private static final int W = 70; + private static final int F_HEIGHT = 400; + private static final int F_WIDTH = (int) (F_HEIGHT * 1.618 + 0.5); + + private static void checkTranslucencyMode(WindowTranslucency arg) { + GraphicsEnvironment ge = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice gd = ge.getDefaultScreenDevice(); + if (!gd.isWindowTranslucencySupported(arg)) { + System.err.println("'" + arg + + "' translucency mode isn't supported."); + System.exit(-1); + } + } + private final ComponentAdapter componentListener = new ComponentAdapter() { + + /** + * Applies the shape to window. It is recommended to apply shape in + * componentResized() method + */ + @Override + public void componentResized(ComponentEvent e) { + int h = getHeight(); + int w = getWidth(); + float a = (float) Math.hypot(h, w); + Float path = new java.awt.geom.Path2D.Float(); + path.moveTo(0, 0); + path.lineTo(w, 0); + path.lineTo(0, h); + path.closePath(); + path.moveTo(W, W); + path.lineTo(W, h - W * (a + h) / w); + path.lineTo(w - W * (a + w) / h, W); + path.closePath(); + setShape(path); + } + }; + private final Action exitAction = new AbstractAction("Exit") { + + { + putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X); + } + + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }; + private final JPopupMenu jPopupMenu = new JPopupMenu(); + + { + jPopupMenu.add(new JMenuItem(exitAction)); + } + /** + * Implements mouse-related behavior: window dragging and popup menu + * invocation + */ + private final MouseAdapter mouseListener = new MouseAdapter() { + + int x, y; + + @Override + public void mousePressed(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1) { + x = e.getX(); + y = e.getY(); + } + } + + @Override + public void mouseDragged(MouseEvent e) { + if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) { + setLocation(e.getXOnScreen() - x, e.getYOnScreen() - y); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) { + jPopupMenu.show(getContentPane(), e.getX(), e.getY()); + } + } + }; + /** + * Implements keyboard navigation. Arrows move by 5 pixels, Ctrl + arrows + * move by 50 pixels, Alt + arrows move by 1 pixel. + * Esc exits the application. + */ + private final KeyAdapter keyboardListener = new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + int step = e.isControlDown() ? 50 : e.isAltDown() ? 1 : 5; + switch (e.getKeyCode()) { + case KeyEvent.VK_LEFT: + setLocation(getX() - step, getY()); + break; + case KeyEvent.VK_RIGHT: + setLocation(getX() + step, getY()); + break; + case KeyEvent.VK_UP: + setLocation(getX(), getY() - step); + break; + case KeyEvent.VK_DOWN: + setLocation(getX(), getY() + step); + break; + case KeyEvent.VK_ESCAPE: + exitAction.actionPerformed(null); + } + } + }; + + public Ruler() { + setUndecorated(true); + + // Enables perpixel translucency + setBackground(new Color(BACKGROUND.getRed(), BACKGROUND.getGreen(), + BACKGROUND.getBlue(), OPACITY)); + + addMouseListener(mouseListener); + addMouseMotionListener(mouseListener); + addComponentListener(componentListener); + addKeyListener(keyboardListener); + setContentPane(new JPanel() { + + @Override + protected void paintComponent(Graphics g) { + Graphics gg = g.create(); + int w = getWidth(); + int h = getHeight(); + int hh = gg.getFontMetrics().getAscent(); + gg.setColor(FOREGROUND); + for (int x = 0; x < w * (h - 8) / h - 5; x += 5) { + boolean hi = x % 50 == 0; + gg.drawLine(x + 5, 0, x + 5, + hi ? 20 : (x % 25 == 0 ? 13 : 8)); + if (hi) { + String number = Integer.toString(x); + int ww = gg.getFontMetrics().stringWidth(number); + gg.drawString(number, x + 5 - ww / 2, 20 + hh); + } + } + gg.dispose(); + } + }); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(F_WIDTH, F_HEIGHT); + setLocationByPlatform(true); + } + + /** + * @param args the command line arguments are ignored + */ + public static void main(String[] args) throws InterruptedException, InvocationTargetException { + + SwingUtilities.invokeAndWait(new Runnable() { + + public void run() { + checkTranslucencyMode(WindowTranslucency.PERPIXEL_TRANSLUCENT); + checkTranslucencyMode(WindowTranslucency.PERPIXEL_TRANSPARENT); + + Ruler ruler = new Ruler(); + ruler.setVisible(true); + } + }); + } +} diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/build.properties b/src/share/demo/nbproject/jfc/TransparentRuler/build.properties new file mode 100644 index 0000000000..b1b2ef89b8 --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/build.properties @@ -0,0 +1,22 @@ +main.dir=${basedir}/../../../jfc/TransparentRuler + +src.dir=${main.dir}/src + +build.dir=build +classes.dir=${build.dir}/classes +jar=${main.dir}/TransparentRuler.jar +javadoc.dir=${build.dir}/javadoc + +build.sysclasspath=ignore +# E.g.: cp=lib/x.jar:lib/y.jar +cp= +extra.run.cp= + +main.class=transparentruler.Ruler + +run.cp=${cp}:${classes.dir}:${extra.run.cp} + +debug=true +deprecation=false + +nbjdk.home=${basedir}/../../../.. diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/build.xml b/src/share/demo/nbproject/jfc/TransparentRuler/build.xml new file mode 100644 index 0000000000..8db759a30c --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/build.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set property 'main.class' (e.g. in build.properties) + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/file-targets.xml b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/file-targets.xml new file mode 100644 index 0000000000..9d750841e3 --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/file-targets.xml @@ -0,0 +1,46 @@ + + + + + + + + + + Must set property 'includes' + + + + + + + diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/jdk.xml b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/jdk.xml new file mode 100644 index 0000000000..3690ef0e8f --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/jdk.xml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/netbeans-targets.xml b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/netbeans-targets.xml new file mode 100644 index 0000000000..84a0e9b3c5 --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/netbeans-targets.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/project.xml b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/project.xml new file mode 100644 index 0000000000..ecc07bc52b --- /dev/null +++ b/src/share/demo/nbproject/jfc/TransparentRuler/nbproject/project.xml @@ -0,0 +1,173 @@ + + + + + + org.netbeans.modules.ant.freeform + + + TransparentRuler + + user.build.properties + build.properties + ${nbjdk.home}/jre/lib/rt.jar + + + + + ${main.dir} + + + + java + ${src.dir} + + + ${build.dir} + + + + + jar + + + clean + + + clean + jar + + + run + + + + show-javadoc + + + + debug + + + + compile-selected + + includes + ${src.dir} + \.java$ + relative-path + + , + + + + + run + + main.class + ${src.dir} + \.java$ + java-name + + + + + + + + debug + + main.class + ${src.dir} + \.java$ + java-name + + + + + + + + debug-fix + + class + ${src.dir} + \.java$ + relative-path-noext + + + + + + + + jar + ${jar} + jar + clean + + + + + + ${src.dir} + + + ${main.dir}/README.txt + + + + + + + + + + + + + + + + + ${src.dir} + ${cp} + ${run.cp} + ${nbjdk.bootclasspath} + ${classes.dir} + ${jar} + ${javadoc.dir} + 1.5 + + + + -- cgit v1.2.3