use of javax.swing.JWindow in project jna by java-native-access.
the class AlphaMaskDemo 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);
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;
// hit testing; not sure why it gets disabled
if (System.getProperty("os.name").startsWith("Windows"))
update(true, true);
}
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.out.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.out.println("Can't parse text: " + html);
return false;
}
System.out.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);
final JSlider slider = new JSlider(0, 255, 255);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
setAlpha(value / 255f);
}
});
p.add(slider, BorderLayout.SOUTH);
frame.getContentPane().add(p);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
centerOnScreen(frame);
frame.setVisible(true);
p.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
update();
}
});
try {
URL url = getClass().getResource("tardis.png");
if (url != null) {
setImage(Toolkit.getDefaultToolkit().getImage(url));
}
} catch (Exception e) {
}
}
use of javax.swing.JWindow in project jna by java-native-access.
the class WindowUtilsTest method testDisposeHeavyweightForcer.
/*
public void testWindowRegion() throws Exception {
if (GraphicsEnvironment.isHeadless())
return;
Frame root = JOptionPane.getRootFrame();
final Window back = new Window(root);
// Avoid display idiosyncrasies by using "standard" colors
// (Don't use black, since a failed sample is sometimes black)
final Color BACKGROUND = Color.GREEN;
final Color FOREGROUND = Color.RED;
back.setBackground(BACKGROUND);
back.setLocation(X, Y);
final JWindow front = new JWindow(root);
front.getContentPane().setBackground(FOREGROUND);
front.setLocation(X, Y);
Area mask = new Area(new Rectangle(0, 0, W, H));
mask.subtract(new Area(new Rectangle(W/4, H/4, W/2, H/2)));
WindowUtils.setWindowMask(front, mask);
front.addMouseListener(handler);
front.addMouseMotionListener(handler);
SwingUtilities.invokeAndWait(new Runnable() { public void run() {
back.pack();
back.setSize(new Dimension(W, H));
back.setVisible(true);
front.pack();
front.setSize(new Dimension(W, H));
front.setVisible(true);
}});
Point where = front.getLocationOnScreen();
where.translate(W/8, H/8);
Color sample = robot.getPixelColor(where.x, where.y);
long start = System.currentTimeMillis();
while (!sample.equals(FOREGROUND)) {
SwingUtilities.invokeAndWait(new Runnable() { public void run() {
front.toFront();
}});
Thread.sleep(10);
if (System.currentTimeMillis() - start > 5000)
fail("Timed out waiting for shaped window to appear, "
+ "expected foreground color (sample="
+ sample + " vs expected=" + FOREGROUND + ")");
sample = robot.getPixelColor(where.x, where.y);
}
where = front.getLocationOnScreen();
where.translate(W/2, H/2);
sample = robot.getPixelColor(where.x, where.y);
start = System.currentTimeMillis();
while (!sample.equals(BACKGROUND)) {
Thread.sleep(10);
if (System.currentTimeMillis() - start > 1000)
assertEquals("Background window should show through (center) "
+ where, BACKGROUND, sample);
sample = robot.getPixelColor(where.x, where.y);
}
}
*/
public void testDisposeHeavyweightForcer() throws Exception {
if (GraphicsEnvironment.isHeadless())
return;
// Forcer not required on OSX
if (Platform.isMac())
return;
Frame root = JOptionPane.getRootFrame();
final JWindow w = new JWindow(root);
w.getContentPane().add(new JLabel(getName()));
final Rectangle mask = new Rectangle(0, 0, 10, 10);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
w.pack();
WindowUtils.setWindowMask(w, mask);
w.setVisible(true);
}
});
try {
Window[] owned = w.getOwnedWindows();
WeakReference<Window> ref = null;
for (int i = 0; i < owned.length; i++) {
if (owned[i].getClass().getName().indexOf("Heavy") != -1) {
ref = new WeakReference<Window>(owned[i]);
break;
}
}
owned = null;
assertNotNull("Forcer not found", ref);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
WindowUtils.setWindowMask(w, WindowUtils.MASK_NONE);
}
});
System.gc();
long start = System.currentTimeMillis();
while (ref.get() != null) {
Thread.sleep(10);
System.gc();
if (System.currentTimeMillis() - start > 5000)
fail("Timed out waiting for forcer to be GC'd");
}
assertNull("Forcer not GC'd", ref.get());
} finally {
w.dispose();
}
}
use of javax.swing.JWindow in project jdk8u_jdk by JetBrains.
the class TopLevelLocation method main.
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
frame = new JFrame();
frame.getContentPane().setBackground(Color.PINK);
frame.setBounds(100, 100, 500, 400);
frame.setUndecorated(true);
frame.setVisible(true);
window = new JWindow(frame);
window.setBackground(Color.BLUE);
window.setAlwaysOnTop(true);
window.setBounds(200, 200, 200, 200);
window.addMouseListener(new MouseAdapter() {
private Point dragOrigin = null;
private Dimension origSize = null;
private Point origLoc = null;
private Point lastLoc = null;
private boolean left = false;
private boolean top = false;
private boolean bottom = false;
private boolean right = false;
@Override
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
dragOrigin = e.getLocationOnScreen();
origSize = window.getSize();
origLoc = window.getLocationOnScreen();
if (lastLoc != null) {
System.out.println("SET LOCATION: " + lastLoc);
System.out.println("CURRENT LOCATION: " + origLoc);
if (lastLoc.x != origLoc.x || lastLoc.y != origLoc.y) {
passed = false;
}
}
right = (origLoc.x + window.getWidth() - dragOrigin.x) < 5;
left = !right && dragOrigin.x - origLoc.x < 5;
bottom = (origLoc.y + window.getHeight() - dragOrigin.y) < 5;
top = !bottom && dragOrigin.y - origLoc.y < 5;
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("mouseDragged");
resize(e);
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
resize(e);
}
void resize(MouseEvent e) {
Point dragDelta = e.getLocationOnScreen();
dragDelta.translate(-dragOrigin.x, -dragOrigin.y);
Point newLoc = new Point(origLoc);
newLoc.translate(dragDelta.x, dragDelta.y);
Dimension newSize = new Dimension(origSize);
if (left || right) {
newSize.width += right ? dragDelta.x : -dragDelta.x;
}
if (top || bottom) {
newSize.height += bottom ? dragDelta.y : -dragDelta.y;
}
if (right || (top || bottom) && !left) {
newLoc.x = origLoc.x;
}
if (bottom || (left || right) && !top) {
newLoc.y = origLoc.y;
}
window.setBounds(newLoc.x, newLoc.y, newSize.width, newSize.height);
lastLoc = newLoc;
}
});
window.setVisible(true);
});
Thread.sleep(500);
Dimension size = window.getSize();
Point location = window.getLocation();
Robot robot = new Robot();
robot.setAutoDelay(200);
robot.setAutoWaitForIdle(true);
robot.waitForIdle();
robot.mouseMove(location.x + size.height - 2, location.y + size.width - 2);
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(location.x + size.height, location.y + size.width);
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(location.x + size.height + 2, location.y + size.width + 2);
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
Thread.sleep(500);
frame.dispose();
if (!passed) {
throw new RuntimeException("TEST FAILED: Location doesn't match!");
}
System.out.println("TEST PASSED!");
}
use of javax.swing.JWindow in project jdk8u_jdk by JetBrains.
the class bug7160604 method init.
public void init() {
SwingUtilities.invokeLater(() -> {
final JWindow window = new JWindow();
window.setLocation(200, 200);
window.setSize(300, 300);
final JLabel label = new JLabel("...click to invoke JPopupMenu");
label.setOpaque(true);
final JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createLineBorder(Color.RED));
window.setContentPane(contentPane);
contentPane.add(label, BorderLayout.NORTH);
final JComboBox comboBox = new JComboBox(new Object[] { "1", "2", "3", "4" });
contentPane.add(comboBox, BorderLayout.SOUTH);
final JPopupMenu jPopupMenu = new JPopupMenu();
jPopupMenu.add("string");
jPopupMenu.add(new AbstractAction("action") {
@Override
public void actionPerformed(final ActionEvent e) {
}
});
jPopupMenu.add(new JLabel("label"));
jPopupMenu.add(new JMenuItem("MenuItem"));
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(final MouseEvent e) {
jPopupMenu.show(label, 0, 0);
}
});
window.setBackground(new Color(0, 0, 0, 0));
window.setVisible(true);
});
}
use of javax.swing.JWindow in project Spark by igniterealtime.
the class RoarPanel method createWindow.
/**
* Creates the WindowGui
*
* @param icon
* @param head
* @param body
* @param posx
* @param posy
* @param backgroundcolor
* @param headerColor
* @param messageColor
* @return
*/
private static JWindow createWindow(Icon icon, String head, String body, int posx, int posy, Color backgroundcolor, Color headerColor, Color messageColor) {
final JWindow window = new JWindow();
JPanel windowpanel = new JPanel(new GridBagLayout());
windowpanel.setBackground(backgroundcolor);
AWTUtilities.setWindowShape(window, new RoundRectangle2D.Float(0, 0, WIDTH, HEIGHT, 20, 20));
try {
AWTUtilities.setWindowOpaque(window, true);
} catch (UnsupportedOperationException ex) {
Log.debug("Unable to make window opaque: " + ex);
}
JLabel text = new JLabel("<HTML>" + body + "</HTML>");
text.setForeground(messageColor);
JLabel header = new JLabel(head);
header.setForeground(headerColor);
header.setFont(new Font(header.getFont().getName(), Font.BOLD, header.getFont().getSize() + 2));
windowpanel.add(new JLabel(icon), new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
windowpanel.add(header, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 0, 5), 0, 0));
windowpanel.add(text, new GridBagConstraints(1, 1, 2, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
window.add(windowpanel);
window.setSize(WIDTH, HEIGHT);
window.setLocation(posx - (WIDTH + 5), posy + 5);
window.setAlwaysOnTop(true);
return window;
}
Aggregations