Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
RustyKnight / Print+Log.swift
Created February 5, 2022 23:23
A conceptual idea of to add additional logging information to the print statement
func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n",
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line, date: Date = Date()
) {
let components = file.description.components(separatedBy: "/")
if let name = components.last {
@RustyKnight
RustyKnight / ImageUtil.java
Created January 16, 2022 02:28
Slightly more advanced image pixelator
public final class ImageUtil {
public static BufferedImage pixelate(BufferedImage imageToPixelate, int pixelSize) {
BufferedImage pixelateImage = new BufferedImage(
imageToPixelate.getWidth(),
imageToPixelate.getHeight(),
imageToPixelate.getType());
for (int y = 0; y < imageToPixelate.getHeight(); y += pixelSize) {
for (int x = 0; x < imageToPixelate.getWidth(); x += pixelSize) {
@RustyKnight
RustyKnight / PixelateImage.java
Created January 16, 2022 02:21
Simple example of pixelating an image
protected BufferedImage pixelate(BufferedImage source) {
// How big should the pixelations be?
final int PIX_SIZE = 16;
BufferedImage img = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(source, 0, 0, this);
g2d.dispose();
// Get the raster data (array of pixels)
@RustyKnight
RustyKnight / RegularExpressionDocumentFilter.java
Created January 14, 2022 03:34
A document filter which can support a regular expression
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class RegularExpressionDocumentFilter extends ChainedDocumentFilter {
// Useful for every kind of input validation !
@RustyKnight
RustyKnight / CaseModificationDocumentFilter.java
Created January 14, 2022 03:34
A document filter which can change the case of the input to either upper or lower case
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
// Original source https://tips4java.wordpress.com/2009/10/18/chaining-document-filters/
public class CaseModificationDocumentFilter extends ChainedDocumentFilter {
public enum Case {
UPPER, LOWER
@RustyKnight
RustyKnight / LengthLimitDocumentFilter.java
Created January 14, 2022 03:33
Document filter that limits the length of the input to a set number of characters
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class LengthLimitDocumentFilter extends ChainedDocumentFilter {
private int maxCharacters;
public LengthLimitDocumentFilter(DocumentFilter filter, int maxChars) {
@RustyKnight
RustyKnight / NumericDocumentFilter.java
Created January 14, 2022 03:32
A document filter which restricts input to numeric values, with customisations for decimal precision and negative values
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class NumericDocumentFilter extends ChainedDocumentFilter {
private int decimalPrecision = 2;
private boolean allowNegative = false;
@RustyKnight
RustyKnight / CharacterOnlyDocumentFilter.java
Created January 14, 2022 03:31
A document filter which restricts input to characters only (alphabetical)
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class CharacterOnlyDocumentFilter extends ChainedDocumentFilter {
public CharacterOnlyDocumentFilter(DocumentFilter filter) {
super(filter);
}
@RustyKnight
RustyKnight / ChainedDocumentFilter.java
Created January 14, 2022 03:28
A DocumentFilter which allows filters to be chained together
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
@RustyKnight
RustyKnight / PointOnEllipse.java
Created January 2, 2022 04:53
Find point on ellipse
protected Point2D pointOnEllipse(double angle, Point2D center, double width, double height) {
width = width / 2d;
height = height / 2d;
angle = Math.toRadians(angle);
double t = Math.atan(width * Math.tan(angle) / height);
return new Point2D.Double(center.getX() + width * Math.cos(t), center.getY() + height * Math.sin(t));
}