Jump to content

System notifications


Tunnelsnake

Recommended Posts

I haven't done it myself but I found this on stack overflow after googling java system notification:

import java.awt.*;
import java.awt.TrayIcon.MessageType;

public class TrayIconDemo {

    public static void main(String[] args) throws AWTException, java.net.MalformedURLException {
        if (SystemTray.isSupported()) {
            TrayIconDemo td = new TrayIconDemo();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }

    public void displayTray() throws AWTException, java.net.MalformedURLException {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resizes the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("System tray icon demo");
        tray.add(trayIcon);
        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
    }
}

 

Link to comment
Share on other sites

1 hour ago, d0zza said:

I haven't done it myself but I found this on stack overflow after googling java system notification:


import java.awt.*;
import java.awt.TrayIcon.MessageType;

public class TrayIconDemo {

    public static void main(String[] args) throws AWTException, java.net.MalformedURLException {
        if (SystemTray.isSupported()) {
            TrayIconDemo td = new TrayIconDemo();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }

    public void displayTray() throws AWTException, java.net.MalformedURLException {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resizes the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("System tray icon demo");
        tray.add(trayIcon);
        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
    }
}

 

 

Link to comment
Share on other sites

I made my own a very long time ago, back before they patched the farming exploit (being able to see plant growth and disease from anywhere in-game):

http://imgur.com/cFwNBZ4

I salvaged my old code to produce this:

http://imgur.com/sFYmx5f

Is this something you're after? Because it works with OSBot. You can always change the colours and fonts to suit your design. If you wanted to add audio as well, that's also in my old code, but audio will need a little more tinkering to get working.

Edited by liverare
  • Like 1
Link to comment
Share on other sites

12 hours ago, liverare said:

I made my own a very long time ago, back before they patched the farming exploit (being able to see plant growth and disease from anywhere in-game):

http://imgur.com/cFwNBZ4

I salvaged my old code to produce this:

http://imgur.com/sFYmx5f

Is this something you're after? Because it works with OSBot. You can always change the colours and fonts to suit your design. If you wanted to add audio as well, that's also in my old code, but audio will need a little more tinkering to get working.

This is exactly what i am looking for

  • Like 1
Link to comment
Share on other sites

Example of how to use:

Image img;
SystemAlert sa;

try {
	
	img = ImageIO.read(YourScript.class.getResourceAsStream("/unnamed.png"));
	
	sa = new SystemAlert(400, 100, img, "Task Completed!", "You've successfully chopped 5000 yew logs.", new Date());
	
	sa.setVisible(true);
	
} catch (Exception e) {
	
	logger.error(e);
}

Note:

  • Load images locally, not externally.
  • You can chose between passing either a java.awt.Image or javax.swing.ImageIcon for the 3rd parameter. If you chose the first, the image will be resized proportionally to the alert box, whereas the other one will allow you to have complete control over the image size.
  • The alert can be dismissed by clicking on it.
  • The default fonts and colours can be found in the configureComponents function (lines 126-129).

Source code:

package com.liverare.api.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * System alert mimic
 * 
 * @author LiveRare
 *
 */
public class SystemAlert extends JDialog {

	private static final long serialVersionUID = 1L;
	private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("E, dd MMM yyyy - HH:mm:ss");
	
	private JLabel iconLabel;
	private JLabel titleLabel;
	private JTextArea contentTextArea;
	private JLabel timestampLabel;
	private JPanel containerPanel;
	
	/**
	 * Main constructor
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 */
	public SystemAlert(int width, int height) {
		super();
		super.setSize(width, height);
		super.setAlwaysOnTop(true);
		super.setUndecorated(true);
		
		configureBoundary();
		configureComponents();
		addComponents();
		addCloseListener();
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(ImageIcon, String, String, Date)}
	 */
	public SystemAlert(int width, int height, ImageIcon icon, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(icon, title, message, timestamp);
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param image
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(Image, String, String, Date)}
	 */
	public SystemAlert(int width, int height, Image image, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(image, title, message, timestamp);
	}
	
	/**
	 * Configures the components for the alert.
	 */
	private void configureComponents() {
		
		iconLabel = new JLabel();
		
		titleLabel = new JLabel("Title");
		
		contentTextArea = new JTextArea("Content", 2, 50);
		contentTextArea.setEditable(false);
		contentTextArea.setWrapStyleWord(true);
		contentTextArea.setLineWrap(true);
		contentTextArea.setFocusable(false);
		
		timestampLabel = new JLabel("Thu, 16 July 2015 - 21:24:11", JLabel.RIGHT);
		
		containerPanel = new JPanel(new GridBagLayout());
		
		setFont(new Font("Calibri", Font.PLAIN, 12));
		setForeground(Color.decode("#fdfdfd"));
		setBackground(Color.decode("#262626"));
		setSelectedTextColor(Color.decode("#fd2112"));
	}
	
	/**
	 * Sets the location for the alert.
	 */
	private void configureBoundary() {
		
		int x = 0;
		int y = 0;
		
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
		
		x += SCREEN_SIZE.width;
		x -= getWidth();
		
		y += SCREEN_SIZE.height;
		y -= getHeight();
		y -= screenInsets.bottom;
		
		setLocation(x, y);
	}
	
	/**
	 * Adds components to the alert.
	 */
	private void addComponents() {
		
		containerPanel.add(iconLabel, new GridBagConstraints(0, 0, 1, 3, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(5, 15, 5, 15), 0, 0));
		containerPanel.add(titleLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
		containerPanel.add(contentTextArea, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		containerPanel.add(timestampLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
		
		add(containerPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Adds a mouse listener that will trigger when either the alert, or child
	 * components of the alert, are clicked.
	 */
	private void addCloseListener() {
		
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
			
		});
	}
	
	@Override
	public synchronized void addMouseListener(MouseListener l) {
		super.addMouseListener(l);
		if (iconLabel != null) {
			iconLabel.addMouseListener(l);
		}
		if (titleLabel != null) {
			titleLabel.addMouseListener(l);
		}
		if (contentTextArea != null) {
			contentTextArea.addMouseListener(l);
		}
		if (timestampLabel != null) {
			timestampLabel.addMouseListener(l);
		}
		if (containerPanel != null) {
			containerPanel.addMouseListener(l);
		}
	}
	
	@Override
	public void setFont(Font f) {
		super.setFont(f);
		if (titleLabel != null) {
			titleLabel.setFont(f.deriveFont(Font.BOLD, 19));
		}
		if (contentTextArea != null) {
			contentTextArea.setFont(f.deriveFont(Font.PLAIN, 13));
		}
		if (timestampLabel != null) {
			timestampLabel.setFont(f.deriveFont(Font.ITALIC, 11));
		}
	}
	
	@Override
	public void setForeground(Color c) {
		super.setForeground(c);
		if (titleLabel != null) {
			titleLabel.setForeground(c);
		}
		if (contentTextArea != null) {
			contentTextArea.setForeground(c);
		}
		if (timestampLabel != null) {
			timestampLabel.setForeground(c);
		}
	}
	
	@Override
	public void setBackground(Color bgColor) {
		super.setBackground(bgColor);
		if (contentTextArea != null) {
			contentTextArea.setBackground(bgColor);
			contentTextArea.setSelectionColor(bgColor);
		}
		if (containerPanel != null) {
			containerPanel.setBackground(bgColor);
		}
	}
	
	public void setSelectedTextColor(Color c) {
		contentTextArea.setSelectedTextColor(c);
	}
	
	/**
	 * Set alert contents
	 * 
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(ImageIcon icon, String title, String message, Date timestamp) {
		iconLabel.setIcon(icon);
		titleLabel.setText(title);
		contentTextArea.setText(message);
		timestampLabel.setText(DATE_FORMAT.format(timestamp));
	}
	
	/**
	 * Set alert contents
	 * 
	 * Note: will attempt to proportionally scale the image provided
	 * 
	 * @param image
	 *            - Alert icon image
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(Image image, String title, String message, Date timestamp) {
		
		double height = getHeight();
		double imgWidth = image.getWidth(this);
		double newImgWidth = 0;
		double newImgHeight = 0;
		
		newImgHeight = (int) (height / 2);
		newImgWidth = (Math.min(newImgHeight, imgWidth) / Math.max(newImgHeight, imgWidth));
		newImgWidth *= imgWidth;
		
		ImageIcon icon = new ImageIcon(image.getScaledInstance((int) newImgWidth, (int) newImgHeight, Image.SCALE_DEFAULT));
		
		setAlert(icon, title, message, timestamp);
	}
}

Link to comment
Share on other sites

12 hours ago, liverare said:

Example of how to use:


Image img;
SystemAlert sa;

try {
	
	img = ImageIO.read(YourScript.class.getResourceAsStream("/unnamed.png"));
	
	sa = new SystemAlert(400, 100, img, "Task Completed!", "You've successfully chopped 5000 yew logs.", new Date());
	
	sa.setVisible(true);
	
} catch (Exception e) {
	
	logger.error(e);
}

Note:

  • Load images locally, not externally.
  • You can chose between passing either a java.awt.Image or javax.swing.ImageIcon for the 3rd parameter. If you chose the first, the image will be resized proportionally to the alert box, whereas the other one will allow you to have complete control over the image size.
  • The alert can be dismissed by clicking on it.
  • The default fonts and colours can be found in the configureComponents function (lines 126-129).

Source code:

 

  Hide contents

 



package com.liverare.api.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * System alert mimic
 * 
 * @author LiveRare
 *
 */
public class SystemAlert extends JDialog {

	private static final long serialVersionUID = 1L;
	private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("E, dd MMM yyyy - HH:mm:ss");
	
	private JLabel iconLabel;
	private JLabel titleLabel;
	private JTextArea contentTextArea;
	private JLabel timestampLabel;
	private JPanel containerPanel;
	
	/**
	 * Main constructor
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 */
	public SystemAlert(int width, int height) {
		super();
		super.setSize(width, height);
		super.setAlwaysOnTop(true);
		super.setUndecorated(true);
		
		configureBoundary();
		configureComponents();
		addComponents();
		addCloseListener();
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(ImageIcon, String, String, Date)}
	 */
	public SystemAlert(int width, int height, ImageIcon icon, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(icon, title, message, timestamp);
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param image
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(Image, String, String, Date)}
	 */
	public SystemAlert(int width, int height, Image image, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(image, title, message, timestamp);
	}
	
	/**
	 * Configures the components for the alert.
	 */
	private void configureComponents() {
		
		iconLabel = new JLabel();
		
		titleLabel = new JLabel("Title");
		
		contentTextArea = new JTextArea("Content", 2, 50);
		contentTextArea.setEditable(false);
		contentTextArea.setWrapStyleWord(true);
		contentTextArea.setLineWrap(true);
		contentTextArea.setFocusable(false);
		
		timestampLabel = new JLabel("Thu, 16 July 2015 - 21:24:11", JLabel.RIGHT);
		
		containerPanel = new JPanel(new GridBagLayout());
		
		setFont(new Font("Calibri", Font.PLAIN, 12));
		setForeground(Color.decode("#fdfdfd"));
		setBackground(Color.decode("#262626"));
		setSelectedTextColor(Color.decode("#fd2112"));
	}
	
	/**
	 * Sets the location for the alert.
	 */
	private void configureBoundary() {
		
		int x = 0;
		int y = 0;
		
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
		
		x += SCREEN_SIZE.width;
		x -= getWidth();
		
		y += SCREEN_SIZE.height;
		y -= getHeight();
		y -= screenInsets.bottom;
		
		setLocation(x, y);
	}
	
	/**
	 * Adds components to the alert.
	 */
	private void addComponents() {
		
		containerPanel.add(iconLabel, new GridBagConstraints(0, 0, 1, 3, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(5, 15, 5, 15), 0, 0));
		containerPanel.add(titleLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
		containerPanel.add(contentTextArea, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		containerPanel.add(timestampLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
		
		add(containerPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Adds a mouse listener that will trigger when either the alert, or child
	 * components of the alert, are clicked.
	 */
	private void addCloseListener() {
		
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
			
		});
	}
	
	@Override
	public synchronized void addMouseListener(MouseListener l) {
		super.addMouseListener(l);
		if (iconLabel != null) {
			iconLabel.addMouseListener(l);
		}
		if (titleLabel != null) {
			titleLabel.addMouseListener(l);
		}
		if (contentTextArea != null) {
			contentTextArea.addMouseListener(l);
		}
		if (timestampLabel != null) {
			timestampLabel.addMouseListener(l);
		}
		if (containerPanel != null) {
			containerPanel.addMouseListener(l);
		}
	}
	
	@Override
	public void setFont(Font f) {
		super.setFont(f);
		if (titleLabel != null) {
			titleLabel.setFont(f.deriveFont(Font.BOLD, 19));
		}
		if (contentTextArea != null) {
			contentTextArea.setFont(f.deriveFont(Font.PLAIN, 13));
		}
		if (timestampLabel != null) {
			timestampLabel.setFont(f.deriveFont(Font.ITALIC, 11));
		}
	}
	
	@Override
	public void setForeground(Color c) {
		super.setForeground(c);
		if (titleLabel != null) {
			titleLabel.setForeground(c);
		}
		if (contentTextArea != null) {
			contentTextArea.setForeground(c);
		}
		if (timestampLabel != null) {
			timestampLabel.setForeground(c);
		}
	}
	
	@Override
	public void setBackground(Color bgColor) {
		super.setBackground(bgColor);
		if (contentTextArea != null) {
			contentTextArea.setBackground(bgColor);
			contentTextArea.setSelectionColor(bgColor);
		}
		if (containerPanel != null) {
			containerPanel.setBackground(bgColor);
		}
	}
	
	public void setSelectedTextColor(Color c) {
		contentTextArea.setSelectedTextColor(c);
	}
	
	/**
	 * Set alert contents
	 * 
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(ImageIcon icon, String title, String message, Date timestamp) {
		iconLabel.setIcon(icon);
		titleLabel.setText(title);
		contentTextArea.setText(message);
		timestampLabel.setText(DATE_FORMAT.format(timestamp));
	}
	
	/**
	 * Set alert contents
	 * 
	 * Note: will attempt to proportionally scale the image provided
	 * 
	 * @param image
	 *            - Alert icon image
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(Image image, String title, String message, Date timestamp) {
		
		double height = getHeight();
		double imgWidth = image.getWidth(this);
		double newImgWidth = 0;
		double newImgHeight = 0;
		
		newImgHeight = (int) (height / 2);
		newImgWidth = (Math.min(newImgHeight, imgWidth) / Math.max(newImgHeight, imgWidth));
		newImgWidth *= imgWidth;
		
		ImageIcon icon = new ImageIcon(image.getScaledInstance((int) newImgWidth, (int) newImgHeight, Image.SCALE_DEFAULT));
		
		setAlert(icon, title, message, timestamp);
	}
}

 

 

Thank you very much worked like a charm

Link to comment
Share on other sites

  • 3 weeks later...
On 18/08/2017 at 6:38 PM, liverare said:

Example of how to use:


Image img;
SystemAlert sa;

try {
	
	img = ImageIO.read(YourScript.class.getResourceAsStream("/unnamed.png"));
	
	sa = new SystemAlert(400, 100, img, "Task Completed!", "You've successfully chopped 5000 yew logs.", new Date());
	
	sa.setVisible(true);
	
} catch (Exception e) {
	
	logger.error(e);
}

Note:

  • Load images locally, not externally.
  • You can chose between passing either a java.awt.Image or javax.swing.ImageIcon for the 3rd parameter. If you chose the first, the image will be resized proportionally to the alert box, whereas the other one will allow you to have complete control over the image size.
  • The alert can be dismissed by clicking on it.
  • The default fonts and colours can be found in the configureComponents function (lines 126-129).

Source code:

 

  Reveal hidden contents

 



package com.liverare.api.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * System alert mimic
 * 
 * @author LiveRare
 *
 */
public class SystemAlert extends JDialog {

	private static final long serialVersionUID = 1L;
	private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("E, dd MMM yyyy - HH:mm:ss");
	
	private JLabel iconLabel;
	private JLabel titleLabel;
	private JTextArea contentTextArea;
	private JLabel timestampLabel;
	private JPanel containerPanel;
	
	/**
	 * Main constructor
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 */
	public SystemAlert(int width, int height) {
		super();
		super.setSize(width, height);
		super.setAlwaysOnTop(true);
		super.setUndecorated(true);
		
		configureBoundary();
		configureComponents();
		addComponents();
		addCloseListener();
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(ImageIcon, String, String, Date)}
	 */
	public SystemAlert(int width, int height, ImageIcon icon, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(icon, title, message, timestamp);
	}

	/**
	 * 
	 * @param width
	 *            - Alert width
	 * @param height
	 *            - Alert height
	 * @param image
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time-stamp
	 * @see {@link SystemAlert#SystemAlert(int, int)}
	 * @see {@link SystemAlert#setAlert(Image, String, String, Date)}
	 */
	public SystemAlert(int width, int height, Image image, String title, String message, Date timestamp) {
		this(width, height);
		setAlert(image, title, message, timestamp);
	}
	
	/**
	 * Configures the components for the alert.
	 */
	private void configureComponents() {
		
		iconLabel = new JLabel();
		
		titleLabel = new JLabel("Title");
		
		contentTextArea = new JTextArea("Content", 2, 50);
		contentTextArea.setEditable(false);
		contentTextArea.setWrapStyleWord(true);
		contentTextArea.setLineWrap(true);
		contentTextArea.setFocusable(false);
		
		timestampLabel = new JLabel("Thu, 16 July 2015 - 21:24:11", JLabel.RIGHT);
		
		containerPanel = new JPanel(new GridBagLayout());
		
		setFont(new Font("Calibri", Font.PLAIN, 12));
		setForeground(Color.decode("#fdfdfd"));
		setBackground(Color.decode("#262626"));
		setSelectedTextColor(Color.decode("#fd2112"));
	}
	
	/**
	 * Sets the location for the alert.
	 */
	private void configureBoundary() {
		
		int x = 0;
		int y = 0;
		
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
		
		x += SCREEN_SIZE.width;
		x -= getWidth();
		
		y += SCREEN_SIZE.height;
		y -= getHeight();
		y -= screenInsets.bottom;
		
		setLocation(x, y);
	}
	
	/**
	 * Adds components to the alert.
	 */
	private void addComponents() {
		
		containerPanel.add(iconLabel, new GridBagConstraints(0, 0, 1, 3, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(5, 15, 5, 15), 0, 0));
		containerPanel.add(titleLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
		containerPanel.add(contentTextArea, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		containerPanel.add(timestampLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
		
		add(containerPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Adds a mouse listener that will trigger when either the alert, or child
	 * components of the alert, are clicked.
	 */
	private void addCloseListener() {
		
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
			
		});
	}
	
	@Override
	public synchronized void addMouseListener(MouseListener l) {
		super.addMouseListener(l);
		if (iconLabel != null) {
			iconLabel.addMouseListener(l);
		}
		if (titleLabel != null) {
			titleLabel.addMouseListener(l);
		}
		if (contentTextArea != null) {
			contentTextArea.addMouseListener(l);
		}
		if (timestampLabel != null) {
			timestampLabel.addMouseListener(l);
		}
		if (containerPanel != null) {
			containerPanel.addMouseListener(l);
		}
	}
	
	@Override
	public void setFont(Font f) {
		super.setFont(f);
		if (titleLabel != null) {
			titleLabel.setFont(f.deriveFont(Font.BOLD, 19));
		}
		if (contentTextArea != null) {
			contentTextArea.setFont(f.deriveFont(Font.PLAIN, 13));
		}
		if (timestampLabel != null) {
			timestampLabel.setFont(f.deriveFont(Font.ITALIC, 11));
		}
	}
	
	@Override
	public void setForeground(Color c) {
		super.setForeground(c);
		if (titleLabel != null) {
			titleLabel.setForeground(c);
		}
		if (contentTextArea != null) {
			contentTextArea.setForeground(c);
		}
		if (timestampLabel != null) {
			timestampLabel.setForeground(c);
		}
	}
	
	@Override
	public void setBackground(Color bgColor) {
		super.setBackground(bgColor);
		if (contentTextArea != null) {
			contentTextArea.setBackground(bgColor);
			contentTextArea.setSelectionColor(bgColor);
		}
		if (containerPanel != null) {
			containerPanel.setBackground(bgColor);
		}
	}
	
	public void setSelectedTextColor(Color c) {
		contentTextArea.setSelectedTextColor(c);
	}
	
	/**
	 * Set alert contents
	 * 
	 * @param icon
	 *            - Alert icon
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(ImageIcon icon, String title, String message, Date timestamp) {
		iconLabel.setIcon(icon);
		titleLabel.setText(title);
		contentTextArea.setText(message);
		timestampLabel.setText(DATE_FORMAT.format(timestamp));
	}
	
	/**
	 * Set alert contents
	 * 
	 * Note: will attempt to proportionally scale the image provided
	 * 
	 * @param image
	 *            - Alert icon image
	 * @param title
	 *            - Alert title
	 * @param message
	 *            - Alert message
	 * @param timestamp
	 *            - Alert time stamp
	 */
	public void setAlert(Image image, String title, String message, Date timestamp) {
		
		double height = getHeight();
		double imgWidth = image.getWidth(this);
		double newImgWidth = 0;
		double newImgHeight = 0;
		
		newImgHeight = (int) (height / 2);
		newImgWidth = (Math.min(newImgHeight, imgWidth) / Math.max(newImgHeight, imgWidth));
		newImgWidth *= imgWidth;
		
		ImageIcon icon = new ImageIcon(image.getScaledInstance((int) newImgWidth, (int) newImgHeight, Image.SCALE_DEFAULT));
		
		setAlert(icon, title, message, timestamp);
	}
}

 

 

Edited: nvm :)

Edited by Viston
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...