Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-07-29 18:09:19, viewed 127 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506  
package net.indiespot.script.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import java.util.LinkedList;

public class CssTextPane {
   public static void main(String[] args) {
      CssTextPaneTest.main(args);
   }

   private static class SyntaxElement {
      Pattern pattern;
      int group;
      String styleName;

      public SyntaxElement(String styleName, String regex, int group, boolean multiline) {
         this.pattern = Pattern.compile(regex, multiline ? Pattern.MULTILINE : 0);
         this.group = group;
         this.styleName = styleName;
      }
   }

   private final StyledDocument doc;
   private final Style root;
   final JTextPane textPane;
   final List<SyntaxElement> elements;

   public CssTextPane() {
      this.textPane = new JTextPane();
      this.doc = textPane.getStyledDocument();
      this.root = doc.addStyle("root", StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
      this.elements = new ArrayList<SyntaxElement>();
   }

   public void addSyntaxElement(String styleName, String pattern, int matchGroup) {
      this.elements.add(new SyntaxElement(styleName, pattern, matchGroup, false));
   }

   public JTextPane getTextPane() {
      return this.textPane;
   }

   public JScrollPane wrapInScrollPane(int w, int h) {
      JScrollPane scroller = new JScrollPane(this.textPane);
      scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
      scroller.setPreferredSize(new Dimension(w, h));
      return scroller;
   }

   public void defineStyles(String css) {
      if((css = css.trim()).isEmpty()) {
         return;
      }

      for(String part : Text.split(css, '}')) {
         if(!(part = part.trim()).isEmpty()) {
            defineStyle(part + '}');
         }
      }
   }

   public Style defineStyle(String css) {
      return defineStyle(Text.before(css, '{').trim(), Text.between(css, "{", "}").trim());
   }

   public Style defineStyle(String name, String props) {
      Style style = null;

      if(name.equals("*")) {
         style = this.root;
      }
      else {
         style = this.doc.getStyle(name);

         if(style == null) {
            style = this.doc.addStyle(name, this.root);
         }
      }

      for(String part : Text.split(props, ';')) {
         part = part.trim();
         if(part.isEmpty())
            continue;

         String[] keyval = Text.splitPair(part, ":");
         if(keyval == null) {
            this.logInputIssue("invalid property definition: [" + part + "]");
            continue;
         }

         String key = keyval[0].trim();
         String val = keyval[1].trim();

         if(key.isEmpty() || val.isEmpty()) {
            this.logInputIssue("invalid property definition: [" + part + "]");
            continue;
         }

         setStyleProperty(style, key, val);
      }
      return style;
   }

   public void setStyleProperty(Style style, String property, String value) {
      if(property.equals("font-family")) {
         StyleConstants.setFontFamily(style, value);
      }
      else if(property.equals("font-size")) {
         StyleConstants.setFontSize(style, Integer.parseInt(Text.replace(value, "pt", "")));
      }
      else if(property.equals("font-weight")) {
         StyleConstants.setBold(style, value.equals("bold"));
      }
      else if(property.equals("font-style")) {
         StyleConstants.setItalic(style, value.equals("italic"));
      }
      else if(property.equals("color")) {
         Color color = parseColor(value);
         if(color != null)
            StyleConstants.setForeground(style, color);
      }
      else if(property.equals("background") || property.equals("background-color")) {
         Color color = parseColor(value);
         if(color != null)
            StyleConstants.setBackground(style, color);
      }
      else if(property.equals("text-decoration")) {
         StyleConstants.setUnderline(style, false);
         StyleConstants.setStrikeThrough(style, false);

         for(String part : Text.split(Text.removeDuplicates(value, ' '), ' ')) {
            if(part.equals("none"))
               continue;
            if(part.equals("underline"))
               StyleConstants.setUnderline(style, true);
            else if(part.equals("line-through"))
               StyleConstants.setStrikeThrough(style, true);
            else
               this.logInputIssue("unexpected style property: '" + property + "'");
         }
      }
      else {
         this.logInputIssue("unexpected style property: '" + property + "'");
      }
   }

   private Color parseColor(String value) {
      if(!value.startsWith("#")) {
         this.logInputIssue("failed to parse color: '" + value + "'");
         return null;
      }

      value = Text.after(value, "#");
      if(value.length() == 3) // "abc" => "aabbcc"
      {
         char[] hex = new char[6];
         for(int i = 0; i < hex.length; i++)
            hex[i] = value.charAt(i >> 1);
         value = new String(hex);
      }

      int rgba;
      try {
         rgba = Integer.parseInt(value, 16);
      }
      catch (NumberFormatException exc) {
         this.logInputIssue("failed to parse hex color: '#" + value + "'");
         return null;
      }
      return new Color(rgba, value.length() == 8);
   }

   private final void logInputIssue(String issue) {
      System.err.println(this.getClass().getSimpleName() + " :: " + issue);
   }

   private String getRange(int off, int end) {
      try {
         return textPane.getDocument().getText(off, end - off);
      }
      catch (BadLocationException e) {
         throw new IllegalStateException(e);
      }
   }

   public final void activateAutoRestyle() {
      this.textPane.getDocument().addDocumentListener(new DocumentListener() {

         @Override
         public void insertUpdate(DocumentEvent e) {
            this.onEdit(e, false);
         }

         @Override
         public void removeUpdate(DocumentEvent e) {
            this.onEdit(e, true);
         }

         @Override
         public void changedUpdate(DocumentEvent e) {
            // DO NOTHING!
         }

         private void onEdit(DocumentEvent e, boolean isRemove) {
            //long t0 = System.nanoTime();
            final int len = textPane.getDocument().getLength();

            //System.out.println(e.getOffset() + "/" + e.getLength());

            int bottom = 0;
            int off = e.getOffset();
            do {
               //System.out.println(off);
               int min = off;
               int end = off;

               while (min > bottom && !getRange(min, end).startsWith("\n")) {
                  min = Math.max(bottom, min - 1);
               }
               if(min > bottom) {
                  min += 1;
               }

               while (end < len && !getRange(min, end).endsWith("\n")) {
                  end = Math.min(len, end + 1);
               }
               if(end < len) {
                  end -= 1;
               }

               //System.out.println("[" + min + ".." + end + "]");
               if(min != end) {
                  //String text = getRange(min, end);
                  //System.out.println("[" + min + ".." + end + "/" + e.getOffset() + "+" + e.getLength() + "] " + text);
                  restyleRange(min, end);
               }

               if(isRemove || end + 1 >= e.getOffset() + e.getLength())
                  break;
               off = end + 1;
               bottom = end + 1;
            }
            while (true);

            //long t1 = System.nanoTime();
            //System.out.println((t1 - t0) / 1000 + "us");
         }
      });
   }

   public final DocumentListener activateUndoRedo() {
      final TextPaneUndo undoSupport = new TextPaneUndo(this.textPane);

      this.textPane.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
      this.textPane.getActionMap().put("Undo", new AbstractAction("Undo") {
         public void actionPerformed(ActionEvent evt) {
            undoSupport.undo();
         }
      });

      this.textPane.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
      this.textPane.getActionMap().put("Redo", new AbstractAction("Redo") {
         public void actionPerformed(ActionEvent evt) {
            undoSupport.redo();
         }
      });

      return undoSupport.register();
   }

   public final void restyleRange(final int off, final int end) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            final String range = getRange(off, end);

            clearStyles(off, end);
            applyStyleOnRange("root", off, end);

            for(SyntaxElement element : elements) {
               for(Matcher matcher = element.pattern.matcher(range); matcher.find();) {
                  applyStyleOnMatch(element.styleName, off, matcher, element.group);
               }
            }
         }
      });
   }

   public void clearStyles(int off, int end) {
      StyledDocument doc = this.textPane.getStyledDocument();
      Style style = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
      doc.setCharacterAttributes(off, end - off, style, true);
   }

   public void applyStyleOnRange(String styleName, int off, int end) {
      StyledDocument doc = this.textPane.getStyledDocument();
      doc.setCharacterAttributes(off, end - off, doc.getStyle(styleName), false);
   }

   public void applyStyleOnMatch(String styleName, int off, Matcher matcher) {
      this.applyStyleOnRange(styleName, off + matcher.start(), off + matcher.end());
   }

   public void applyStyleOnMatch(String styleName, int off, Matcher matcher, int group) {
      this.applyStyleOnRange(styleName, off + matcher.start(group), off + matcher.end(group));
   }
}

class CssTextPaneTest {
   public static void main(String[] args) {
      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
         e.printStackTrace();
      }

      final CssTextPane pane = new CssTextPane();

      pane.defineStyle("*        { color: #000; font-style: italic; font-size:12; font-family: courier; } ");
      pane.defineStyle("digit    { color: #00f; font-style: normal; } ");
      pane.defineStyle("address  { color: #c0c; font-style: normal; } ");
      pane.defineStyle("const    { color: #080; font-style: normal; } ");
      pane.defineStyle("labeluse { color: #c40; font-style: normal; } ");
      pane.defineStyle("labeldef { color: #808; font-style: normal; } ");
      pane.defineStyle("comment  { color: #088; font-style: italic; text-decoration: none } ");

      String pre = "^|\\s|\\G";
      String post = "\\s|//|$";

      if(false) {
         pane.addSyntaxElement("digit", "(" + pre + ")(\\d+)(" + post + ")", 2);
         pane.addSyntaxElement("address", "(" + pre + ")(@\\d+)(\\s|//|$)", 2);
         pane.addSyntaxElement("const", "(" + pre + ")(HLT|ADD|SUB|STA|LDA|BRA|BRZ|BRP|INP|OUT)(" + post + ")", 2);
         pane.addSyntaxElement("labeluse", "(" + pre + ")(:[a-zA-Z0-9]+)(" + post + ")", 2);
         pane.addSyntaxElement("labeldef", "(" + pre + ")([a-zA-Z0-9]+:)(" + post + ")", 2);
         pane.addSyntaxElement("comment", "(//.*$)", 1);
      }
      else {
         pane.addSyntaxElement("digit", "(" + pre + ")(\\d+)(" + post + ")", 2);
         pane.addSyntaxElement("labeluse", "(" + pre + ")(RAISE)(" + post + ")", 2);
         pane.addSyntaxElement("address", "(" + pre + ")(GOTO|CALL|HALT|BREAK|REPEAT|SLEEP|YIELD)(" + post + ")", 2);
         pane.addSyntaxElement("const", "(" + pre + ")(BEGIN|END|FUNCTION|IF|ELSE|TRAP)(" + post + ")", 2);
         pane.addSyntaxElement("comment", "(#.*$)", 1);
      }

      pane.activateUndoRedo();
      pane.activateAutoRestyle();

      JFrame frame = new JFrame("CSS");
      frame.getContentPane().setLayout(new BorderLayout());
      frame.getContentPane().add(pane.wrapInScrollPane(640, 480));
      frame.setResizable(true);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

class TextPaneUndo {
   final JTextPane textPane;
   private boolean isBusy;

   private final LinkedList<Snapshot> undoStack;
   private final LinkedList<Snapshot> redoStack;
   private final int maxHistory;

   public TextPaneUndo(JTextPane textPane) {
      this.textPane = textPane;

      this.maxHistory = 256;
      this.isBusy = false;

      this.redoStack = new LinkedList<Snapshot>();
      this.undoStack = new LinkedList<Snapshot>();
      this.undoStack.addFirst(new Snapshot(false));
   }

   public final DocumentListener register() {
      DocumentListener listener = new DocumentListener() {
         @Override
         public void insertUpdate(DocumentEvent e) {
            TextPaneUndo.this.onEvent(true);
         }

         @Override
         public void removeUpdate(DocumentEvent e) {
            TextPaneUndo.this.onEvent(false);
         }

         @Override
         public void changedUpdate(DocumentEvent e) {
            // DO NOTHING!
         }
      };

      this.textPane.getDocument().addDocumentListener(listener);

      return listener;
   }

   private class Snapshot {
      final String text;
      final int caret;

      public Snapshot(boolean isInsertEvent) {
         this.text = TextPaneUndo.this.textPane.getText();

         int pos = TextPaneUndo.this.textPane.getCaretPosition();
         int len = TextPaneUndo.this.textPane.getDocument().getLength();
         this.caret = Math.min(pos + (isInsertEvent ? 1 : 0), len);
      }

      public void restore() {
         TextPaneUndo.this.textPane.setText(this.text);
         TextPaneUndo.this.textPane.setCaretPosition(this.caret);
      }
   }

   public void onEvent(boolean isInsertEvent) {
      if(!this.isBusy) {
         redoStack.clear();
         undoStack.addFirst(new Snapshot(isInsertEvent));

         while (undoStack.size() > this.maxHistory) {
            undoStack.removeLast();
         }
      }
   }

   public final void undo() {
      this.isBusy = true;

      try {
         String curr = textPane.getText();
         Snapshot undo = null;
         while (!undoStack.isEmpty()) {
            undo = undoStack.removeFirst();
            if(redoStack.isEmpty() || !redoStack.getFirst().text.equals(undo.text))
               redoStack.addFirst(undo);
            if(!undo.text.equals(curr))
               break;
         }

         if(undo != null) {
            undo.restore();
         }

         // store new situation
         undoStack.addFirst(new Snapshot(false));
      }
      finally {
         this.isBusy = false;
      }
   }

   public final void redo() {
      this.isBusy = true;

      try {
         String curr = textPane.getText();
         Snapshot redo = null;
         while (!redoStack.isEmpty()) {
            redo = redoStack.removeFirst();
            if(undoStack.isEmpty() || !undoStack.getFirst().text.equals(redo.text))
               undoStack.addFirst(redo);
            if(!redo.text.equals(curr))
               break;
         }

         if(redo != null) {
            redo.restore();
         }
      }
      finally {
         this.isBusy = false;
      }
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267  
package net.indiespot.script.util;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Text {
   public static String ascii(byte[] data) {
      if(data == null)
         return null;
      char[] ca = new char[data.length];
      for(int i = 0; i < ca.length; i++)
         ca[i] = (char) data[i];
      return new String(ca);
   }

   public static byte[] ascii(String data) {
      if(data == null)
         return null;
      byte[] ba = new byte[data.length()];
      for(int i = 0; i < ba.length; i++)
         if(data.charAt(i) <= 0x00ff)
            ba[i] = (byte) data.charAt(i);
         else
            throw new IllegalStateException();
      return ba;
   }

   //

   private static final Charset utf8_charset = Charset.forName("utf-8");

   public static String utf8(byte[] data) {
      if(data == null)
         return null;
      return new String(data, utf8_charset);
   }

   public static byte[] utf8(String data) {
      if(data == null)
         return null;
      return data.getBytes(utf8_charset);
   }

   //

   public static String normalizeLines(String s) {
      s = replace(s, "\r\n", "\n");
      s = replace(s, "\r", "\n");
      return s;
   }

   public static String[] splitOnLines(String s) {
      return normalizeLines(s).split("\n");
   }

   public static String[] splitPair(String s, String find) {
      int io = s.indexOf(find);
      if(io == -1)
         return null;
      return new String[] { s.substring(0, io), s.substring(io + find.length()) };
   }

   public static final String LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
   public static final String UPPERCASE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   public static String randomText(int length, String chars, Random random) {
      char[] array = new char[length];
      for(int i = 0; i < array.length; i++)
         array[i] = chars.charAt(random.nextInt(chars.length()));
      return new String(array);
   }

   //
   private static final char[] hex_lookup_table = "0123456789abcdef".toCharArray();

   public static String toHex(byte[] data) {
      char[] hex = new char[data.length * 2];
      for(int i = 0, p = 0; i < data.length; i++) {
         hex[p++] = hex_lookup_table[(data[i] >> 4) & 0xF];
         hex[p++] = hex_lookup_table[(data[i] >> 0) & 0xF];
      }
      return new String(hex);
   }

   public static byte[] fromHex(String hex) {
      if(hex.length() % 2 == 1)
         throw new IllegalStateException();

      byte[] raw = new byte[hex.length() >> 1];
      for(int i = 0; i < hex.length(); i++) {
         char c = hex.charAt(i);

         int v;
         if(c >= '0' && c <= '9')
            v = (c - '0');
         else if(c >= 'a' && c <= 'f')
            v = (c - 'a') + 10;
         else if(c >= 'A' && c <= 'F')
            v = (c - 'A') + 10;
         else
            throw new IllegalStateException("char: " + c);

         raw[i >> 1] |= v << ((~i & 1) * 4);
      }
      return raw;
   }

   //

   public static String before(String value, char find) {
      return before(value, find, null);
   }

   public static String before(String value, char find, String orElse) {
      int io = value.indexOf(find);
      return (io == -1) ? orElse : value.substring(0, io);
   }

   public static String before(String value, String find) {
      return before(value, find, null);
   }

   public static String before(String value, String find, String orElse) {
      int io = value.indexOf(find);
      return (io == -1) ? orElse : value.substring(0, io);
   }

   public static String after(String value, char find) {
      return after(value, find, null);
   }

   public static String after(String value, char find, String orElse) {
      int io = value.indexOf(find);
      return (io == -1) ? orElse : value.substring(io + 1);
   }

   public static String after(String value, String find) {
      return after(value, find, null);
   }

   public static String after(String value, String find, String orElse) {
      int io = value.indexOf(find);
      return (io == -1) ? orElse : value.substring(io + find.length());
   }

   public static String removeLast(String value, char find) {
      int indexOf = value.lastIndexOf(find);
      if(indexOf != -1) {
         String before = value.substring(0, indexOf);
         String after = value.substring(indexOf + 1);
         value = before + after;
      }
      return value;
   }

   public static String trimToNull(String s) {
      if(s == null)
         return null;
      s = s.trim();
      if(s.isEmpty())
         return null;
      return s;
   }

   public static String between(String s, String after, String before) {
      return Text.before(Text.after(s, after), before);
   }

   // ----

   public static List<String> split(String s, char find) {
      List<String> list = new ArrayList<>();
      int off = 0;
      while (true) {
         int io = s.indexOf(find, off);
         if(io == -1)
            break;
         list.add(s.substring(off, io));
         off = io + 1;
      }
      list.add(s.substring(off));
      return list;
   }

   public static List<String> split(String s, String find) {
      List<String> list = new ArrayList<>();
      int off = 0;
      while (true) {
         int io = s.indexOf(find, off);
         if(io == -1)
            break;
         list.add(s.substring(off, io));
         off = io + find.length();
      }
      list.add(s.substring(off));
      return list;
   }

   public static String replace(String s, String find, String replace) {
      StringBuilder sb = new StringBuilder();
      int off = 0;
      while (true) {
         int io = s.indexOf(find, off);
         if(io == -1)
            break;
         sb.append(s.substring(off, io));
         sb.append(replace);
         off = io + find.length();
      }
      return sb.append(s.substring(off)).toString();
   }

   public static String removeDuplicates(String s, char c) {
      StringBuilder sb = new StringBuilder();
      boolean isAt = false;
      for(char x : s.toCharArray()) {
         if(x == c) {
            if(isAt)
               continue;
            isAt = true;
         }
         else {
            isAt = false;
         }
         sb.append(x);
      }
      return sb.toString();

   }

   // ----

   public static String dashesToCapitalize(String in) {
      StringBuilder sb = new StringBuilder();
      for(String s : in.split("-"))
         if(!s.isEmpty())
            sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1).toLowerCase());
      return sb.toString();
   }

   public static String dashesToCamelCase(String in) {
      StringBuilder sb = new StringBuilder();
      for(String s : in.split("-"))
         if(!s.isEmpty())
            if(sb.length() == 0)
               sb.append(s);
            else
               sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1).toLowerCase());
      return sb.toString();
   }

   public static String capitalsToDashes(String in) {
      StringBuilder sb = new StringBuilder();
      for(int i = 0; i < in.length(); i++) {
         char c = in.charAt(i);
         if(Character.isUpperCase(c)) {
            if(sb.length() > 0)
               sb.append('-');
            c = Character.toLowerCase(c);
         }
         sb.append(c);
      }
      return sb.toString();
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (56 views)
2014-12-09 22:41:13

BurntPizza (91 views)
2014-12-08 04:46:31

JscottyBieshaar (51 views)
2014-12-05 12:39:02

SHC (67 views)
2014-12-03 16:27:13

CopyableCougar4 (69 views)
2014-11-29 21:32:03

toopeicgaming1999 (129 views)
2014-11-26 15:22:04

toopeicgaming1999 (120 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!