Jump to content

HScrollingLogger | Display messages dynamically and aesthetically.


Recommended Posts

Posted

HScrollingLogger Code:

public class HScrollingLogger {

	private static int messagesDisplayed = 5;
	private static String[] messages = new String[messagesDisplayed];
	private final static DateFormat dateFormat = new SimpleDateFormat("HH:mm a");
	private static int i = 0;

	private final static Color backgroundColor = new Color(50, 205, 50);
	private final static Color textColor = new Color(0, 0, 0);

	private final static Font FONT = new Font("Arial", 0, 11);

	public static void add(String s) {
		messages[i] = (s + " | " + (dateFormat.format(new Date())));
		i = (i + 1) % messagesDisplayed;
	}

	public static void paintLog(int x, int y, Graphics g) {
		FontMetrics metrics = g.getFontMetrics(FONT);
		int height = metrics.getHeight();
		int descent = metrics.getDescent() - 5;
		int index = i;
		for (int messageIndex = 0; messageIndex < messagesDisplayed; messageIndex++) {
			index = index == 0 ? messagesDisplayed - 1 : index - 1;
			String s = messages[index];
			if (s == null) {
				break;
			}
			g.setColor(backgroundColor);
			g.fillRect(x - metrics.stringWidth(s) - 10, y + descent - height, metrics.stringWidth(s) + 10, height);
			g.setColor(textColor);
			g.setFont(FONT);
			g.drawString(s, x - metrics.stringWidth(s) - 5, y - 5);
			y -= 15;
		}
	}

} 

 

Usage:

HScrollingLogger.add("Started Script.");
HScrollingLogger.add("Test message 1.");
HScrollingLogger.add("Test message 2.");
HScrollingLogger.add("Test message 3.");
HScrollingLogger.add("Test message 4.");

...

@ Override
public void onPaint(Graphics2D g) {
HScrollingLogger.paintLog(516, 340, g);
}

What it looks like ingame:

29194745ce7ca4a32da9450caf9c4082.png

  • Like 8

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...