add badge to tray icon (java) - java

i have created a simple app in java to show a tray icon and from there show a list of JIRA issues that are assigned to me.
what i have at the moment is a tray icon that when you right click on it brungs up a popup message with the last 10 open issues assigned to me, when you click a menu item it directs you to the desired issue in your browser of choice.
What i would now like it to do is display a badge over the top of the tray icon that shows how many open issues i have. i have the code to find the number of issues but i cant for the life of me work out how to add the badge to the tray icon.
im using :
java.awt.MenuItem;
java.awt.PopupMenu;
java.awt.SystemTray;
java.awt.TrayIcon;
to create the tray icon and popup menu.
any help would be greatly appreciated
Thanks

Okay so i figured it out,
first i select the original icon:
BufferedImage im = ImageIO.read(Systray.class.getResource("icon.gif"));
then i use Graphics2D to draw ontop of the image:
Graphics2D g2 = im.createGraphics();
g2.setColor(Color.BLACK);
g2.drawString("10", 2, 10);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(im, "png", baos);
byte[] b = baos.toByteArray();
then i create a new image icon from the byte array:
ImageIcon imgTmp = new ImageIcon(b);
finally i set the tray icon:
_icon.setImage(imgTmp.getImage());
(_icon is an instance of TrayIcon)
i hope that this helps someone else and if you have a better solution id love to see it

Related

How to save as image an on-screen Canvas3D?

I'm doing a program to create a die (cube) with different image textures based on the input of the user (user choose images on a SWT GUI).
Once the user choose the images, it can visualize the dice on a separate dialog, and perform some rotate operations over it (see, after perform a small rotation to see three faces in the screenshots).
See screenshots: http://pastebin.com/XqJfXL6i
And my problem starts here: I want to save the content of the canvas (the dice with the background in its current form, after being rotated). I've been searching for several codes and I think that my problem is because my current canvas is an "on-screen" canvas and I need an off-screen canvas, which will allow to save the content.
My current code is the following:
http://pastebin.com/ZAv0ATJN
And.. here starts the problem. It throws this exception:
java.lang.IllegalStateException: Canvas3D: Not in off-screen mode
Concretely it fails in this line:
ImageComponent2D ic2d = canvas.getOffScreenBuffer();
As you can see there are several lines commented that I tried before, but they didn't work neither.
Any clue about how to do it?
Thanks!
Based on the comment provided by gouessej (thanks!) finally I use this code, which works fine for my doubt:
private void saveImage(String img) throws Exception {
FileOutputStream fileOut = new FileOutputStream(img);
Robot r = new Robot();
BufferedImage bi = r.createScreenCapture(new java.awt.Rectangle(
(int) frame.getLocationOnScreen().getX(), (int) frame
.getLocationOnScreen().getY(), frame.getBounds().width,
frame.getBounds().height));
ImageIO.write(bi, "jpeg", fileOut);
fileOut.flush();
fileOut.close();
}

Jdialog box Title bar icon change

I need to change the Jdialog box title bar icon. By default it uses a java coffee image.
I have searched in internet and used many codes
1. Image im = Toolkit.getDefaultToolkit().getImage("/org/qmon/generate/Images/JDialog -2.ico");
dialog.setIconImage(im);
2. Toolkit kit = Toolkit.getDefaultToolkit ();
Image img = kit.getImage ("/org/qmon/generate/Images/Create File Tag-16x16.png");
dialog.setIconImage(img);
nothing works properly.. Kindly help me.. Thanks in Advance
Firtsly, ico is not a support image format for Java.
The likely reason you're having issues with the second approach is that getImage is expecting a file reference and the image you seem to referencing looks like it's embedded (stored within your application)
Try using something more like...
Image img = kit.getImage (getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
Instead.
Personally, I prefer ImageIO.read as it throws a IOException when something goes wrong...
Image img = ImageIO.read(getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
But that's me...
You should also consider taking a look at Convert List<BufferedImage> to Image which demonstrates the use of ico file (from a 3rd party API) and setIconImages method
Image image = ImageIO.read(new URL(
"http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
dialog.setIconImage( image );
dialog.setVisible(true);
I am using this in my application and working fine
java.net.URL url = ClassLoader.getSystemResource("res/java.png");
ImageIcon icon = new ImageIcon(url);
JOptionPane.showMessageDialog(null, jep, "UroSync",JOptionPane.INFORMATION_MESSAGE, icon);
To improve what MadProgrammer has said, I met the problem and I solved it instantiating a JDialog but using the static class Toolkit method getDefaultToolkit().getImage(Image img).
JDialog dialog = new JDialog();
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(MyMainClass.class.getResource("/myIcon.png")));
To do that you need to add before the image into the build path of the Project.

Encoding transparent animated gif in Java

I am using GifDecoder to read an animated .gif file and AnimGifEncoder to write it. (link)
If I display the original frames read by GifDecoder they display correctly and are transparent, but if I display the frames created by AnimatedGifEncoder the transparency is all wrong.
GifDecoder gif = new GifDecoder();
gif.read("image.gif");
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start("newimage.gif");
e.setTransparent(Color.BLACK);
for (int i=0;i<gif.getFrameCount();i++) {
anim.addFrame(gif.getFrame(i));
anim.setDelay(gif.getDelay(i));
}
anim.finish();
In this example I set the transparent color to black. But actually I want to get the transparent color information from the GifDecoder but I don't know how.
I'm using the following solution, although there are still some gif files that
are a mess after scaling... :
(At least it seems to get along with most non-transparent gifs)
Color transparentColor = null;
BufferedImage firstFrameImage = ImageIO.read([sourceFileHere]);
if (firstFrameImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel cm = (IndexColorModel) firstFrameImage.getColorModel();
int transparentPixel = cm.getTransparentPixel();
transparentColor = new Color(cm.getRGB(transparentPixel), true);
}
e.setTransparent(transparentColor);
This was all a long time ago, but I did manage to get it working at the time. Without digging out the code again... I got it working by:
Scan the original image and set the transparent areas to a colour that does not exist inside the original image.
Set the transparent colour inside the AnimGifEncoder to the colour that was previously assigned to the transparent areas.

JButton shows light blue background on Windows

I have the following code to make a custom looking JButton
ImageIcon icon = createImageIcon(
CommonUtils.class.getClassLoader().getResource("images/wright.png")
);
RightSlide.setIcon( icon );
ImageIcon icon2 = createImageIcon(
CommonUtils.class.getClassLoader().getResource("images/right_selected.png")
);
RightSlide.setPressedIcon( icon2);
RightSlide.setSelectedIcon(icon2);
RightSlide.setRolloverEnabled(true); // turn on before rollovers work
RightSlide.setRolloverIcon(icon2);
RightSlide.setBorderPainted(false);
RightSlide.setFocusPainted(false);
RightSlide.addActionListener(new ActionListener(){
The code generates a custom button. The button behaves as expected when hover over, pressed, clicked, and selected. This works on MacOS and Linux (Ubuntu). But the same code has a light blue background on Windows. Where does this come from and how do I get rid of it ?
Thanks
I think that you missing JButton#setContentAreaFilled(false); example here

Eclipse MenuManager: get ImageDescriptor of Image?

I cannot get this to work so I thought it might be a wise idea posting over here...
I have a context menu in SWT (actually its an Eclipse plugin). It's a cascaded menu, so it expands as soon as you hover over a certain entry...
My problem is, that I want to attach a small icon to the menu but I struggle with that!
Code:
....
manager.add(new Separator());
// icon for the "change color" menu
ImageDescriptor icon = ImageDescriptor.createFromFile(null,
"icons/palette_brush.png");
// submenu
MenuManager colorMenu = new MenuManager("Menu", icon, null);
// Actions
colorMenu.add(someAction);
// add the action to the submenu
manager.add(colorMenu);
....
My problem is, that the new MenuManager can either be called with 2 Arguments (no attached image) or 3 (with attached image). The image should be passed along as ImageDescriptor.
The question basically is:
"How can I get an Imagedescriptor off an image?"
Maybe it's an stupid mistake - but I cannot get an ImageDescriptor from an image file. I have an *.png icon ready to use, but I struggle incorporating this.
If anyone could help out with a snippet, that would get me an ImageDescriptor from an image file this would be soo awesome!
Best regards!
MenuManager Documentation:
MenuManager Docu
Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, "icons/palette_brush.png");
ImageDescriptor.createFromURL(fullPathString);
pluginId is the id of the plugin where you put your icon.
Bundle bundle= org.eclipse.core.runtime.Platform.getBundle(pluginId);
URL fullPathString = bundle.getEntry("icons/palette_brush.png");
desc= ImageDescriptor.createFromURL(fullPathString);
desc.createImage();

Categories