[Util] Text color formatter

Discussion in 'Resources' started by TomFromCollege, Jun 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    TomFromCollege

    Earlier today I accidentally deleted the config for my plugin, this config contained MANY strings that were formatted and colored, and as it turned out, replace them 1 by 1 was taking me a really long time!

    So I created this:
    [​IMG]

    It's ridiculously simple and possibly one of the most horrible things I've ever written, but it does the job!

    I see that the Upload a File button restricts Jar files, so instead I'll post a link to my Dropbox.
    If this is against the rules, I'll take it down straight away but just for the record, here is the source to it aswell:
    Code:java
    1. /*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package com.inlustra;
    6.  
    7. import java.awt.Color;
    8. import java.awt.Dimension;
    9. import java.awt.FlowLayout;
    10. import java.awt.Font;
    11. import java.awt.FontFormatException;
    12. import java.awt.Graphics;
    13. import java.awt.GraphicsEnvironment;
    14. import java.awt.HeadlessException;
    15. import java.awt.Insets;
    16. import java.awt.event.ActionEvent;
    17. import java.awt.event.ActionListener;
    18. import java.awt.event.KeyAdapter;
    19. import java.awt.event.KeyEvent;
    20. import java.awt.event.WindowAdapter;
    21. import java.awt.event.WindowEvent;
    22. import java.awt.event.WindowListener;
    23. import java.awt.image.BufferedImage;
    24. import java.io.IOException;
    25. import java.util.logging.Level;
    26. import java.util.logging.Logger;
    27. import javax.imageio.ImageIO;
    28. import javax.swing.Box;
    29. import javax.swing.JButton;
    30. import javax.swing.JEditorPane;
    31. import javax.swing.JFrame;
    32. import javax.swing.JTextPane;
    33. import javax.swing.text.html.HTMLDocument;
    34.  
    35. /**
    36.  *
    37.  * @author Thomas
    38.  */
    39. public class MinecraftTextFormatter extends JFrame implements WindowListener {
    40.  
    41. private JEditorPane formatter;
    42. private ImageTextPane formatted;
    43. private Font mcFont;
    44.  
    45. public static void main(String[] args) {
    46. MinecraftTextFormatter minecraftTextFormatter = new MinecraftTextFormatter("Minecraft Text Formatter by TomFromCollege");
    47. }
    48.  
    49. public MinecraftTextFormatter(String title) throws HeadlessException {
    50. super(title);
    51. this.setLayout(new FlowLayout());
    52. this.addWindowListener(this);
    53.  
    54. formatter = new JEditorPane();
    55. try {
    56. formatted = new ImageTextPane(ImageIO.read(this.getClass().getResourceAsStream("img.jpg")));
    57. } catch (IOException ex) {
    58. Logger.getLogger(MinecraftTextFormatter.class.getName()).log(Level.SEVERE, null, ex);
    59. }
    60.  
    61. formatter.setPreferredSize(new Dimension(600, 60));
    62. formatted.setContentType("text/html");
    63.  
    64. try {
    65. final Font f = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font.ttf"));
    66. mcFont = f;
    67. final String bodyRule = "body { font-family: " + f.getFamily() + "; "
    68. + "font-size: " + 16 + "pt; "
    69. + "color: #ffffff;"
    70. + "text-shadow: 0.1em 0.1em 0.2em black; }";
    71. ((HTMLDocument) formatted.getDocument()).getStyleSheet().addRule(bodyRule);
    72. GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f);
    73. formatted.setFont(f.deriveFont(Font.PLAIN, 20));
    74. formatter.addKeyListener(new KeyAdapter() {
    75. @Override
    76. public void keyReleased(KeyEvent e) {
    77. formatted.setText(formatText(formatter.getText()));
    78. }
    79. });
    80. Logger.getLogger(MinecraftTextFormatter.class.getName()).log(Level.SEVERE, null, ex);
    81. }
    82.  
    83. formatted.setPreferredSize(new Dimension(600, 60));
    84.  
    85. this.add(formatColorButton("§4", new Color(0xbe0000), true));
    86. this.add(formatColorButton("§c", new Color(0xfe3f3f)));
    87. this.add(formatColorButton("§6", new Color(0xD9A334)));
    88. this.add(formatColorButton("§e", new Color(0xfefe3f)));
    89. this.add(formatColorButton("§2", new Color(0x00be00)));
    90. this.add(formatColorButton("§a", new Color(0x3ffe3f)));
    91. this.add(formatColorButton("§b", new Color(0x3ffefe)));
    92. this.add(formatColorButton("§3", new Color(0x00bebe)));
    93. this.add(formatColorButton("§1", new Color(0x0000be), true));
    94. this.add(formatColorButton("§9", new Color(0x3f3ffe)));
    95. this.add(formatColorButton("§d", new Color(0xfe3ffe)));
    96. this.add(formatColorButton("§5", new Color(0xbe00be)));
    97. this.add(formatColorButton("§f", new Color(0xffffff)));
    98. this.add(formatColorButton("§7", new Color(0xbebebe)));
    99. this.add(formatColorButton("§8", new Color(0x3f3f3f), true));
    100. this.add(formatColorButton("§0", new Color(0x000000), true));
    101. this.add(Box.createRigidArea(new Dimension(20, 15)));
    102. this.add(formatButton("§n"));
    103. this.add(formatButton("§l"));
    104. this.add(formatButton("§m"));
    105. this.add(formatButton("§o"));
    106. this.add(formatter);
    107. this.add(formatted);
    108. formatted.setEditable(false);
    109. this.setSize(625, 200);
    110. this.setBackground(Color.white);
    111. this.setVisible(true);
    112. formatter.grabFocus();
    113. }
    114.  
    115. private JButton formatButton(final String insert) {
    116. JButton btn = formatColorButton(insert, new Color(0xbebebe));
    117. if (insert.contains("n")) {
    118. btn.setText("<html><u>" + btn.getText() + "</u></html>");
    119. } else if (insert.contains("l")) {
    120. btn.setText("<html><b>" + btn.getText() + "</b></html>");
    121. } else if (insert.contains("m")) {
    122. btn.setText("<html><s>" + btn.getText() + "</s></html>");
    123. } else if (insert.contains("o")) {
    124. btn.setText("<html><i>" + btn.getText() + "</i></html>");
    125. }
    126. return btn;
    127. }
    128.  
    129. private JButton formatColorButton(final String insert, Color c) {
    130. JButton red = new JButton(insert);
    131. red.addActionListener(new ActionListener() {
    132. @Override
    133. public void actionPerformed(ActionEvent e) {
    134. String text = formatter.getText();
    135. int caretpos = formatter.getCaretPosition();
    136. text = text.substring(0, caretpos) + insert + text.substring(caretpos);
    137. formatter.setText(text);
    138. formatter.setCaretPosition(caretpos + 2);
    139. formatted.setText(formatText(formatter.getText()));
    140. formatter.grabFocus();
    141. }
    142. });
    143. red.setBackground(c);
    144. red.setFont(red.getFont().deriveFont(10f));
    145. red.setMargin(new Insets(0, 0, 0, 0));
    146. red.setBorder(null);
    147. red.setPreferredSize(new Dimension(20, 15));
    148. return red;
    149. }
    150.  
    151. private JButton formatColorButton(final String insert, Color c, boolean whiteText) {
    152. JButton btn = formatColorButton(insert, c);
    153. if (whiteText) {
    154. btn.setForeground(Color.white);
    155. }
    156. return btn;
    157. }
    158.  
    159. private String formatText(String text) {
    160. text = text.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    161. try {
    162. StringBuilder sb = new StringBuilder();
    163. sb.append("<font face='Minecraft'>");
    164. if (text.contains("§")) {
    165. String[] split = text.split("§");
    166. String splitStr;
    167. for (int i = 0; i < split.length; i++) {
    168. splitStr = split[i];
    169. if (i != 0) {
    170. String currentString = splitStr.substring(1, splitStr.length());
    171. System.out.println(currentString);
    172. sb.append(formatCode(splitStr.charAt(0))).append(currentString);
    173. } else {
    174. sb.append(splitStr);
    175. }
    176. }
    177. sb.append("</font>");
    178. return sb.toString();
    179. } else {
    180. return text;
    181. }
    182. } catch (Exception e) {
    183. return text;
    184. }
    185. }
    186.  
    187. private String formatCode(char format) {
    188. System.out.println("formatting: " + format);
    189. switch (format) {
    190. case '4':
    191. return "</b></u></i></s><font color='#be0000'>";
    192. case 'c':
    193. return "</b></u></i></s><font color='#fe3f3f'>";
    194. case '6':
    195. return "</b></u></i></s><font color='#D9A334'>";
    196. case 'e':
    197. return "</b></u></i></s><font color='#fefe3f'>";
    198. case '2':
    199. return "</b></u></i></s><font color='#00be00'>";
    200. case 'a':
    201. return "</b></u></i></s><font color='#3ffe3f'>";
    202. case 'b':
    203. return "</b></u></i></s><font color='#3ffefe'>";
    204. case '3':
    205. return "</b></u></i></s><font color='#00bebe'>";
    206. case '1':
    207. return "</b></u></i></s><font color='#0000be'>";
    208. case '9':
    209. return "</b></u></i></s><font color='#3f3ffe'>";
    210. case 'd':
    211. return "</b></u></i></s><font color='#fe3ffe'>";
    212. case '5':
    213. return "</b></u></i></s><font color='#be00be'>";
    214. case 'f':
    215. return "</b></u></i></s><font color='#ffffff'>";
    216. case '7':
    217. return "</b></u></i></s><font color='#bebebe'>";
    218. case '8':
    219. return "</b></u></i></s><font color='#3f3f3f'>";
    220. case '0':
    221. return "</b></u></i></s><font color='#000000'>";
    222.  
    223. case 'n':
    224. return "</b><u></i></s>";
    225. case 'l':
    226. return "<b></u></i></s>";
    227. case 'm':
    228. return "<b></u></i><s>";
    229. case 'o':
    230. return "</b></u><i></s>";
    231. }
    232. return "";
    233. }
    234.  
    235. @Override
    236. public void windowOpened(WindowEvent e) {
    237. }
    238.  
    239. @Override
    240. public void windowClosing(WindowEvent e) {
    241. }
    242.  
    243. @Override
    244. public void windowClosed(WindowEvent e) {
    245. System.exit(0);
    246. }
    247.  
    248. @Override
    249. public void windowIconified(WindowEvent e) {
    250. }
    251.  
    252. @Override
    253. public void windowDeiconified(WindowEvent e) {
    254. }
    255.  
    256. @Override
    257. public void windowActivated(WindowEvent e) {
    258. }
    259.  
    260. @Override
    261. public void windowDeactivated(WindowEvent e) {
    262. }
    263.  
    264. private static class ImageTextPane extends JTextPane {
    265.  
    266.  
    267. public ImageTextPane(BufferedImage img) {
    268. super();
    269. this.img = img;
    270. setOpaque(false);
    271. setBackground(new Color(0, 0, 0, 0));
    272. }
    273.  
    274. @Override
    275. protected void paintComponent(Graphics g) {
    276. g.drawImage(img, 0, 0, this);
    277. super.paintComponent(g);
    278. }
    279. }
    280. }[I][/I][/i]
     
    TheE, AstramG, zachoooo and 8 others like this.
  2. Offline

    hawkfalcon

    I love it.
     
  3. Offline

    kreashenz

    Other than the retarded BBCode in the syntax blocks, bloody great!
     
  4. Offline

    TomFromCollege

    kreashenz It was the easiest and quickest way, F*ck up the HTML! :D

    Edit:
    OH, WHAT. Why's it doing that!? :p
    Ok, I think I fixed it, but for some reason there are still a few tags at the end of the code, just remove them if you copy the code :)
     
  5. Offline

    Minecrell

    TomFromCollege
    Looks really nice :p
    Stupid question: Where did you get the hex values for the color codes? :oops:
     
  6. Offline

    TomFromCollege

    The wiki! I just right clicked - View source :D
     
  7. Offline

    DSH105

    Simply fantastic. Love it :)
     
  8. Offline

    chasechocolate

    Love it. Nice job :)
     
  9. Offline

    Minecrell

    Ah lol :p
     
  10. Offline

    TomFromCollege

    Small update to the colors ;) Barely even noticeable!
     
  11. Offline

    andrewabosh

    TomFromCollege This is insane man good job! Could you just fix the colors on the mac? (dosen't display properly)

    [​IMG]
     
  12. Offline

    TomFromCollege

    Fucking mac. I hate Mac, so fucking much. 'Scuse the language.
    I'll fix it eventually :p Check back periodically
     
    zachoooo likes this.
  13. Offline

    bob7

    Awesome! It's much more easy to test my strings now!
     
  14. Offline

    TomFromCollege

    andrewabosh
    I think I fixed it :) Go ahead and re-dl

    bob7
    Careful! It becomes a sort of habit after a while, I find the need to test them all the time! :p
     
  15. Offline

    andrewabosh

  16. Offline

    Minecrell

    Did you change the colors? Because at the moment red and gold look a little bit different in your program than ingame :p Are you still using the wiki colors?

    EDIT: The bold text doesn't work for me :/
     
  17. Offline

    TomFromCollege

    Weird! Yeah, still using the Wiki colours, I know they're a little bit off and actually like quit rubbish in the program but I can't find any more accurate colours...
     
  18. Offline

    Minecrell

    Well, I wrote a little website some weeks ago that is checking the server list and it should color the motd, so I needed the colors too. I created a little client mod that has displayed the colors when I entered them in the chat and I got the same results as the foreground colors in the wiki (the first color column) with it. This colors seemed correct for me. However if your source above is correct you are using different colors than them in the wiki? (For example, you have #fe3f3f for §c, the wiki says #FF5555) (Actually with white background I don't see any difference :p)

    I wasn't able to find the colors in your code above in the wiki.. :/
     
  19. Offline

    NLGamingBross

    Hey There This Looks Nice! But can you make an second window with the output to Java(Bukkit) With
    Code:java
    1. player.sendMessage(ChatColor.RED + "Example");


    I want to use it and i see directly the source line!
     
  20. Offline

    TomFromCollege

    I have literally no clue what you mean, if you're asking me to add "player.sendMessage" No sorry, that would be a rather pointless task
     
  21. Offline

    NLGamingBross


    When u write a collor text in your progam you see the "Message Ouput"And the javaq output you can use in you bukkit plugin
     
  22. Offline

    TomFromCollege

    But not everyone is using player.sendMessage, There's no way I'm going to create an output for something THAT vague. No offense but, I'm not adding it to save you 2 seconds of time :p
    Unless your referring to the ChatColor. In which case, no thanks. Waste of time, this works fine.
     
  23. Offline

    NLGamingBross


    Nope but i try now to make aan example.

    The program gives:
    Code:java
    1. player.sendMessage(ChatColor.RED + "This is an example");


    An i can change it to:
    Code:java
    1. p.sendMessage(ChatColor.RED + "This is an example");
     
  24. Offline

    TomFromCollege

    NLGamingBross

    I have no clue what you mean, the Program would give you:
    Code:
    §cThis is an example
    Where are you getting the whole sendMessage thing from!?!?
     
Thread Status:
Not open for further replies.

Share This Page