Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 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. for (ModifiedLine lines: linesFromModel) {
  58. linesFromModel = theModel.getLinesToDraw();
  59. theModel.toFiveLines(lines, depth);
  60. }
  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. for (ModifiedLine lines: linesFromModel) {
  80. g2.setColor(Color.red);
  81. g2.draw(lines);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. public Model getTheModel() {
  88. return theModel;
  89. }
  90.  
  91. public void setTheModel(Model theModel) {
  92. this.theModel = theModel;
  93. }
  94.  
  95. public ModifiedLine getaLine() {
  96. return aLine;
  97. }
  98.  
  99. public void setaLine(ModifiedLine aLine) {
  100. this.aLine = aLine;
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. //Model class
  110. import java.awt.geom.*;
  111. import java.awt.*;
  112. import java.awt.event.*;
  113. import java.util.*;
  114.  
  115. import javax.swing.*;
  116.  
  117. public class Model {
  118. ArrayList<ModifiedLine> linesToDraw = new ArrayList<ModifiedLine>();
  119. FractalGUI theGUI;
  120.  
  121. public Model() {
  122. linesToDraw.add(new ModifiedLine(250.0, 300.0, 250.0, 200.0));
  123. linesToDraw.add(new ModifiedLine(250.0, 200.0, 350.0, 200.0));
  124. linesToDraw.add(new ModifiedLine(350.0, 200.0, 350.0, 300.0));
  125. linesToDraw.add(new ModifiedLine(350.0, 300.0, 250.0, 300.0));
  126. }
  127.  
  128. // converts a given line into 5 new lines that branch off of it
  129. public void toFiveLines(ModifiedLine line, int depth) {
  130. //creates new ArrayList of modifiedLines
  131. ArrayList<ModifiedLine> newLines = new ArrayList<ModifiedLine>();
  132.  
  133. //divides line length by 3 for calculations of new points
  134. double thirdOfLengthX = ((Math.abs(line.getX1() - line.getX2()))/3);
  135. double thirdOfLengthY = ((Math.abs(line.getY1() - line.getY2()))/3);
  136.  
  137. if (depth < 1) {
  138.  
  139. }
  140.  
  141. else {
  142.  
  143. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
  144.  
  145. // if both points have same x coordinate the line is vertical, then whichever
  146. // y point is higher determines orientation
  147.  
  148. // if both points have same y coordinate the line is vertical, then whichever
  149. // x point is higher determines orientation
  150.  
  151. if ( (line.getX1() == line.getX2()) && (line.getY2() < line.getY1()) ) {
  152. //first third of old vertical line
  153. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
  154. //last third of old vertical line
  155. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
  156. //outside vertical line
  157. newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY)));
  158. //outside bottom line
  159. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY)));
  160. //outside top line
  161. newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY1()-(2*thirdOfLengthY)));
  162.  
  163. }
  164. else if ( (line.getY1() == line.getY2()) && (line.getX1() < line.getX2()) ){
  165. //first third of old horizontal line
  166. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
  167. //last third of old horizontal line
  168. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
  169. //outside horizontal line
  170. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
  171. //outside "right" (relative to original line position)line
  172. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
  173. //outside "left" (relative to original line position)line
  174. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
  175. }
  176. else if ( (line.getX1() == line.getX2()) && (line.getY2() > line.getY1()) ){
  177. //first third of old vertical line
  178. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()+thirdOfLengthY));
  179. //last third of old vertical line
  180. newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
  181. //outside vertical line
  182. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY)));
  183. //outside top line
  184. newLines.add(new ModifiedLine(line.getX1(), line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY)));
  185. //outside bottom line
  186. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY), line.getX1(), line.getY1()+(2*thirdOfLengthY)));
  187. }
  188. else if ( (line.getY1() == line.getY2()) && (line.getX1() > line.getX2()) ){
  189. //first third of old horizontal line
  190. newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
  191. //last third of old horizontal line
  192. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
  193. //outside horizontal line
  194. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
  195. //outside "right" (relative to original line position)line
  196. newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
  197. //outside "left" (relative to original line position)line
  198. newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
  199.  
  200. }
  201. else {
  202.  
  203. }
  204. }
  205. //add new Lines to list of lines to draw
  206. for (ModifiedLine lines: newLines) {
  207. linesToDraw.add(lines);
  208. }
  209. //remove original line
  210. linesToDraw.remove(line);
  211.  
  212. //Recursive method calls here on each new line created
  213. for (ModifiedLine lines: newLines) {
  214. toFiveLines(lines, depth-1);
  215. }
  216. // toFiveLines(newLines.get(0), depth-1);
  217. // toFiveLines(newLines.get(1), depth-1);
  218. // toFiveLines(newLines.get(2), depth-1);
  219. // toFiveLines(newLines.get(3), depth-1);
  220. // toFiveLines(newLines.get(4), depth-1);
  221.  
  222. }
  223.  
  224. public ArrayList<ModifiedLine> getLinesToDraw() {
  225. return linesToDraw;
  226. }
  227.  
  228. public void setLinesToDraw(ArrayList<ModifiedLine> linesToDraw) {
  229. this.linesToDraw = linesToDraw;
  230. }
  231.  
  232. public FractalGUI getTheGUI() {
  233. return theGUI;
  234. }
  235.  
  236. public void setTheGUI(FractalGUI theGUI) {
  237. this.theGUI = theGUI;
  238. }
  239.  
  240. }
  241.  
  242.  
  243.  
  244.  
  245. //ModifiedLine
  246. import java.awt.geom.*;
  247. import java.awt.*;
  248. import java.awt.event.*;
  249. import java.util.*;
  250. import javax.swing.*;
  251.  
  252.  
  253. public class ModifiedLine extends Line2D.Double {
  254.  
  255. // overrides Line2D.Double's constructor
  256. public ModifiedLine(double X1, double Y1, double X2, double Y2) {
  257. super(X1, Y1, X2, Y2);
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement