Resetting a Screen

Published — Edited

This guide assumes that you have already read Drawing a Screen.

Executor Service

In order to show the result of each reset method, ExecutorServices are used to run the reset and repaint methods two seconds after the program is launched.

The following example shows the creation of an ExecutorService which will print "Hello, World!".


public class ExampleA {
	public static void main(final String[] args) {
		Executors.newSingleThreadScheduledExecutor().schedule(() -> {
			System.out.println("Hello, World!");
			System.exit(0);
		}, 2, TimeUnit.SECONDS);
	}
}

Resetting Code Points

The resetCodePoints() method can be used to reset the code point of every tile on a VPanel.


public class ExampleB {
	public static void main(final String[] args) {
		try {
			UIManager.setLookAndFeel(VTerminalLookAndFeel.getInstance(24));
		} catch (final UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(() -> {
			final var frame = new VFrame(40, 20);
			frame.setVisible(true);
			frame.pack();
			frame.setLocationRelativeTo(null);

			final var panel = frame.getContentPane();
			for (int y = 0 ; y < panel.getHeightInTiles() ; y++) {
				for (int x = 0 ; x < panel.getWidthInTiles() ; x++) {
					panel.setCodePointAt(x, y, getRandomCodePoint());
				}
			}

			Executors.newSingleThreadScheduledExecutor().schedule(() -> {
				panel.resetCodePoints();
				SwingUtilities.invokeLater(panel::repaint);
			}, 2, TimeUnit.SECONDS);
		});
	}

	private static int getRandomCodePoint() {
		return ThreadLocalRandom.current().nextInt(33, 127);
	}
}

Resetting Background Colors

The resetBackgroundColors() method can be used to reset the background color of every tile on a VPanel.


public class ExampleC {
	public static void main(final String[] args) {
		try {
			UIManager.setLookAndFeel(VTerminalLookAndFeel.getInstance(24));
		} catch (final UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(() -> {
			final var frame = new VFrame(40, 20);
			frame.setVisible(true);
			frame.pack();
			frame.setLocationRelativeTo(null);

			final var panel = frame.getContentPane();
			for (int y = 0 ; y < panel.getHeightInTiles() ; y++) {
				for (int x = 0 ; x < panel.getWidthInTiles() ; x++) {
					panel.setBackgroundAt(x, y, getRandomColor());
				}
			}

			Executors.newSingleThreadScheduledExecutor().schedule(() -> {
				panel.resetBackgroundColors();
				SwingUtilities.invokeLater(panel::repaint);
			}, 2, TimeUnit.SECONDS);
		});
	}

	private static Color getRandomColor() {
		switch (ThreadLocalRandom.current().nextInt(0, 6)) {
			case 0 -> { return Color.MAGENTA; }
			case 1 -> { return Color.GREEN; }
			case 2 -> { return Color.YELLOW; }
			case 3 -> { return Color.BLUE; }
			case 4 -> { return Color.RED; }
			case 5 -> { return Color.ORANGE; }
			default -> { return Color.WHITE; }
		}
	}
}

Resetting Foreground Colors

The resetForegroundColors() method can be used to reset the foreground color of every tile on a VPanel.


public class ExampleD {
	public static void main(final String[] args) {
		try {
			UIManager.setLookAndFeel(VTerminalLookAndFeel.getInstance(24));
		} catch (final UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(() -> {
			final var frame = new VFrame(40, 20);
			frame.setVisible(true);
			frame.pack();
			frame.setLocationRelativeTo(null);

			final var panel = frame.getContentPane();
			for (int y = 0 ; y < panel.getHeightInTiles() ; y++) {
				for (int x = 0 ; x < panel.getWidthInTiles() ; x++) {
					panel.setCodePointAt(x, y, getRandomCodePoint());
					panel.setForegroundAt(x, y, getRandomColor());
				}
			}

			Executors.newSingleThreadScheduledExecutor().schedule(() -> {
				panel.resetForegroundColors();
				SwingUtilities.invokeLater(panel::repaint);
			}, 2, TimeUnit.SECONDS);
		});
	}

	private static int getRandomCodePoint() {
		return ThreadLocalRandom.current().nextInt(33, 127);
	}

	private static Color getRandomColor() {
		switch (ThreadLocalRandom.current().nextInt(0, 6)) {
			case 0 -> { return Color.MAGENTA; }
			case 1 -> { return Color.GREEN; }
			case 2 -> { return Color.YELLOW; }
			case 3 -> { return Color.BLUE; }
			case 4 -> { return Color.RED; }
			case 5 -> { return Color.ORANGE; }
			default -> { return Color.WHITE; }
		}
	}
}

Resetting Sequential Image Operations

The resetSequentialImageOps() method can be used to reset the sequential image operation of every tile on a VPanel.

Please do not attempt to run the following example as it requires the image filters by JH Labs, and the code will fail without this dependency.


public class ExampleE {
	public static void main(final String[] args) {
		try {
			UIManager.setLookAndFeel(VTerminalLookAndFeel.getInstance(24));
		} catch (final UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(() -> {
			final var frame = new VFrame(40, 20);
			frame.setVisible(true);
			frame.pack();
			frame.setLocationRelativeTo(null);

			final var panel = frame.getContentPane();
			for (int y = 0 ; y < panel.getHeightInTiles() ; y++) {
				for (int x = 0 ; x < panel.getWidthInTiles() ; x++) {
					panel.setCodePointAt(x, y, getRandomCodePoint());
					panel.setSequentialImageOpAt(x, y, new SequentialOp(new GaussianFilter(3f)));
				}
			}

			Executors.newSingleThreadScheduledExecutor().schedule(() -> {
				panel.resetSequentialImageOps();
				SwingUtilities.invokeLater(panel::repaint);
			}, 2, TimeUnit.SECONDS);
		});
	}

	private static int getRandomCodePoint() {
		return ThreadLocalRandom.current().nextInt(33, 127);
	}
}

Resetting Everything

The reset() method can be used to reset the code points, colors, and sequential image operations of every tile on a VPanel.

Please do not attempt to run the following example as it requires the image filters by JH Labs, and the code will fail without this dependency.


public class ExampleF {
	public static void main(final String[] args) {
		try {
			UIManager.setLookAndFeel(VTerminalLookAndFeel.getInstance(24));
		} catch (final UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(() -> {
			final var frame = new VFrame(40, 20);
			frame.setVisible(true);
			frame.pack();
			frame.setLocationRelativeTo(null);

			final var panel = frame.getContentPane();
			for (int y = 0 ; y < panel.getHeightInTiles() ; y++) {
				for (int x = 0 ; x < panel.getWidthInTiles() ; x++) {
					panel.setCodePointAt(x, y, getRandomCodePoint());
					panel.setBackgroundAt(x, y, getRandomColor());
					panel.setForegroundAt(x, y, getRandomColor());
					panel.setSequentialImageOpAt(x, y, new SequentialOp(new GaussianFilter(3f)));
				}
			}

			Executors.newSingleThreadScheduledExecutor().schedule(() -> {
				panel.reset();
				SwingUtilities.invokeLater(panel::repaint);
			}, 2, TimeUnit.SECONDS);
		});
	}

	private static int getRandomCodePoint() {
		return ThreadLocalRandom.current().nextInt(33, 127);
	}

	private static Color getRandomColor() {
		switch (ThreadLocalRandom.current().nextInt(0, 6)) {
			case 0 -> { return Color.MAGENTA; }
			case 1 -> { return Color.GREEN; }
			case 2 -> { return Color.YELLOW; }
			case 3 -> { return Color.BLUE; }
			case 4 -> { return Color.RED; }
			case 5 -> { return Color.ORANGE; }
			default -> { return Color.WHITE; }
		}
	}
}

Further Reading