Question

I've attempted to create some progress bar images using the following code:

public static void makeImage(int percent)
{
    BufferedImage img = new BufferedImage(100, 30, BufferedImage.TYPE_INT_ARGB);
    Graphics g = img.getGraphics();

    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, 100, 30);

    for (int x = 0; x < percent; x++)
    {
        for (int y = 0; y < img.getHeight(); y++)
        {
            img.setRGB(x, y, Color.GREEN.darker().getRGB() );
        }
    }

    ImgUtility.save("/home/xxx/java/myProj/src/myProj/resources/progressbars/"
            + percent + ".png", img);
}

This creates a bunch of 100x30 images from 1.png to 100.png, which look like this:

1.png

50.png

100.png

Then, I'm trying to show these images in the tray, and cycle through them one by one, to show the illusion of an animated progress bar (in actual usage, I want to use these images to show the progress of a task being done):

    Tray tray = Display.getDefault().getSystemTray();
    TrayItem trayItem = new TrayItem(tray, SWT.NONE);

    for (int p = 1; p <= 100; p++)
    {
        Image icon = IconLoader.load("progressbars/" + p + ".png");
        System.out.println(icon.getBounds()); //always outputs
        //Rectangle {0, 0, 100, 30}, showing that the 100x30 images were loaded
        //correctly.
        trayItem.setImage(icon);
        try
        {
            Thread.sleep(1000);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(0);
        }
    }

However, when I run this code, instead of seeing the full image in the taskbar, I only see a single vertical black/dark green line.

I am on Linux Mint.

What am I doing wrong..?

Was it helpful?

Solution

In what context is the code that is manipulating the tray icon running? Is possible that it is blocking the UI thread from painting the icon? I'm not sure, but you may need to do this on a separate thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top