Showing posts with label swing. Show all posts
Showing posts with label swing. Show all posts

February 23, 2013

Java transparent widget

9 comments:


Cool widget in java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Scanner;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class Status {

    public static void main(String[] args) {
        final int w = 130;
        final int h = 500;

        Dimension x = Toolkit.getDefaultToolkit().getScreenSize();

        final JDialog d = new JDialog();
        d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        d.setUndecorated(true);
        d.setSize(w, h);
        d.setBackground(new Color(0, 0, 0, 0));
        d.setBounds(x.width - w - 20, 20, w, h);
        d.setAlwaysOnTop(true);

        JLabel l = new JLabel();
        l.setBackground(new Color(0, 0, 0, 0));
        l.setForeground(new Color(255, 255, 255));
        try {
            l.setFont(Font.createFont(Font.TRUETYPE_FONT, l.getClass().getResourceAsStream("/consola.ttf")).deriveFont(Font.PLAIN, 13));
        } catch (Exception e) {
            l.setFont(l.getFont().deriveFont(Font.PLAIN, 13));
            e.printStackTrace();
        }

        l.setBorder(javax.swing.BorderFactory.createMatteBorder(5, 0, 0, 0, new Color(255, 255, 255)));

        l.setVerticalAlignment(l.TOP);
        l.setHorizontalAlignment(l.LEFT);
        d.add(l);
        d.setVisible(true);
        String user = System.getProperty("user.name");
        String os = System.getProperty("os.name");
        double ram = 0;
        try {
            Process p = Runtime.getRuntime().exec("wmic OS get TotalVisibleMemorySize /Value");
            Scanner scan = new Scanner(p.getInputStream());
            while (scan.hasNext()) {
                String temp = scan.nextLine();
                //System.out.println(temp);
                if (temp.startsWith("TotalVisibleMemorySize")) {
                    ram = Long.parseLong(temp.split("=")[1]);
                    //System.out.println("RAM :" + ram);
                    break;
                }
            }
        } catch (Exception e) {
        }


        d.addMouseMotionListener(new java.awt.event.MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                d.setBounds(e.getXOnScreen(), e.getYOnScreen(), d.getWidth(), d.getHeight());
            }

            @Override
            public void mouseMoved(MouseEvent e) {
            }
        });

        d.addMouseListener(new java.awt.event.MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON2) {
                    System.exit(0);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

        long itime = Calendar.getInstance().getTimeInMillis();

        while (true) {
            StringBuilder status = new StringBuilder();
            status.append("Current user: ").append(user);
            status.append("
------------------");
            Calendar c = Calendar.getInstance();
            status.append("
Time: ").append(c.get(Calendar.HOUR_OF_DAY)).append(":").append(c.get(Calendar.MINUTE)).append(":").append(c.get(Calendar.SECOND));
            status.append("
Date: ").append(c.get(Calendar.DATE)).append("/").append(c.get(Calendar.MONTH) + 1).append("/").append(c.get(Calendar.YEAR));
            status.append("
------------------");
            status.append("
OS: " + os);
            status.append("
Memory: ").append(new DecimalFormat("#.##").format(ram / 1024 / 1024)).append(" GB");
            status.append("
------------------");
            File[] f = File.listRoots();
            long total = 0;
            long free = 0;
            for (int i = 0; i < f.length; i++) {
                total = f[i].getTotalSpace();
                free = f[i].getFreeSpace();
                if (total > 0) {
                    long p = (free * 100 / total);
                    status.append("
").append(f[i].getAbsolutePath().replace(File.separator, "")).append(" ").append(total / 1024 / 1024 / 1024).append(" GB");
                    status.append("
").append(p).append("% free : ").append(free / 1024 / 1024 / 1024).append(" GB");
                }
            }
            status.append("
------------------");
            try {
                Socket s = new Socket("www.google.com", 80);
                s.getInputStream();
                s.setSoTimeout(500);
                s.close();
                status.append("
Internet: On");
            } catch (Exception e) {
                status.append("
Internet: Off");
            }
            status.append("
Uptime: ").append((c.getTimeInMillis() - itime) / 1000).append(" s");
            status.append("
------------------");
            //status.append("
Geek.Manish");
            status.append("");
            l.setText(status.toString());
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        }

    }
}
Read More