Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
RustyKnight / Test.java
Created May 5, 2018 04:14
Testing `ResizableCardLayout` proposed as an answer to https://stackoverflow.com/questions/50184994/cardlayout-in-java-gui - but which doesn't do anything that `CardLayout` doesn't already do
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@RustyKnight
RustyKnight / Counter.java
Last active May 16, 2018 21:57
Simple "counter" for duration based counting (ie timeouts)
import java.time.Duration;
import java.time.LocalDateTime;
/*
I seem to need to provide a lot of examples on stack overflow which are either performing a forward, backward or needs
to calculate the percentage of progression for a period of time.
Since most of this functionality can easily be distilled, I wrote myself a simple "counter" class.
@RustyKnight
RustyKnight / StopWatch.java
Last active June 12, 2018 03:55
A simple concept of a reusable "stop watch" with pause/resume capabilities
import java.time.Duration;
import java.time.Instant;
public class StopWatch {
private Instant startTime;
private Duration totalRunTime = Duration.ZERO;
public StopWatch start() {
startTime = Instant.now();
@RustyKnight
RustyKnight / TestInvokeLater.java
Created May 18, 2018 21:49
Simple test of invoke and wait
JFileChooser chooser = new JFileChooser();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
chooser.showOpenDialog(null);
JOptionPane.showMessageDialog(null, "Bananas");
@RustyKnight
RustyKnight / TimeLine.java
Last active May 27, 2018 04:03
A concept of a generic, reusable TimeLine which can be used to calculate a value between key frames based on the progression through the timeline
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class TimeLine<T> {
private Map<Float, KeyFrame<T>> mapEvents;
@RustyKnight
RustyKnight / SplineInterpolator.java
Created May 27, 2018 04:00
A implementation of a SplineInterpolator, used to adjust animation timings
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
public class SplineInterpolator {
private final double points[];
private final List<PointUnit> normalisedCurve;
@RustyKnight
RustyKnight / DurationTimeLine.java
Created May 27, 2018 04:04
A duration based `TimeLine`
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationTimeLine<T> extends TimeLine<T> {
private Duration duration;
private LocalDateTime startedAt;
public DurationTimeLine(Duration duration, Blender<T> blender) {
@RustyKnight
RustyKnight / Animation.java
Last active June 3, 2018 10:42
Simple example of a central "animator" and "animatable" properties, with easement
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.time.Duration;
import java.time.LocalDateTime;
@RustyKnight
RustyKnight / Animator.java
Created June 6, 2018 22:35
A simple wrapper around a Swing `Timer` that provides `Duration` based animation that also provides pause/resume functionality
public class Animator {
private Instant startTime;
private Duration totalRunTime = Duration.ZERO;
private Duration runTime;
private Timer timer;
@RustyKnight
RustyKnight / AlarmClock.swift
Last active June 25, 2018 22:49
Simple implementation of a "clock" or "stop watch", capable of been paused
import Foundation
class AlarmClock: SimpleClock {
var timeout: TimeInterval = 0
var hasExpired: Bool {
return duration >= timeout
}