Advertisement
Jnk1296

Popup

May 23rd, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package net.risenphoenix.jnk.StarField.Popups;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.util.ArrayList;
  8.  
  9. import net.risenphoenix.jnk.StarField.INIT;
  10.  
  11. public class Popup {
  12.  
  13.     private String title;
  14.     private String body;
  15.     private int width;
  16.     private int height;
  17.     private FontMetrics fm;
  18.    
  19.     private int margin = 30;
  20.    
  21.     private String exit = "Press ESCAPE to close this popup...";
  22.    
  23.     private ArrayList<String> bodyLines = new ArrayList<String>();
  24.    
  25.     private boolean hasCalculated;
  26.    
  27.     public Popup(String title, String body, int width, int height) {
  28.         this.title = title;
  29.         this.body = body;
  30.         this.width = width;
  31.         this.height = height;
  32.     }
  33.    
  34.     public void drawPopup(Graphics g) {
  35.         // Popup Body
  36.         g.setColor(new Color(0,0,0,180));
  37.         g.fillRect((INIT.getFrameSize().width / 2) - (width / 2), 100, width, height);
  38.        
  39.         // Popup Border
  40.         g.setColor(new Color(0,0,0,255));
  41.         g.drawRect((INIT.getFrameSize().width / 2) - (width / 2), 100, width, height);
  42.        
  43.         // Text Title
  44.         g.setColor(Color.WHITE);
  45.         this.fm = g.getFontMetrics();
  46.        
  47.         if (fm.stringWidth(title) < (width - (margin * 2))) {
  48.             g.drawString(title, (INIT.getFrameSize().width / 2) - ((width / 2) - margin), 140);
  49.         } else {
  50.             g.drawString("Title too large to display...", (INIT.getFrameSize().width / 2) - ((width / 2) - margin), 140);
  51.         }
  52.        
  53.         g.drawLine((INIT.getFrameSize().width / 2) - ((width / 2) - margin), 155, (INIT.getFrameSize().width / 2) + ((width / 2) - margin), 155);
  54.        
  55.         // Text Body
  56.         g.setFont(new Font("Century Gothic", Font.PLAIN, 20));
  57.         this.fm = g.getFontMetrics();
  58.        
  59.         if (!this.hasCalculated) {
  60.             StringBuilder line = new StringBuilder();
  61.             StringBuilder word = new StringBuilder();
  62.             StringBuilder toCheck = new StringBuilder();
  63.            
  64.             // Line Truncating
  65.             for (int i = 0; i < body.length(); i++) {
  66.                 if (body.charAt(i) != ' ') {
  67.                     word.append(body.charAt(i));
  68.                 } else if (body.charAt(i) == ' ') {
  69.                     word.append(body.charAt(i));
  70.                     toCheck.append(word.toString());
  71.                    
  72.                     if (fm.stringWidth(toCheck.toString()) < this.width - (margin * 2)) {
  73.                         line.append(word.toString());
  74.                         word = new StringBuilder();
  75.                     } else {
  76.                         bodyLines.add(line.toString());
  77.                         line = new StringBuilder();
  78.                         toCheck = new StringBuilder();
  79.                         line.append(word.toString());
  80.                         toCheck.append(word.toString());
  81.                         word = new StringBuilder();
  82.                     }
  83.                 }
  84.             }
  85.            
  86.             this.bodyLines.add(line.toString());
  87.             this.hasCalculated = true;
  88.         }
  89.        
  90.         int yPos = 180;
  91.        
  92.         for(String s:bodyLines) {
  93.             g.drawString(s, (INIT.getFrameSize().width / 2) - ((width / 2) - margin), yPos);
  94.             yPos += 20;
  95.         }
  96.        
  97.         // Exit Message
  98.         g.setFont(new Font("Century Gothic", Font.BOLD, 16));
  99.         this.fm = g.getFontMetrics();
  100.        
  101.         g.drawString(this.exit, (INIT.getFrameSize().width / 2) - (fm.stringWidth(exit) / 2), (this.height + 100) - 30);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement