提交 9927d4bc 编写于 作者: M mrkam

7130241: [macosx] TransparentRuler demo can not run due to lacking of perpixel transparency support

Reviewed-by: art
上级 e246f9d4
/* /*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -40,12 +40,9 @@ ...@@ -40,12 +40,9 @@
package transparentruler; package transparentruler;
import java.awt.Color; import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency; import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment; import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
...@@ -79,16 +76,32 @@ public class Ruler extends JFrame { ...@@ -79,16 +76,32 @@ public class Ruler extends JFrame {
private static final int F_HEIGHT = 400; private static final int F_HEIGHT = 400;
private static final int F_WIDTH = (int) (F_HEIGHT * 1.618 + 0.5); private static final int F_WIDTH = (int) (F_HEIGHT * 1.618 + 0.5);
private static void checkTranslucencyMode(WindowTranslucency arg) { private static boolean translucencySupported;
private static boolean transparencySupported;
private static boolean checkTranslucencyMode(WindowTranslucency arg) {
GraphicsEnvironment ge = GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsDevice gd = ge.getDefaultScreenDevice();
if (!gd.isWindowTranslucencySupported(arg)) { return gd.isWindowTranslucencySupported(arg);
System.err.println("'" + arg
+ "' translucency mode isn't supported.");
System.exit(-1);
}
} }
public Shape buildShape() {
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();
return path;
}
private final ComponentAdapter componentListener = new ComponentAdapter() { private final ComponentAdapter componentListener = new ComponentAdapter() {
/** /**
...@@ -97,36 +110,32 @@ public class Ruler extends JFrame { ...@@ -97,36 +110,32 @@ public class Ruler extends JFrame {
*/ */
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
int h = getHeight();
int w = getWidth(); // We do apply shape only if PERPIXEL_TRANSPARENT is supported
float a = (float) Math.hypot(h, w); if (transparencySupported) {
Float path = new java.awt.geom.Path2D.Float(); setShape(buildShape());
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") { private final Action exitAction = new AbstractAction("Exit") {
{ {
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X); putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X);
} }
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
System.exit(0); System.exit(0);
} }
}; };
private final JPopupMenu jPopupMenu = new JPopupMenu(); private final JPopupMenu jPopupMenu = new JPopupMenu();
{ {
jPopupMenu.add(new JMenuItem(exitAction)); jPopupMenu.add(new JMenuItem(exitAction));
} }
/** /**
* Implements mouse-related behavior: window dragging and popup menu * Implements mouse-related behavior: window dragging and popup menu
* invocation * invocation
...@@ -157,6 +166,7 @@ public class Ruler extends JFrame { ...@@ -157,6 +166,7 @@ public class Ruler extends JFrame {
} }
} }
}; };
/** /**
* Implements keyboard navigation. Arrows move by 5 pixels, Ctrl + arrows * Implements keyboard navigation. Arrows move by 5 pixels, Ctrl + arrows
* move by 50 pixels, Alt + arrows move by 1 pixel. * move by 50 pixels, Alt + arrows move by 1 pixel.
...@@ -201,10 +211,22 @@ public class Ruler extends JFrame { ...@@ -201,10 +211,22 @@ public class Ruler extends JFrame {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
Graphics gg = g.create(); Graphics2D gg = (Graphics2D) g.create();
int w = getWidth(); int w = getWidth();
int h = getHeight(); int h = getHeight();
int hh = gg.getFontMetrics().getAscent(); int hh = gg.getFontMetrics().getAscent();
// This is an approach to apply shape when PERPIXEL_TRANSPARENT
// isn't supported
if (!transparencySupported) {
gg.setBackground(new Color(0, 0, 0, 0));
gg.clearRect(0, 0, w, h);
gg.clip(buildShape());
gg.setBackground(Ruler.this.getBackground());
gg.clearRect(0, 0, w, h);
}
gg.setColor(FOREGROUND); gg.setColor(FOREGROUND);
for (int x = 0; x < w * (h - 8) / h - 5; x += 5) { for (int x = 0; x < w * (h - 8) / h - 5; x += 5) {
boolean hi = x % 50 == 0; boolean hi = x % 50 == 0;
...@@ -216,6 +238,7 @@ public class Ruler extends JFrame { ...@@ -216,6 +238,7 @@ public class Ruler extends JFrame {
gg.drawString(number, x + 5 - ww / 2, 20 + hh); gg.drawString(number, x + 5 - ww / 2, 20 + hh);
} }
} }
gg.dispose(); gg.dispose();
} }
}); });
...@@ -231,9 +254,17 @@ public class Ruler extends JFrame { ...@@ -231,9 +254,17 @@ public class Ruler extends JFrame {
SwingUtilities.invokeAndWait(new Runnable() { SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() { public void run() {
checkTranslucencyMode(WindowTranslucency.PERPIXEL_TRANSLUCENT); translucencySupported = checkTranslucencyMode(PERPIXEL_TRANSLUCENT);
checkTranslucencyMode(WindowTranslucency.PERPIXEL_TRANSPARENT); transparencySupported = checkTranslucencyMode(PERPIXEL_TRANSPARENT);
if (!translucencySupported) {
System.err.println("This application requires "
+ "'PERPIXEL_TRANSLUCENT' translucency mode to "
+ "be supported.");
System.exit(-1);
}
Ruler ruler = new Ruler(); Ruler ruler = new Ruler();
ruler.setVisible(true); ruler.setVisible(true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册