For the first blog post I will post code for clock coded in java that is always on top. It is not finished project needs some improvements and features added, like options to turn on or off always on top, change themes and so on. So here is the code:
class gui is getting system time and display it:
class gui is getting system time and display it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package clock; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.Timer; /** * * @author Anak1n */ public class gui extends JPanel implements ActionListener { int hour, minutes, seconds; String a, b, c; Calendar time; ImageIcon zero = new ImageIcon("images/zero.png"); ImageIcon one = new ImageIcon("images/one.png"); ImageIcon two = new ImageIcon("images/two.png"); ImageIcon three = new ImageIcon("images/three.png"); ImageIcon four = new ImageIcon("images/four.png"); ImageIcon five = new ImageIcon("images/five.png"); ImageIcon six = new ImageIcon("images/six.png"); ImageIcon seven = new ImageIcon("images/seven.png"); ImageIcon eight = new ImageIcon("images/eight.png"); ImageIcon nine = new ImageIcon("images/nine.png"); Image[] numbers; public gui() { setOpaque(false); Timer timer = new Timer(1000, this); numbers = new Image[]{ zero.getImage(), one.getImage(), two.getImage(), three.getImage(), four.getImage(), five.getImage(), six.getImage(), seven.getImage(), eight.getImage(), nine.getImage()}; a = ""; b = ""; c = ""; timer.start(); } @Override public void actionPerformed(ActionEvent e) { time = Calendar.getInstance(); hour = time.get(Calendar.HOUR_OF_DAY); minutes = time.get(Calendar.MINUTE); seconds = time.get(Calendar.SECOND); if (hour < 10) { b = String.valueOf("0" + hour); } else { b = String.valueOf(hour); } if (seconds < 10) { a = String.valueOf("0" + seconds); } else { a = String.valueOf(seconds); } if (minutes < 10) { c = String.valueOf("0" + minutes); } else { c = String.valueOf(minutes); } repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < a.length(); i++) { g.drawImage(numbers[a.charAt(i) - '0'], 180 + (40 * i), 5, this); } for (int i = 0; i < b.length(); i++) { g.drawImage(numbers[b.charAt(i) - '0'], 0 + (40 * i), 5, this); } for (int i = 0; i < c.length(); i++) { g.drawImage(numbers[c.charAt(i) - '0'], 90 + (40 * i), 5, this); } } } |
Main class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package clock; import java.awt.AWTException; import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JOptionPane; /** * * @author Anak1n */ public class ClockMain { public static void main(String[] args) { final JDialog window = new JDialog(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int left = (int) screenSize.getWidth(); Image trayImage; ImageIcon trayIconImage; trayIconImage = new ImageIcon("images/tray.jpg"); trayImage = trayIconImage.getImage(); window.setLocation(left / 2 - 140, 0); if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(trayImage); final SystemTray tray = SystemTray.getSystemTray(); MenuItem exitItem = new MenuItem("Exit"); MenuItem aboutItem = new MenuItem("About"); exitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); aboutItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, " Autor: Kostic Goran" + "\n E-mail: red.zmaja@gmail.com" + "\n Verzija: 1.0", "About", JOptionPane.INFORMATION_MESSAGE); } }); popup.add(aboutItem); popup.add(exitItem); trayIcon.setToolTip("Clock"); trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { System.out.println("TrayIcon could not be added."); } window.setSize(280, 50); window.setResizable(false); gui clockGui = new gui(); window.setUndecorated(true); window.add(clockGui); window.setAlwaysOnTop(true); window.setBackground(new Color(0, 0, 0, 0)); window.setVisible(true); } } |
No comments:
Post a Comment