Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
RustyKnight / Main.java
Created July 24, 2023 03:09
Simple demonstration of using multiple panels with a single MouseListener
package stackoverflow;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
@RustyKnight
RustyKnight / Main.java
Created September 1, 2022 23:42
Example layout of using CardLayout in a slightly unconventional way, supporting a custom background image
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
@RustyKnight
RustyKnight / ThreadedFractal.java
Created July 20, 2022 00:59
An example of using threads to generate a fractal
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@RustyKnight
RustyKnight / AnimateSorter.java
Created July 19, 2022 08:12
Animate sorting
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@RustyKnight
RustyKnight / ScalableBackground.java
Created June 9, 2022 00:59
Example of a scalable background image in Swing
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
@RustyKnight
RustyKnight / PrintPreviewJTable.java
Created June 8, 2022 02:05
A "simple" example of making a print preview of a JTable
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
@RustyKnight
RustyKnight / FilteredComoboBoxEditor.java
Created May 5, 2022 01:29
A "simple" demonstration of using a `DocumentFilter` with a `ComboBoxEditor`
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
@RustyKnight
RustyKnight / ButtonLayout.java
Created April 8, 2022 00:25
An attempt to provide a simple "button layout" concept for Swing - this will layout buttons, based on the largest button, either vertically or horizontally with some anchor options
public class ButtonLayout implements LayoutManager2 {
public enum Alignment {
VERTICAL, HORIZONTAL
}
public enum Anchor {
LEADING, CENTER, TRAILING
}
@RustyKnight
RustyKnight / ColorBand.java
Created March 31, 2022 03:03
Example of color band/blending algorthim
public class ColorBand {
private float[] fractions;
private Color[] colors;
public ColorBand(float[] fractions, Color[] colors) {
this.fractions = fractions;
this.colors = colors;
}
@RustyKnight
RustyKnight / TerminalInput.java
Last active March 30, 2022 21:40
A conceptually terminal parser for Java using `Scanner`
public class TerminalInput {
private Scanner terminalScanner;
public TerminalInput() {
terminalScanner = new Scanner(System.in);
}
public String getNextLine() {
return terminalScanner.nextLine();
}