Code completion.
Posted by kgowen on December 3, 2009
Some major changes have been made to the source code. I have gotten it down to one class. I have fixed the two-dimensionality which happens when the form is rotated. It has even been compacted down to about 85 lines (I would like to shave off at least 5 more).
There may be one more update with some stylistic changes, variable names, font choice, colors, etc. But functionally it will be the same.
Here is the newest rendition.
1 import processing.core.*;
2 import java.io.*;
3
4 public class HelloQuine extends PApplet {
5 // the size of the stage
6 private final int WIDTH = 1280, HEIGHT = 740;
7 // shape rotation values
8 private float zRotation = 0, xRotation = 0, yRotation = -1;
9 // shape translation values
10 private float xMove = WIDTH/2, yMove = HEIGHT/4, zMove = HEIGHT/4;
11 // structures the characters to display
12 char[][] charMatrix; // in the form char[tall][across]
13 File sourceFile;
14
15 /** Do everything that should be done before objects are drawn. **/
16 public void setup() {
17 size(WIDTH, HEIGHT, P3D); // the size of the stage
18 lights(); // give the stage some ambient light
19 textFont(loadFont("Univers66.vlw.gz"), 8);
20
21 sourceFile = new File("C:\\Users\\Trip\\Desktop\\eclipse\\" +
22 "workspace\\HelloQuine\\src\\HelloQuine.java");
23 try {
24 charMatrix = new char[countLines()][80];
25 for (int x = 0; x < charMatrix.length; x++) {
26 for (int y = 0; y < charMatrix[0].length; y++) {
27 charMatrix[x][y] = ‘ ‘;
28 }
29 }
30 loadChars();
31 } catch (IOException e) {}
32 }
33
34 /** Repeatedly called to keep drawing to the screen. **/
35 public void draw() {
36 background(0,0,0); // the background color in RGB
37 pushMatrix(); // start developing a shape
38 translate(xMove, yMove, zMove);
39 rotateX(xRotation); rotateY(yRotation); rotateZ(zRotation);
40 drawCylinder(100, 80);
41 popMatrix(); // draw the shape to the screen
42 }
43
44 /** Draw a cylinder of the specified dimensions to the screen. **/
45 public void drawCylinder(float radius, float sides) {
46 float angle = 180;
47 float angleIncrement = radians(360 / sides);
48
49 // draw letters to the screen
50 fill(0×88, 0×99, 0xFF);
51 for (int j = 0; j < charMatrix.length; j++, angle = 180) { // rows
52 for (int i = 0; i < sides; i++) { // letters
53 beginShape();
54 text(charMatrix[j][i], 0, j * 9, radius);
55 rotateY(angleIncrement);
56 endShape();
57 angle -= angleIncrement;
58 }
59 }
60 }
61
62 /** Load characters from the source file into the matrix. **/
63 private void loadChars() throws IOException {
64 BufferedReader in = new BufferedReader(new FileReader(sourceFile));
65 String line;
66 char[] chars;
67 int lineCount = 0;
68
69 while ((line = in.readLine()) != null) {
70 chars = line.toCharArray();
71 for (int i = 0; i < chars.length; i++) {
72 charMatrix[lineCount][i] = chars[i];
73 }
74 lineCount++;
75 }
76 }
77
78 /** Count the lines in the source file. **/
79 private int countLines() throws IOException {
80 int lineCount = 0;
81 BufferedReader in = new BufferedReader(new FileReader(sourceFile));
82
83 while ((in.readLine()) != null) { lineCount++; }
84 return lineCount;
85 }
86 }
Images of Code Completions « Hello Quine said
[...] Comments (RSS) « Code completion. [...]