Jump to content

Scrolling text bar for the onpaint


Recommended Posts

Posted

I was bored, so I made this, hope some can enjoy it. It just gives you the option to have any text in a box appearing and disappearing while scrolling along.  Feel free to optimize or edit and use it.

gr2ur8E.gifGt0M83u.gif

1. Copy this class to your project

Spoiler
package org.wackylib.paint;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class ScrollingTextBar {
    private String text = "place holder";
    private int textXPosition;
    private int borderXPosition;
    private int yPosition;
    private int width;
    private int height;
    private int scrollSpeed;
    private Color textColor = Color.WHITE;
    private Color borderColor = Color.BLACK;
    private boolean drawBorder;

    public ScrollingTextBar(String text, int xPosition, int yPosition, int width, int height, int scrollSpeed) {
        this.text = text;
        this.borderXPosition = xPosition;
        this.textXPosition = xPosition + width; //Starts text from the right side of the border
        this.yPosition = yPosition;
        this.width = width;
        this.height = height;
        this.scrollSpeed = scrollSpeed;
    }

    public ScrollingTextBar(int xPosition, int yPosition, int width, int height, int scrollSpeed) {
        this.borderXPosition = xPosition;
        this.textXPosition = xPosition + width; //Starts text from the right side of the border
        this.yPosition = yPosition;
        this.width = width;
        this.height = height;
        this.scrollSpeed = scrollSpeed;
    }

    public void update() {
        textXPosition -= scrollSpeed;
        if (textXPosition + getTextWidth() < borderXPosition) {
            textXPosition = borderXPosition + width;
        }
    }

    public void draw(Graphics g) {
        if (drawBorder) {
            //Draw border if the flag is true
            g.setColor(borderColor);
            g.drawRect(borderXPosition, yPosition, width, height);
        }

        //Set clipping to the area of the border, this makes the text seem appearing and disappearing.
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setClip(new Rectangle(borderXPosition, yPosition, width, height));

        //Draw text within the clipping region
        g2d.setColor(textColor);
        FontMetrics fm = g2d.getFontMetrics();
        int textYPosition = yPosition + ((height - fm.getHeight()) / 2) + fm.getAscent();
        g2d.drawString(text, textXPosition, textYPosition);

        //Dispose the graphics context to avoid memory leaks
        g2d.dispose();
    }

    private int getTextWidth() {
        Graphics g = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB).getGraphics();
        FontMetrics fm = g.getFontMetrics();
        return fm.stringWidth(text);
    }

    public void setTextColor(Color textColor) {
        this.textColor = textColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setDrawBorder(boolean drawBorder) {
        this.drawBorder = drawBorder;
    }
}


2. Get an instance by importing it.
3. In the onstart decide how you want to make it look, use the 4 setters to customize: text color, border color, set your custom text string, draw border yes or no.

Spoiler
scrollingTextBar = new ScrollingTextBar(365, 440, 130, 20, 1); //
scrollingTextBar.setTextColor(Color.green);
scrollingTextBar.setBorderColor(Color.black);
scrollingTextBar.setDrawBorder(false);
scrollingTextBar.setText("your text here");

Optional: 
set the text with the constructor instead of the setter ^^
 

  • Like 5

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...