Jump to content

HScrollingLogger | Display messages dynamically and aesthetically.


House

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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