Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. // FractalGrammars.java
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class FractalGrammars extends Frame
  6. { public static void main(String[] args)
  7. { if (args.length == 0)
  8. System.out.println("Use filename as program argument.");
  9. else
  10. new FractalGrammars(args[0]);
  11. }
  12. FractalGrammars(String fileName)
  13. { super("Click left or right mouse button to change the level");
  14. addWindowListener(new WindowAdapter()
  15. {public void windowClosing(
  16. WindowEvent e){System.exit(0);}});
  17. setSize (800, 600);
  18. add("Center", new CvFractalGrammars(fileName));
  19. show();
  20. }
  21. }
  22.  
  23. class CvFractalGrammars extends Canvas
  24. { String fileName, axiom, strF, strf, strX, strY;
  25. int maxX, maxY, level = 1;
  26. double xLast, yLast, dir, rotation, dirStart, fxStart, fyStart,
  27. lengthFract, reductFact;
  28.  
  29. void error(String str)
  30. { System.out.println(str);
  31. System.exit(1);
  32. }
  33.  
  34. CvFractalGrammars(String fileName)
  35. { Input inp = new Input(fileName);
  36. if (inp.fails())
  37. error("Cannot open input file.");
  38.  
  39.  
  40. axiom = inp.readString(); inp.skipRest();
  41. strF = inp.readString(); inp.skipRest();
  42. strf = inp.readString(); inp.skipRest();
  43. strX = inp.readString(); inp.skipRest();
  44. strY = inp.readString(); inp.skipRest();
  45. rotation = inp.readFloat(); inp.skipRest();
  46. dirStart = inp.readFloat(); inp.skipRest();
  47. fxStart = inp.readFloat(); inp.skipRest();
  48. fyStart = inp.readFloat(); inp.skipRest();
  49. lengthFract = inp.readFloat(); inp.skipRest();
  50. reductFact = inp.readFloat();
  51. if (inp.fails())
  52. error("Input file incorrect.");
  53.  
  54. addMouseListener(new MouseAdapter()
  55. { public void mousePressed(MouseEvent evt)
  56. { if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
  57. { level--; // Right mouse button decreases level
  58. if (level < 1)
  59. level = 1;
  60. }
  61. else
  62. level++; // Left mouse button increases level
  63. repaint();
  64. }
  65. });
  66.  
  67. }
  68.  
  69. Graphics g;
  70. int iX(double x){return (int)Math.round(x);}
  71. int iY(double y){return (int)Math.round(maxY-y);}
  72.  
  73. void drawTo(Graphics g, double x, double y)
  74. { g.drawLine(iX(xLast), iY(yLast), iX(x) ,iY(y));
  75. xLast = x;
  76. yLast = y;
  77. }
  78.  
  79. void moveTo(Graphics g, double x, double y)
  80. { xLast = x;
  81. yLast = y;
  82. }
  83.  
  84. public void paint(Graphics g)
  85. { Dimension d = getSize();
  86. maxX = d.width - 1;
  87. maxY = d.height - 1;
  88. xLast = fxStart * maxX;
  89. yLast = fyStart * maxY;
  90. dir = dirStart; // Initial direction in degrees
  91. turtleGraphics(g, axiom, level, lengthFract * maxY);
  92. }
  93.  
  94. public void turtleGraphics(Graphics g, String instruction,
  95. int depth, double len)
  96. { double xMark=0, yMark=0, dirMark=0;
  97. for (int i=0;i<instruction.length();i++)
  98. { char ch = instruction.charAt(i);
  99. switch(ch)
  100. {
  101. case 'F': // Step forward and draw
  102. // Start: (xLast, yLast), direction: dir, steplength: len
  103. if (depth == 0)
  104. { double rad = Math.PI/180 * dir, // Degrees -> radians
  105. dx = len * Math.cos(rad), dy = len * Math.sin(rad);
  106. drawTo(g, xLast + dx, yLast + dy);
  107. }
  108. else
  109. turtleGraphics(g, strF, depth - 1, reductFact * len);
  110. break;
  111. case 'f': // Step forward without drawing
  112. // Start: (xLast, yLast), direction: dir, steplength: len
  113. if (depth == 0)
  114. { double rad = Math.PI/180 * dir, // Degrees -> radians
  115. dx = len * Math.cos(rad), dy = len * Math.sin(rad);
  116. moveTo(g, xLast + dx, yLast + dy);
  117. }
  118. else
  119. turtleGraphics(g, strf, depth - 1, reductFact * len);
  120. break;
  121. case 'X':
  122. if (depth > 0)
  123. turtleGraphics(g, strX, depth - 1, reductFact * len);
  124. break;
  125. case 'Y':
  126. if (depth > 0)
  127.  
  128. turtleGraphics(g, strY, depth - 1, reductFact * len);
  129. break;
  130. case '+': // Turn right
  131. dir -= rotation; break;
  132. case '-': // Turn left
  133. dir += rotation; break;
  134. case '[': // Save position and direction
  135. xMark = xLast; yMark = yLast; dirMark = dir; break;
  136. case ']': // Back to saved position and direction
  137. xLast = xMark; yLast = yMark; dir = dirMark; break;
  138. }
  139. }
  140. }
  141. }
  142.  
  143. // Input.java: A class to read numbers and characters from textfiles.
  144. // Methods of this class, available for other program files:
  145. // Input(fileName) (constructor; open input file)
  146. // Input() (constructor; prepare for input from keyboard)
  147. // readInt() (read an integer)
  148. // readFloat() (read a float number)
  149. // readChar() (read a character)
  150. // readString() (read a string between double quotes)
  151. // skipRest() (skip all remaining characters of current line)
  152. // fails() (input operation failed)
  153. // eof() (failure because of end of file)
  154. // clear() (reset error flag)
  155. // close() (close input file)
  156. // pushBack(ch) (push character ch back into the input stream)
  157. import java.io.*;
  158.  
  159. class Input
  160. { private PushbackInputStream pbis;
  161. private boolean ok = true;
  162. private boolean eoFile = false;
  163.  
  164. Input(){pbis = new PushbackInputStream(System.in);}
  165.  
  166. Input(String fileName)
  167. { try
  168. { InputStream is = new FileInputStream(fileName);
  169. pbis = new PushbackInputStream(is);
  170. }
  171. catch(IOException ioe){ok = false;}
  172. }
  173.  
  174. int readInt()
  175. { boolean neg = false;
  176. char ch;
  177. do {ch = readChar();}while (Character.isWhitespace(ch));
  178. if (ch == '-') neg = true; ch = readChar();}
  179. if (!Character.isDigit(ch))
  180. { pushBack(ch);
  181. ok = false;
  182. return 0;
  183. }
  184. int x = ch - '0';
  185. for (;;)
  186. { ch = readChar();
  187. if (!Character.isDigit(ch)){pushBack(ch); break;}
  188. x = 10 * x + (ch - '0');
  189. }
  190. return (neg ? -x : x);
  191. }
  192.  
  193. float readFloat()
  194. { char ch;
  195. int nDec = −1;
  196. boolean neg = false;
  197. do
  198. { ch = readChar();
  199. } while (Character.isWhitespace(ch));
  200. if (ch == '-'){neg = true; ch = readChar();}
  201. if (ch == '.'){nDec = 1; ch = readChar();}
  202. if (!Character.isDigit(ch)){ok = false; pushBack(ch); return 0;}
  203. float x = ch - '0';
  204. for (;;)
  205. { ch = readChar();
  206. if (Character.isDigit(ch))
  207. { x = 10 * x + (ch - '0');
  208. if (nDec >= 0) nDec++;
  209. }
  210. else
  211. if (ch == '.' && nDec == −1) nDec = 0;
  212. else break;
  213. }
  214. while (nDec > 0){x *= 0.1; nDec--;}
  215. if (ch == 'e' || ch == 'E')
  216. { int exp = readInt();
  217. if (!fails())
  218. { while (exp < 0){x *= 0.1; exp++;}
  219. while (exp > 0){x *= 10; exp--;}
  220. }
  221. }
  222. else pushBack(ch);
  223. return (neg ? -x : x);
  224. }
  225.  
  226. char readChar()
  227. { int ch=0;
  228. try
  229. { ch = pbis.read();
  230. if (ch == −1) {eoFile = true; ok = false;}
  231. }
  232. catch(IOException ioe){ok = false;
  233. return (char)ch;
  234. }
  235.  
  236.  
  237. String readString() // Read first string between quotes (").
  238. { String str = " ";
  239. char ch;
  240. do ch = readChar(); while (!(eof() || ch == '"'));
  241. // Initial quote
  242. for (;;)
  243. { ch = readChar();
  244. if (eof() || ch == '"') // Final quote (end of string)
  245. break;
  246. str += ch;
  247. }
  248. return str;
  249. }
  250.  
  251. void skipRest() // Skip rest of line
  252. { char ch;
  253. do ch = readChar(); while (!(eof() || ch == '\n'));
  254. }
  255.  
  256. boolean fails(){return !ok;}
  257. boolean eof(){return eoFile;}
  258. void clear(){ok = true;}
  259.  
  260. void close()
  261. { if (pbis != null)
  262. try {pbis.close();}catch(IOException ioe){ok = false;}
  263. }
  264.  
  265. void pushBack(char ch)
  266. { try {pbis.unread(ch);}catch(IOException ioe){}
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement