Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.risenphoenix.jnk.StarField.Popups;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.util.ArrayList;
- import net.risenphoenix.jnk.StarField.INIT;
- public class Popup {
- private String title;
- private String body;
- private int width;
- private int height;
- private FontMetrics fm;
- private int margin = 30;
- private String exit = "Press ESCAPE to close this popup...";
- private ArrayList<String> bodyLines = new ArrayList<String>();
- private boolean hasCalculated;
- public Popup(String title, String body, int width, int height) {
- this.title = title;
- this.body = body;
- this.width = width;
- this.height = height;
- }
- public void drawPopup(Graphics g) {
- // Popup Body
- g.setColor(new Color(0,0,0,180));
- g.fillRect((INIT.getFrameSize().width / 2) - (width / 2), 100, width, height);
- // Popup Border
- g.setColor(new Color(0,0,0,255));
- g.drawRect((INIT.getFrameSize().width / 2) - (width / 2), 100, width, height);
- // Text Title
- g.setColor(Color.WHITE);
- this.fm = g.getFontMetrics();
- if (fm.stringWidth(title) < (width - (margin * 2))) {
- g.drawString(title, (INIT.getFrameSize().width / 2) - ((width / 2) - margin), 140);
- } else {
- g.drawString("Title too large to display...", (INIT.getFrameSize().width / 2) - ((width / 2) - margin), 140);
- }
- g.drawLine((INIT.getFrameSize().width / 2) - ((width / 2) - margin), 155, (INIT.getFrameSize().width / 2) + ((width / 2) - margin), 155);
- // Text Body
- g.setFont(new Font("Century Gothic", Font.PLAIN, 20));
- this.fm = g.getFontMetrics();
- if (!this.hasCalculated) {
- StringBuilder line = new StringBuilder();
- StringBuilder word = new StringBuilder();
- StringBuilder toCheck = new StringBuilder();
- // Line Truncating
- for (int i = 0; i < body.length(); i++) {
- if (body.charAt(i) != ' ') {
- word.append(body.charAt(i));
- } else if (body.charAt(i) == ' ') {
- word.append(body.charAt(i));
- toCheck.append(word.toString());
- if (fm.stringWidth(toCheck.toString()) < this.width - (margin * 2)) {
- line.append(word.toString());
- word = new StringBuilder();
- } else {
- bodyLines.add(line.toString());
- line = new StringBuilder();
- toCheck = new StringBuilder();
- line.append(word.toString());
- toCheck.append(word.toString());
- word = new StringBuilder();
- }
- }
- }
- this.bodyLines.add(line.toString());
- this.hasCalculated = true;
- }
- int yPos = 180;
- for(String s:bodyLines) {
- g.drawString(s, (INIT.getFrameSize().width / 2) - ((width / 2) - margin), yPos);
- yPos += 20;
- }
- // Exit Message
- g.setFont(new Font("Century Gothic", Font.BOLD, 16));
- this.fm = g.getFontMetrics();
- g.drawString(this.exit, (INIT.getFrameSize().width / 2) - (fm.stringWidth(exit) / 2), (this.height + 100) - 30);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement