Created
September 28, 2015 07:16
-
-
Save holgerbrandl/7c61bace1d90aeaf7d11 to your computer and use it in GitHub Desktop.
An android like toast inspired by https://github.com/schnie/android-toasts-for-swing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package info.movito.ui; | |
| import javax.swing.*; | |
| import java.awt.*; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.awt.geom.RoundRectangle2D; | |
| class Toast extends JFrame { | |
| private final float MAX_OPACITY = 0.8f; | |
| private final float OPACITY_INCREMENT = 0.05f; | |
| private final int FADE_REFRESH_RATE = 20; | |
| private final int WINDOW_RADIUS = 15; | |
| private final int CHARACTER_LENGTH_MULTIPLIER = 9; | |
| private final int DISTANCE_FROM_PARENT_BOTTOM = 100; | |
| public Toast(JFrame owner, String toastText) { | |
| setTitle("Transparent JFrame Demo"); | |
| setLayout(new GridBagLayout()); | |
| setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| setUndecorated(true); | |
| setFocusableWindowState(false); | |
| setOpacity(0.4f); | |
| // setup the toast lable | |
| JLabel b1 = new JLabel(toastText); | |
| b1.setForeground(Color.WHITE); | |
| b1.setOpaque(false); | |
| add(b1); | |
| setSize(toastText.length() * CHARACTER_LENGTH_MULTIPLIER, 50); | |
| // setLocationRelativeTo(owner); | |
| int x = (int) (owner.getLocation().getX() + (owner.getWidth() / 2)); | |
| int y = (int) (owner.getLocation().getY() + owner.getHeight() - DISTANCE_FROM_PARENT_BOTTOM); | |
| setLocation(new Point(x, y)); | |
| // configure frame | |
| setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), WINDOW_RADIUS, WINDOW_RADIUS)); | |
| getContentPane().setBackground(new Color(0, 0, 0, 170)); | |
| } | |
| public void fadeIn() { | |
| setOpacity(0); | |
| setVisible(true); | |
| final Timer timer = new Timer(FADE_REFRESH_RATE, null); | |
| timer.setRepeats(true); | |
| timer.addActionListener(new ActionListener() { | |
| private float opacity = 0; | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| opacity += OPACITY_INCREMENT; | |
| setOpacity(Math.min(opacity, MAX_OPACITY)); | |
| if (opacity >= MAX_OPACITY) { | |
| timer.stop(); | |
| } | |
| } | |
| }); | |
| timer.start(); | |
| } | |
| public void fadeOut() { | |
| final Timer timer = new Timer(FADE_REFRESH_RATE, null); | |
| timer.setRepeats(true); | |
| timer.addActionListener(new ActionListener() { | |
| private float opacity = MAX_OPACITY; | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| opacity -= OPACITY_INCREMENT; | |
| setOpacity(Math.max(opacity, 0)); | |
| if (opacity <= 0) { | |
| timer.stop(); | |
| setVisible(false); | |
| dispose(); | |
| } | |
| } | |
| }); | |
| setOpacity(MAX_OPACITY); | |
| timer.start(); | |
| } | |
| public static void makeToast(final JFrame owner, final String toastText, final int durationSec) { | |
| new Thread(new Runnable() { | |
| @Override | |
| public void run() { | |
| try { | |
| Toast toastFrame = new Toast(owner, toastText); | |
| toastFrame.fadeIn(); | |
| Thread.sleep(durationSec * 1000); | |
| toastFrame.fadeOut(); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| }).start(); | |
| } | |
| public static void main(String args[]) { | |
| final JFrame frame = new JFrame("Cloud Tester"); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| JPanel jPanel = new JPanel(); | |
| jPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); | |
| // jPanel.setLayout(new BorderLayout()); | |
| JButton toastButton = new JButton("show toast"); | |
| jPanel.add(toastButton); | |
| toastButton.addActionListener(new AbstractAction() { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| Toast.makeToast(frame, "a toast!", 3); | |
| } | |
| }); | |
| frame.add(jPanel); | |
| frame.setSize(800, 600); | |
| frame.setVisible(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use ? getting error.
TRANSLUCENT translucency is not supported.