use of java.awt.Window in project jna by java-native-access.
the class AlphaMaskDemo method updateX11.
private void updateX11(boolean a, boolean i) {
X11 x11 = X11.INSTANCE;
X11.Window win = X11.Window.None;
Display dpy = x11.XOpenDisplay(null);
try {
if (!alphaWindow.isDisplayable()) {
alphaWindow.pack();
if (System.getProperty("java.version").matches("^1\\.4\\..*"))
alphaWindow.setVisible(true);
win = new X11.Window((int) Native.getWindowID(alphaWindow));
Window parent = alphaWindow.getOwner();
Point where = parent.getLocationOnScreen();
where.translate(parent.getWidth(), 0);
alphaWindow.removeAll();
alphaWindow.setLocation(where);
alphaWindow.setBackground(new Color(0, 0, 0, 0));
} else {
win = new X11.Window((int) Native.getWindowID(alphaWindow));
}
if (i) {
int w = image.getWidth(null);
int h = image.getHeight(null);
alphaWindow.setSize(w, h);
if (buffer == null || buffer.size() != w * h * 4) {
buffer = new com.sun.jna.Memory(w * h * 4);
pixels = new int[w * h];
}
BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = buf.getGraphics();
g.drawImage(image, 0, 0, w, h, null);
long start = System.currentTimeMillis();
long blitTime, putImageTime, write;
GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
long gcTime = System.currentTimeMillis();
try {
Raster raster = buf.getData();
int[] pixel = new int[4];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
raster.getPixel(x, y, pixel);
int alpha = (pixel[3] & 0xFF) << 24;
int red = (pixel[2] & 0xFF);
int green = (pixel[1] & 0xFF) << 8;
int blue = (pixel[0] & 0xFF) << 16;
pixels[y * w + x] = alpha | red | green | blue;
}
}
blitTime = System.currentTimeMillis();
X11.XWindowAttributes xwa = new X11.XWindowAttributes();
x11.XGetWindowAttributes(dpy, win, xwa);
X11.XImage image = x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap, 0, buffer, w, h, 32, w * 4);
buffer.write(0, pixels, 0, pixels.length);
write = System.currentTimeMillis();
x11.XPutImage(dpy, win, gc, image, 0, 0, 0, 0, w, h);
x11.XFree(image.getPointer());
putImageTime = System.currentTimeMillis();
} finally {
if (gc != null)
x11.XFreeGC(dpy, gc);
}
long end = System.currentTimeMillis();
//System.out.println("gc: " + (gcTime-start) + "ms");
//System.out.println("blit: " + (blitTime-gcTime) + "ms");
//System.out.println("write: " + (write-blitTime) + "ms");
//System.out.println("put image: " + (putImageTime-write) + "ms");
//System.out.println("total: " + (end-start) + "ms");
}
} finally {
if (dpy != null)
x11.XCloseDisplay(dpy);
}
if (a)
WindowUtils.setWindowAlpha(alphaWindow, alpha);
if (!alphaWindow.isVisible()) {
alphaWindow.setVisible(true);
// hack for initial refresh (X11)
update(true, true);
}
}
use of java.awt.Window in project jna by java-native-access.
the class AlphaMaskDemo2 method run.
public void run() {
// Must find a graphics configuration with a depth of 32 bits
GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
frame = new JFrame("Alpha Mask Demo");
alphaWindow = new JWindow(frame, gconfig);
icon = new JLabel();
icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
alphaWindow.getContentPane().add(icon);
JButton quit = new JButton("Quit");
JLabel label = new JLabel("Drag this window by its image");
label.setHorizontalAlignment(SwingConstants.CENTER);
alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
MouseInputAdapter handler = new MouseInputAdapter() {
private Point offset;
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e))
offset = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
offset = null;
}
public void mouseDragged(MouseEvent e) {
if (offset != null) {
Window w = (Window) e.getSource();
Point where = e.getPoint();
where.translate(-offset.x, -offset.y);
Point loc = w.getLocationOnScreen();
loc.translate(where.x, where.y);
w.setLocation(loc.x, loc.y);
}
}
};
alphaWindow.addMouseListener(handler);
alphaWindow.addMouseMotionListener(handler);
JPanel p = new JPanel(new BorderLayout(8, 8));
p.setBorder(new EmptyBorder(8, 8, 8, 8));
p.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 1L;
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
List<DataFlavor> list = Arrays.asList(transferFlavors);
if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
return true;
}
if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
return true;
}
System.err.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
return false;
}
public boolean importData(JComponent comp, Transferable t) {
try {
if (t.isDataFlavorSupported(URL_FLAVOR)) {
URL url = (URL) t.getTransferData(URL_FLAVOR);
setImage(Toolkit.getDefaultToolkit().getImage(url));
return true;
}
if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
String s = (String) t.getTransferData(URI_LIST_FLAVOR);
String[] uris = s.split("[\r\n]");
if (uris.length > 0) {
URL url = new URL(uris[0]);
setImage(Toolkit.getDefaultToolkit().getImage(url));
return true;
}
return false;
}
if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
setImage(image);
return true;
}
if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
File f = files.get(0);
URL url = new URL("file://" + f.toURI().toURL().getPath());
Image image = Toolkit.getDefaultToolkit().getImage(url);
setImage(image);
return true;
}
DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
if (flavor != null) {
Reader reader = flavor.getReaderForText(t);
char[] buf = new char[512];
StringBuilder b = new StringBuilder();
int count;
// encoding wrong
while ((count = reader.read(buf)) > 0) {
for (int i = 0; i < count; i++) {
if (buf[i] != 0)
b.append(buf, i, 1);
}
}
String html = b.toString();
Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
Matcher m = p.matcher(html);
if (m.find()) {
URL url = new URL(m.group(1));
System.out.println("Load image from " + url);
Image image = Toolkit.getDefaultToolkit().getImage(url);
setImage(image);
return true;
}
System.err.println("Can't parse text: " + html);
return false;
}
System.err.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
});
p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
final JSlider slider = new JSlider(0, 255, 255);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
}
});
p.add(slider, BorderLayout.SOUTH);
frame.getContentPane().add(p);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
centerOnScreen(frame);
frame.setVisible(true);
WindowUtils.setWindowTransparent(alphaWindow, true);
alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
try {
URL url = getClass().getResource("tardis.png");
if (url != null) {
setImage(Toolkit.getDefaultToolkit().getImage(url));
}
} catch (Exception e) {
}
}
use of java.awt.Window in project jna by java-native-access.
the class WindowUtilsTest method xtestReveal.
public void xtestReveal() throws Exception {
final int SIZE = 200;
System.setProperty("sun.java2d.noddraw", "true");
GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
Window w;
Container content;
if (true) {
JFrame frame = new JFrame(getName(), gconfig);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content = frame.getContentPane();
w = frame;
} else {
Frame frame = JOptionPane.getRootFrame();
JWindow window = new JWindow(frame, gconfig);
content = window.getContentPane();
w = window;
}
final Window f = w;
WindowUtils.setWindowTransparent(f, true);
content.add(new JButton("Quit") {
private static final long serialVersionUID = 1L;
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}, BorderLayout.SOUTH);
content.add(new JComponent() {
private static final long serialVersionUID = 1L;
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
protected void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics.create();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, SIZE, SIZE);
g.dispose();
g = (Graphics2D) graphics.create();
Color[] colors = { new Color(0, 0, 0), new Color(0, 0, 0, 128), new Color(128, 128, 128), new Color(128, 128, 128, 128), new Color(255, 255, 255), new Color(255, 255, 255, 128) };
for (int i = 0; i < colors.length; i++) {
g.setColor(colors[i]);
g.fillRect((SIZE * i) / colors.length, 0, (SIZE + colors.length - 1) / colors.length, SIZE);
}
g.setColor(Color.red);
g.drawRect(0, 0, SIZE - 1, SIZE - 1);
g.dispose();
SwingUtilities.getWindowAncestor(this).toFront();
}
});
f.pack();
f.addMouseListener(handler);
f.addMouseMotionListener(handler);
f.setLocation(100, 100);
f.setVisible(true);
while (f.isVisible()) {
Thread.sleep(1000);
//f.repaint();
}
}
use of java.awt.Window in project jna by java-native-access.
the class WindowUtilsTest method xtestWindowTransparency.
// Expect failure on windows and x11, since transparent pixels are not
// properly captured by java.awt.Robot
public void xtestWindowTransparency() throws Exception {
if (GraphicsEnvironment.isHeadless())
return;
System.setProperty("sun.java2d.noddraw", "true");
GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
Frame root = JOptionPane.getRootFrame();
final Window background = new Window(root);
background.setBackground(Color.white);
background.setLocation(X, Y);
final JWindow transparent = new JWindow(root, gconfig);
transparent.setLocation(X, Y);
((JComponent) transparent.getContentPane()).setOpaque(false);
transparent.getContentPane().add(new JComponent() {
private static final long serialVersionUID = 1L;
public Dimension getPreferredSize() {
return new Dimension(W, H);
}
protected void paintComponent(Graphics g) {
g = g.create();
g.setColor(Color.red);
g.fillRect(getWidth() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.dispose();
}
});
transparent.addMouseListener(handler);
transparent.addMouseMotionListener(handler);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
background.pack();
background.setSize(new Dimension(W, H));
background.setVisible(true);
transparent.pack();
transparent.setSize(new Dimension(W, H));
transparent.setVisible(true);
transparent.toFront();
}
});
WindowUtils.setWindowTransparent(transparent, true);
//robot.delay(60000);
Color sample = robot.getPixelColor(X + W / 2, Y + H / 2);
assertEquals("Painted pixel should be opaque", Color.red, sample);
sample = robot.getPixelColor(X + 10, Y + 10);
assertEquals("Unpainted pixel should be transparent", Color.white, sample);
}
use of java.awt.Window in project jna by java-native-access.
the class WindowUtilsTest method xtestWindowAlpha.
// Expect failure on windows and x11, since transparent pixels are not
// properly captured by java.awt.Robot
public void xtestWindowAlpha() throws Exception {
if (GraphicsEnvironment.isHeadless())
return;
System.setProperty("sun.java2d.noddraw", "true");
GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
Frame root = JOptionPane.getRootFrame();
final Window background = new Window(root);
background.setBackground(Color.white);
background.setLocation(X, Y);
final Window transparent = new Window(root, gconfig);
transparent.setBackground(Color.black);
transparent.setLocation(X, Y);
WindowUtils.setWindowAlpha(transparent, .5f);
transparent.addMouseListener(handler);
transparent.addMouseMotionListener(handler);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
background.pack();
background.setSize(new Dimension(W, H));
background.setVisible(true);
transparent.pack();
transparent.setSize(new Dimension(W, H));
transparent.setVisible(true);
}
});
//robot.delay(60000);
Point where = new Point(transparent.getX() + W / 2, transparent.getY() + H / 2);
Color sample = robot.getPixelColor(where.x, where.y);
// NOTE: w32 won't sample non-opaque windows
if (System.getProperty("os.name").startsWith("Windows")) {
assertFalse("Sample not transparent (w32)", sample.equals(transparent.getBackground()));
} else {
assertEquals("Sample should be 50% fg/bg", new Color(128, 128, 128), sample);
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
WindowUtils.setWindowAlpha(transparent, 1f);
}
});
sample = robot.getPixelColor(where.x, where.y);
assertEquals("Window should be opaque with alpha=1f", transparent.getBackground(), sample);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
WindowUtils.setWindowAlpha(transparent, 0f);
}
});
sample = robot.getPixelColor(where.x, where.y);
assertEquals("Window should be transparent with alpha=0f", transparent.getBackground(), sample);
}
Aggregations