use of java.awt.image 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.image in project groovy by apache.
the class JCaricature method paintComponent.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (empty) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
Image image = getImage("face", getFaceStyle());
int iw = image.getWidth(null);
int ih = image.getHeight(null);
// g2.translate(iw / 2, ih / 2);
g2.translate(getWidth() / 2, getHeight() / 2);
if (iw != getWidth()) {
float forcedScale = (float) getWidth() / (float) iw;
g2.scale(forcedScale, forcedScale);
}
float scale = getScale();
if (scale != 1) {
g2.scale((double) scale, (double) scale);
}
int rotation = getRotation();
if (rotation != 0) {
g2.rotate(Math.toRadians(rotation));
}
drawImage(g2, "face", getFaceStyle());
drawImage(g2, "hair", getHairStyle());
drawImage(g2, "eyes", getEyeStyle());
drawImage(g2, "nose", getNoseStyle());
drawImage(g2, "mouth", getMouthStyle());
g2.dispose();
}
use of java.awt.image in project screenbird by adamhub.
the class SoundBar method paint.
@Override
public void paint(Graphics g) {
Image img = indeterminateImage;
if (!active) {
img = indeterminateImage2;
}
int index = (this.getValue() * (activeImages.length - 1)) / this.getMaximum();
if (!isIndeterminate()) {
if (this.isActive()) {
img = activeImages[index];
} else {
img = inactiveImages[index];
}
}
g.drawImage(img, 0, 0, this);
}
use of java.awt.image in project cryptomator by cryptomator.
the class ExitUtil method createTrayIcon.
private TrayIcon createTrayIcon(Runnable exitCommand) {
final PopupMenu popup = new PopupMenu();
final MenuItem showItem = new MenuItem(localization.getString("tray.menu.open"));
showItem.addActionListener(this::restoreFromTray);
popup.add(showItem);
final MenuItem exitItem = new MenuItem(localization.getString("tray.menu.quit"));
exitItem.addActionListener(e -> exitCommand.run());
popup.add(exitItem);
final Image image;
if (SystemUtils.IS_OS_MAC_OSX && isMacMenuBarDarkMode()) {
image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_mac_white.png"));
} else if (SystemUtils.IS_OS_MAC_OSX) {
image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_mac_black.png"));
} else {
image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon.png"));
}
return new TrayIcon(image, localization.getString("app.name"), popup);
}
use of java.awt.image in project EnrichmentMapApp by BaderLab.
the class SwingUtil method resizeIcon.
public static ImageIcon resizeIcon(final ImageIcon icon, int width, int height) {
final Image img = icon.getImage().getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(img, 0, 0, width, height, null);
g.dispose();
return new ImageIcon(bi);
}
Aggregations