Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2009 Klaus Reimer <k@ailis.de> 
 * See LICENSE.md for licensing information.
 */

import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import java.util.List;

public class Main {
    /**
     * Creates a tray icon which uses the best matching icon from the specified list of icons. The icon list must be
     * sorted from smallest to largest image. The tray icon is NOT yet added to the system tray. The calling code must
     * do this when needed.
     *
     * @param icons
     *            The list of icons to choose from
     * @return The tray icon
     */

    public static TrayIcon createTrayIcon(final List<Image> icons) {
        final SystemTray systemTray = SystemTray.getSystemTray();
        final int trayIconWidth = systemTray.getTrayIconSize().width;
        Image useIcon = null;
        for (final Image icon : icons) {
            if (icon.getWidth(null) >= trayIconWidth) {
                useIcon = icon;
                break;
            }
        }
        if (useIcon == null) {
            useIcon = icons.get(icons.size() - 1);
        }
        final TrayIcon trayIcon = new TrayIcon(useIcon);
        trayIcon.setImageAutoSize(true);
        return trayIcon;
    }
}