Last active
September 18, 2023 14:45
-
-
Save d3ep4k/742ad2ca6f7e4214b18b to your computer and use it in GitHub Desktop.
This file contains 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
import java.awt.BasicStroke; | |
import java.awt.Color; | |
import java.awt.EventQueue; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.RenderingHints; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.geom.Area; | |
import java.awt.geom.GeneralPath; | |
import java.awt.geom.Rectangle2D; | |
import java.util.concurrent.TimeUnit; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.Timer; | |
public class HourGlass extends JFrame { | |
public static void main(String[] args) { | |
new HourGlass(); | |
} | |
public HourGlass() { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
setBackground(new Color(0, 0, 0, 0)); | |
Canvas c = new Canvas(0,1); | |
setContentPane(c); | |
pack(); | |
setSize(120, 300); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
}); | |
} | |
} | |
class Canvas extends JPanel implements ActionListener{ | |
private int off; | |
private int hh,mm,elapsed; | |
private int h = 100; | |
private int tick; | |
Timer t; | |
public Canvas(int hh, int mm) { | |
super(); | |
this.hh = hh; | |
this.mm = mm; | |
tick = ((hh * 3600 + mm * 60) * 1000) / h; | |
t = new Timer(tick, this); | |
t.start(); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
int x = 0, y = 0, w = 100; | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
RenderingHints.VALUE_ANTIALIAS_ON); | |
g2.clearRect(x, y, w, h * 2+20); | |
g2.setColor(Color.orange); | |
GeneralPath polygon = new GeneralPath(GeneralPath.WIND_NON_ZERO, 4); | |
polygon.moveTo(x, y); | |
polygon.quadTo(x, h, w / 2, h); | |
polygon.quadTo(x, h, x, h * 2); | |
polygon.lineTo(w, h * 2); | |
polygon.quadTo(w, h, w / 2, h); | |
polygon.quadTo(w, h, w, y); | |
polygon.closePath(); | |
Rectangle2D mask = new Rectangle2D.Double(x, y + off, w, h - off); | |
Rectangle2D mask2 = new Rectangle2D.Double(x, y + 2 * h - off, w, off); | |
Area a1 = new Area(mask); | |
Area a2 = new Area(polygon); | |
a2.intersect(a1); | |
g2.fill(a2); | |
a1 = new Area(mask2); | |
a2 = new Area(polygon); | |
a2.intersect(a1); | |
g2.fill(a2); | |
g2.setStroke(new BasicStroke(1)); | |
g2.setColor(new Color(0xCCCCCC)); | |
g2.draw(polygon); | |
int millis = ((hh * 3600 + mm * 60) * 1000) - elapsed; | |
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), | |
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), | |
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); | |
g2.drawString(hms, 10, 210); | |
} | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
off = off + 1; | |
elapsed = elapsed + tick; | |
repaint(); | |
if(off == h){ | |
t.stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment