Wacky Jacky Posted June 21 Share Posted June 21 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. 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 ^^ 5 Quote Link to comment Share on other sites More sharing options...
Czar Posted June 21 Share Posted June 21 Awesome good work mate! 1 Quote Link to comment Share on other sites More sharing options...
adumon Posted June 22 Share Posted June 22 Looks cool, well done! 1 Quote Link to comment Share on other sites More sharing options...