Loading...
Logo
Processing Forum
Sorry for the trivial question,
but I found this .java source code

How can I execute this source code starting from Processing? 
I can copy the code inside a tab in the processing IDE and after?

Thanks for your help.


Replies(2)

Simple copy / paste of the relevant parts, removing stuff not useful or blocking:
Copy code
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.Shape;
  9. import java.awt.font.GlyphVector;
  10. import java.awt.geom.PathIterator;
  11. import java.awt.geom.Rectangle2D;
  12. import java.util.ArrayList;
  13. import java.util.Iterator;
  14. import java.util.List;
  15.     private static final char ASCII_START_CHAR = 32; // space
  16.     private static final char ASCII_END_CHAR = 126; // ~
  17.     private static final int FONT_HEIGHT = 400;
  18.     private Shape shape;
  19.     private final List<Glyph> glyphs = new ArrayList<Glyph>();
  20.     void setup() {
  21.         // Simple default font. Easy to describe, few points, fits well in memory
  22.         Font font = getFont().deriveFont(Font.PLAIN, FONT_HEIGHT);
  23.         // More complex font. space to z will probably not fit in memory. Need
  24.         // to select character span carefully
  25.         // File fontFile = new File("/Library/Fonts/Didot.ttc"); // Mac default location
  26.         // Font font = Font.createFont(Font.PLAIN, fontFile).deriveFont(Font.PLAIN, FONT_HEIGHT);
  27.         for (char i = ASCII_START_CHAR; i <= ASCII_END_CHAR; i++) {
  28.             String str = String.valueOf(i);
  29.             GlyphVector v = font.createGlyphVector(getFontMetrics(font).getFontRenderContext(), str);
  30.             shape = v.getOutline();
  31.             PathIterator pathIterator = shape.getPathIterator(null);
  32.             List<Segment> segments = new ArrayList<Segment>();
  33.             while (!pathIterator.isDone()) {
  34.                 double[] coords = new double[6];
  35.                 int type = pathIterator.currentSegment(coords);
  36.                 segments.add((new Segment(type, coords)));
  37.                 pathIterator.next();
  38.             }
  39.             Rectangle2D glyphBounds = v.getLogicalBounds();
  40.             int glyphWidth = (int) glyphBounds.getWidth();
  41.             glyphs.add(new Glyph(segments, glyphWidth));
  42.         }
  43.         StringBuilder sb = new StringBuilder();
  44.         sb.append("/* Font: " + font.getName() + " */\n");
  45.         sb.append("const int START_CHAR_VALUE = " + Integer.valueOf(ASCII_START_CHAR) + ";\n");
  46.         int maxNofVals = 0;
  47.         for (int i = 0; i < glyphs.size(); i++) {
  48.             Glyph glyph = glyphs.get(i);
  49.             sb.append("/* " + String.valueOf((char) (ASCII_START_CHAR + i)) + " */ int char_" + i + "[] PROGMEM = { ");
  50.             Iterator<Segment> segmentIterator = glyph.getSegments().iterator();
  51.             int nofVals = 0;
  52.             sb.append(glyph.getWidth() + ", ");
  53.             StringBuilder coordsSb = new StringBuilder();
  54.             while (segmentIterator.hasNext()) {
  55.                 Segment segment = (Segment) segmentIterator.next();
  56.                 double[] coords = segment.getCoords();
  57.                 if (segment.getType() == PathIterator.SEG_QUADTO) {
  58.                     coordsSb.append(segment.getType() + ", " + (int) coords[0] + ", " + ((int) -coords[1]) + ", "
  59.                             + (int) coords[2] + ", " + ((int) -coords[3]));
  60.                     nofVals += 5;
  61.                 } else if (segment.getType() == PathIterator.SEG_CLOSE) {
  62.                     coordsSb.append(segment.getType());
  63.                     nofVals += 1;
  64.                 } else {
  65.                     coordsSb.append(segment.getType() + ", " + (int) coords[0] + ", " + ((int) -coords[1]));
  66.                     nofVals += 3;
  67.                 }
  68.                 if (segmentIterator.hasNext()) {
  69.                     coordsSb.append(", ");
  70.                 }
  71.             }
  72.             sb.append((nofVals + 2));
  73.             if (nofVals > 0) {
  74.                 sb.append(", ");
  75.             }
  76.             sb.append(coordsSb);
  77.             sb.append("};\n");
  78.             if (nofVals > maxNofVals) {
  79.                 maxNofVals = nofVals;
  80.             }
  81.         }
  82.         int currentCharArraySize = maxNofVals + 2;
  83.         sb.append("const int CURR_CHAR_ARRAY_SIZE = " + currentCharArraySize + ";\n");
  84.         sb.append("int curr_char[CURR_CHAR_ARRAY_SIZE];\n");
  85.         sb.append("PROGMEM const int* chars[] = {");
  86.         for (int i = 0; i < glyphs.size(); i++) {
  87.             sb.append("char_" + i);
  88.             if (i < glyphs.size() - 1) {
  89.                 sb.append(", ");
  90.             }
  91.         }
  92.         sb.append("};");
  93.         System.out.println(sb.toString());
  94.     }
  95.     private class Glyph {
  96.         private final int width;
  97.         private final List<Segment> segments;
  98.         public Glyph(List<Segment> segments, int width) {
  99.             this.segments = segments;
  100.             this.width = width;
  101.         }
  102.         public List<Segment> getSegments() {
  103.             return segments;
  104.         }
  105.         public int getWidth() {
  106.             return width;
  107.         }
  108.     }
  109.     private class Segment {
  110.         private final int type;
  111.         private final double[] coords;
  112.         public Segment(int action, double[] coords) {
  113.             this.type = action;
  114.             this.coords = coords;
  115.         }
  116.         public int getType() {
  117.             return type;
  118.         }
  119.         public double[] getCoords() {
  120.             return coords;
  121.         }
  122.     }

Amazing!!!! 
It works very very well. Thanks!!