Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //FractalGUI class
- import java.awt.geom.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- public class FractalGUI extends JFrame {
- ArrayList<ModifiedLine> linesFromModel;
- FractalGUI theGUI;
- Model theModel;
- ModifiedLine aLine;
- JTextField setDepthField = new JTextField(20);
- JPanel depthFieldPanel = new JPanel();
- JButton startFractal = new JButton("Fractalize!");
- public static void main(String[] args) {
- Model model = new Model();
- FractalGUI theGUI = new FractalGUI(model);
- model.setTheGUI(theGUI);
- }
- public FractalGUI(Model aModel) {
- super("Fractal Generator");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setVisible(true);
- JPanel canvas = new DrawPanel();
- add(canvas, BorderLayout.NORTH);
- add(addTextFieldPanel(), BorderLayout.SOUTH);
- pack();
- theModel = aModel;
- setupLineArray();
- }
- public void setupLineArray() {
- linesFromModel = theModel.getLinesToDraw();
- }
- public JPanel addTextFieldPanel() {
- depthFieldPanel.setLayout(new BorderLayout());
- setDepthField.setEditable(true);
- depthFieldPanel.add(setDepthField, BorderLayout.WEST);
- startFractal.addActionListener(new ButtonListener());
- depthFieldPanel.add(startFractal, BorderLayout.EAST);
- return depthFieldPanel;
- }
- class ButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getActionCommand() == "Fractalize!") {
- int depth = Integer.parseInt(setDepthField.getText());
- System.out.println(depth + "");
- for (ModifiedLine lines: linesFromModel) {
- linesFromModel = theModel.getLinesToDraw();
- theModel.toFiveLines(lines, depth);
- }
- repaint();
- }
- }
- }
- class DrawPanel extends JPanel {
- public DrawPanel(){
- // Set window stuff
- setBackground(Color.gray); // window background color
- setPreferredSize(new Dimension(500,500));
- setResizable(false);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- for (ModifiedLine lines: linesFromModel) {
- g2.setColor(Color.red);
- g2.draw(lines);
- }
- }
- }
- public Model getTheModel() {
- return theModel;
- }
- public void setTheModel(Model theModel) {
- this.theModel = theModel;
- }
- public ModifiedLine getaLine() {
- return aLine;
- }
- public void setaLine(ModifiedLine aLine) {
- this.aLine = aLine;
- }
- }
- //Model classimport java.awt.geom.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- public class Model {
- ArrayList<ModifiedLine> linesToDraw = new ArrayList<ModifiedLine>();
- FractalGUI theGUI;
- public Model() {
- linesToDraw.add(new ModifiedLine(250.0, 300.0, 250.0, 200.0));
- linesToDraw.add(new ModifiedLine(250.0, 200.0, 350.0, 200.0));
- linesToDraw.add(new ModifiedLine(350.0, 200.0, 350.0, 300.0));
- linesToDraw.add(new ModifiedLine(350.0, 300.0, 250.0, 300.0));
- }
- // converts a given line into 5 new lines that branch off of it
- public void toFiveLines(ModifiedLine line, int depth) {
- //creates new ArrayList of modifiedLines
- ArrayList<ModifiedLine> newLines = new ArrayList<ModifiedLine>();
- //divides line length by 3 for calculations of new points
- double thirdOfLengthX = ((Math.abs(line.getX1() - line.getX2()))/3);
- double thirdOfLengthY = ((Math.abs(line.getY1() - line.getY2()))/3);
- if (depth < 1) {
- }
- else {
- newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
- // if both points have same x coordinate the line is vertical, then whichever
- // y point is higher determines orientation
- // if both points have same y coordinate the line is vertical, then whichever
- // x point is higher determines orientation
- if ( (line.getX1() == line.getX2()) && (line.getY2() < line.getY1()) ) {
- //first third of old vertical line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()-thirdOfLengthY));
- //last third of old vertical line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
- //outside vertical line
- newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY)));
- //outside bottom line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(thirdOfLengthY), line.getX1()-thirdOfLengthY, line.getY1()-(thirdOfLengthY)));
- //outside top line
- newLines.add(new ModifiedLine(line.getX1()-thirdOfLengthY, line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY1()-(2*thirdOfLengthY)));
- }
- else if ( (line.getY1() == line.getY2()) && (line.getX1() < line.getX2()) ){
- //first third of old horizontal line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
- //last third of old horizontal line
- newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
- //outside horizontal line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
- //outside "right" (relative to original line position)line
- newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
- //outside "left" (relative to original line position)line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
- }
- else if ( (line.getX1() == line.getX2()) && (line.getY2() > line.getY1()) ){
- //first third of old vertical line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()+thirdOfLengthY));
- //last third of old vertical line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1()-(2*thirdOfLengthY), line.getX1(), line.getY2()));
- //outside vertical line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY)));
- //outside top line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1()+(thirdOfLengthY), line.getX1()+thirdOfLengthY, line.getY1()+(thirdOfLengthY)));
- //outside bottom line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthY, line.getY1()+(2*thirdOfLengthY), line.getX1(), line.getY1()+(2*thirdOfLengthY)));
- }
- else if ( (line.getY1() == line.getY2()) && (line.getX1() > line.getX2()) ){
- //first third of old horizontal line
- newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
- //last third of old horizontal line
- newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
- //outside horizontal line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
- //outside "right" (relative to original line position)line
- newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
- //outside "left" (relative to original line position)line
- newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
- }
- else {
- // //test
- // //first third of old horizontal line
- // newLines.add(new ModifiedLine(line.getX1(), line.getY1(), line.getX1()+thirdOfLengthX, line.getY1()));
- // //last third of old horizontal line
- // newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1(), line.getX2(), line.getY1()));
- // //outside horizontal line
- // newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1()-(thirdOfLengthX), line.getX1()+(2*thirdOfLengthX), line.getY1()-(thirdOfLengthX)));
- // //outside "right" (relative to original line position)line
- // newLines.add(new ModifiedLine(line.getX1()+(2*thirdOfLengthX), line.getY1()-thirdOfLengthX, line.getX1()+(2*thirdOfLengthX), line.getY1()));
- // //outside "left" (relative to original line position)line
- // newLines.add(new ModifiedLine(line.getX1()+thirdOfLengthX, line.getY1(), line.getX1()+thirdOfLengthY, line.getY1()-(thirdOfLengthX)));
- }
- }
- //add new Lines to list of lines to draw
- for (ModifiedLine lines: newLines) {
- linesToDraw.add(lines);
- }
- //remove original line
- linesToDraw.remove(line);
- //Recursive method calls here on each new line created
- for (ModifiedLine lines: newLines) {
- toFiveLines(lines, depth-1);
- }
- // toFiveLines(newLines.get(0), depth-1);
- // toFiveLines(newLines.get(1), depth-1);
- // toFiveLines(newLines.get(2), depth-1);
- // toFiveLines(newLines.get(3), depth-1);
- // toFiveLines(newLines.get(4), depth-1);
- }
- public ArrayList<ModifiedLine> getLinesToDraw() {
- return linesToDraw;
- }
- public void setLinesToDraw(ArrayList<ModifiedLine> linesToDraw) {
- this.linesToDraw = linesToDraw;
- }
- public FractalGUI getTheGUI() {
- return theGUI;
- }
- public void setTheGUI(FractalGUI theGUI) {
- this.theGUI = theGUI;
- }
- }
- //ModifiedLineimport java.awt.geom.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- public class ModifiedLine extends Line2D.Double {
- // overrides Line2D.Double's constructor
- public ModifiedLine(double X1, double Y1, double X2, double Y2) {
- super(X1, Y1, X2, Y2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement