Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. //FractalGUI class
  2. import java.awt.geom.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6.  
  7. import javax.swing.*;
  8.  
  9. public class FractalGUI extends JFrame {
  10.  
  11. ArrayList<ModifiedLine> linesFromModel;
  12. FractalGUI theGUI;
  13. Model theModel;
  14. ModifiedLine aLine;
  15. JTextField setDepthField = new JTextField(20);
  16. JPanel depthFieldPanel = new JPanel();
  17. JButton startFractal = new JButton("Fractalize!");
  18.  
  19. public static void main(String[] args) {
  20. Model model = new Model();
  21. FractalGUI theGUI = new FractalGUI(model);
  22. model.setTheGUI(theGUI);
  23. }
  24.  
  25. public FractalGUI(Model aModel) {
  26. super("Fractal Generator");
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. setLocationRelativeTo(null);
  29. setVisible(true);
  30. JPanel canvas = new DrawPanel();
  31. add(canvas, BorderLayout.NORTH);
  32. add(addTextFieldPanel(), BorderLayout.SOUTH);
  33. pack();
  34. theModel = aModel;
  35. setupLineArray();
  36.  
  37. }
  38.  
  39. public void setupLineArray() {
  40. linesFromModel = theModel.getLinesToDraw();
  41. }
  42.  
  43. public JPanel addTextFieldPanel() {
  44. depthFieldPanel.setLayout(new BorderLayout());
  45. setDepthField.setEditable(true);
  46. depthFieldPanel.add(setDepthField, BorderLayout.WEST);
  47. startFractal.addActionListener(new ButtonListener());
  48. depthFieldPanel.add(startFractal, BorderLayout.EAST);
  49. return depthFieldPanel;
  50. }
  51.  
  52. class ButtonListener implements ActionListener {
  53. public void actionPerformed(ActionEvent e) {
  54. if (e.getActionCommand() == "Fractalize!") {
  55. int depth = Integer.parseInt(setDepthField.getText());
  56. System.out.println(depth + "");
  57. theModel.toFiveLines(linesFromModel.get(0), depth);
  58. theModel.toFiveLines(linesFromModel.get(1), depth);
  59. theModel.toFiveLines(linesFromModel.get(2), depth);
  60. theModel.toFiveLines(linesFromModel.get(3), depth);
  61. repaint();
  62. }
  63. }
  64. }
  65.  
  66. class DrawPanel extends JPanel {
  67.  
  68. public DrawPanel(){
  69.  
  70. // Set window stuff
  71. setBackground(Color.gray); // window background color
  72. setPreferredSize(new Dimension(500,500));
  73. setResizable(false);
  74.  
  75. }
  76. public void paintComponent(Graphics g) {
  77. super.paintComponent(g);
  78. Graphics2D g2 = (Graphics2D) g;
  79. linesFromModel = theModel.getLinesToDraw();
  80. for (ModifiedLine lines: linesFromModel) {
  81. g2.setColor(Color.red);
  82. g2.draw(lines);
  83. }
  84. }
  85. }
  86.  
  87.  
  88. public Model getTheModel() {
  89. return theModel;
  90. }
  91.  
  92. public void setTheModel(Model theModel) {
  93. this.theModel = theModel;
  94. }
  95.  
  96. public ModifiedLine getaLine() {
  97. return aLine;
  98. }
  99.  
  100. public void setaLine(ModifiedLine aLine) {
  101. this.aLine = aLine;
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. //Model class
  112. import java.awt.geom.*;
  113. import java.awt.*;
  114. import java.awt.event.*;
  115. import java.util.*;
  116.  
  117. import javax.swing.*;
  118.  
  119. public class Model {
  120. ArrayList<ModifiedLine> linesToDraw = new ArrayList<ModifiedLine>();
  121. FractalGUI theGUI;
  122.  
  123. public Model() {
  124. linesToDraw.add(new ModifiedLine(250.0, 300.0, 250.0, 200.0));
  125. linesToDraw.add(new ModifiedLine(250.0, 200.0, 350.0, 200.0));
  126. linesToDraw.add(new ModifiedLine(350.0, 200.0, 350.0, 300.0));
  127. linesToDraw.add(new ModifiedLine(350.0, 300.0, 250.0, 300.0));
  128. }
  129.  
  130. // converts a given line into 5 new lines that branch off of it
  131. public void toFiveLines(ModifiedLine line, int depth) {
  132. //creates new ArrayList of modifiedLines
  133. ArrayList<ModifiedLine> newLines = new ArrayList<ModifiedLine>();
  134.  
  135. //divides line length by 3 for calculations of new points
  136. double thirdOfLengthX = ((Math.abs(line.getX1() - line.getX2()))/3);
  137. double thirdOfLengthY = ((Math.abs(line.getY1() - line.getY2()))/3);
  138.  
  139. if (depth < 1) {
  140.  
  141. }
  142.  
  143. else {
  144.  
  145. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
  146.  
  147. // if both points have same x coordinate the line is vertical, then whichever
  148. // y point is higher determines orientation
  149.  
  150. // if both points have same y coordinate the line is vertical, then whichever
  151. // x point is higher determines orientation
  152.  
  153. if ( (line.getX1() == line.getX2()) && (line.getY2() < line.getY1()) ) {
  154. //first third of old vertical line
  155. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
  156. //last third of old vertical line
  157. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
  158. //outside vertical line
  159. newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY)));
  160. //outside bottom line
  161. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY)));
  162. //outside top line
  163. newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY1()-(2*thirdOfLengthY)));
  164.  
  165. }
  166. else if ( (line.getY1() == line.getY2()) && (line.getX1() < line.getX2()) ){
  167. //first third of old horizontal line
  168. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
  169. //last third of old horizontal line
  170. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
  171. //outside horizontal line
  172. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
  173. //outside "right" (relative to original line position)line
  174. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
  175. //outside "left" (relative to original line position)line
  176. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
  177. }
  178. else if ( (line.getX1() == line.getX2()) && (line.getY2() > line.getY1()) ){
  179. //first third of old vertical line
  180. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()+thirdOfLengthY));
  181. //last third of old vertical line
  182. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
  183. //outside vertical line
  184. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY)));
  185. //outside top line
  186. newLines.add(new ModifiedLine(line.getX1(), line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY)));
  187. //outside bottom line
  188. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY), line.getX1(), line.getY1()+(2*thirdOfLengthY)));
  189. }
  190. else if ( (line.getY1() == line.getY2()) && (line.getX1() > line.getX2()) ){
  191. //first third of old horizontal line
  192. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
  193. //last third of old horizontal line
  194. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
  195. //outside horizontal line
  196. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
  197. //outside "right" (relative to original line position)line
  198. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
  199. //outside "left" (relative to original line position)line
  200. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
  201.  
  202. }
  203. else {
  204.  
  205. }
  206. }
  207. //add new Lines to list of lines to draw
  208. for (ModifiedLine lines: newLines) {
  209. linesToDraw.add(lines);
  210. }
  211. //remove original line
  212. linesToDraw.remove(line);
  213.  
  214. //Recursive method calls here on each new line created
  215. for (ModifiedLine lines: newLines) {
  216. toFiveLines(lines, depth-1);
  217. }
  218. // toFiveLines(newLines.get(0), depth-1);
  219. // toFiveLines(newLines.get(1), depth-1);
  220. // toFiveLines(newLines.get(2), depth-1);
  221. // toFiveLines(newLines.get(3), depth-1);
  222. // toFiveLines(newLines.get(4), depth-1);
  223.  
  224. }
  225.  
  226. public ArrayList<ModifiedLine> getLinesToDraw() {
  227. return linesToDraw;
  228. }
  229.  
  230. public void setLinesToDraw(ArrayList<ModifiedLine> linesToDraw) {
  231. this.linesToDraw = linesToDraw;
  232. }
  233.  
  234. public FractalGUI getTheGUI() {
  235. return theGUI;
  236. }
  237.  
  238. public void setTheGUI(FractalGUI theGUI) {
  239. this.theGUI = theGUI;
  240. }
  241.  
  242. }
  243.  
  244.  
  245.  
  246.  
  247. //ModifiedLine
  248. import java.awt.geom.*;
  249. import java.awt.*;
  250. import java.awt.event.*;
  251. import java.util.*;
  252. import javax.swing.*;
  253.  
  254.  
  255. public class ModifiedLine extends Line2D.Double {
  256.  
  257. // overrides Line2D.Double's constructor
  258. public ModifiedLine(double X1, double Y1, double X2, double Y2) {
  259. super(X1, Y1, X2, Y2);
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement