Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.swing.*;
- public class SierpinskiGUI {
- private JFrame frame;
- private JPanel superPanel, rightPanel, textFieldPanel, pickersPanel;
- private Canvas canvas;
- private JLabel textFieldLabel;
- private JTextField textField;
- private JCheckBox randomizeBox;
- private JButton drawButton;
- private MyColor[] colors;
- private ArrayList<Triangle> triangles;
- private ColorSelector[] colorSelector;
- private boolean randomColors;
- private int totalDepth;
- private final int cSize = 512;
- /**
- * @author phil
- *
- * Holds the data for one triangle.
- */
- private class Triangle {
- private Color color;
- private int[] xvals;
- private int[] yvals;
- private Polygon triangle;
- /**
- *
- * Make a triangle with a specified color.
- *
- * @param d
- * @param x
- * @param y
- * @param S
- * @param color
- */
- Triangle(int d, int x, int y, int S, Color color) {
- this.xvals = new int[3];
- this.yvals = new int[3];
- this.color = color;
- if (d == -1) {
- xvals[0] = x;
- yvals[0] = y + S; // bottom left
- xvals[1] = x + S;
- yvals[1] = y + S; // bottom right
- xvals[2] = x + (S / 2);
- yvals[2] = y; // top
- } else {
- xvals[0] = x + S / 2;
- yvals[0] = y + S; // bottom
- xvals[1] = x + S / 4;
- yvals[1] = y + S / 2; // top left
- xvals[2] = x + 3 * S / 4;
- yvals[2] = y + S / 2; // top right
- }
- this.triangle = new Polygon(xvals, yvals, 3);
- }
- /**
- *
- * Make a triangle and assign the color automatically.
- *
- * @param d
- * @param x
- * @param y
- * @param S
- */
- Triangle(int d, int x, int y, int S) {
- this(d, x, y, S, Color.black);
- int colorNum;
- if (randomColors == false) {
- colorNum = totalDepth - d;
- colorNum = colorNum % 5;
- this.color = colorSelector[colorNum].getColor();
- } else {
- colorNum = totalDepth - d;
- this.color = colors[colorNum].getColor();
- }
- // System.out.printf("Auto selecting color %d: %s%n",colorNum,colors[colorNum].toString());
- }
- /**
- * @return The color of the triangle
- */
- public Color getColor() {
- return this.color;
- }
- /**
- * @return The polygon object for this triangle
- */
- public Polygon getPolygon() {
- return this.triangle;
- }
- /**
- * @param color The color to set for this triangle.
- */
- public void setColor(Color color) {
- this.color = color;
- }
- }
- /**
- * @author phil
- * Includes a corresponding JComboBox and JLabel for the color selector.
- *
- */
- private class ColorSelector {
- private JLabel text;
- private JComboBox comboBox;
- /**
- * @param number Number of the box, e.g. Color 1
- */
- ColorSelector(int number) {
- text = new JLabel("Color " + (number + 1));
- comboBox = new JComboBox(colors);
- comboBox.setSelectedIndex(number);
- }
- public JComboBox getComboBox() {
- return comboBox;
- }
- public JLabel getText() {
- return text;
- }
- /**
- * @return The Color object for the selected color
- */
- public Color getColor() {
- MyColor theMyColor;
- theMyColor = (MyColor) comboBox.getSelectedItem();
- return theMyColor.getColor();
- }
- }
- /**
- * Start the program
- * @param args none
- */
- public static void main(String[] args) {
- new SierpinskiGUI();
- }
- /**
- * Contains a Color and a String for the name.
- * @author phil
- *
- */
- private class MyColor {
- private String name;
- private Color color;
- MyColor(String name, Color color) {
- this.name = name;
- this.color = color;
- }
- public Color getColor() {
- return color;
- }
- public String toString() {
- return name;
- }
- }
- /**
- * @return Array of the available colors.
- */
- public MyColor[] getColors() {
- return colors;
- }
- /**
- * Set up colors to have the predefined Java colors.
- */
- private void setupColors() {
- colors = new MyColor[13];
- colors[0] = new MyColor("Blue", Color.blue);
- colors[1] = new MyColor("Cyan", Color.cyan);
- colors[2] = new MyColor("Dark Grey", Color.darkGray);
- colors[3] = new MyColor("Grey", Color.gray);
- colors[4] = new MyColor("Green", Color.green);
- colors[5] = new MyColor("Light Grey", Color.lightGray);
- colors[6] = new MyColor("Magenta", Color.magenta);
- colors[7] = new MyColor("Orange", Color.orange);
- colors[8] = new MyColor("Pink", Color.pink);
- colors[9] = new MyColor("Red", Color.red);
- colors[10] = new MyColor("White", Color.white);
- colors[11] = new MyColor("Yellow", Color.yellow);
- colors[12] = new MyColor("Black", Color.black);
- randomColors = false;
- }
- /**
- * Change the colors[] array to have a set of random MyColors.
- */
- private void setupRandomColors() {
- int[] rgb;
- rgb = new int[3];
- Random rng = new Random();
- colors = new MyColor[13];
- for (int i = 0; i < 13; i++) {
- rgb[0] = rng.nextInt(256);
- rgb[1] = rng.nextInt(256);
- rgb[2] = rng.nextInt(256);
- String name = "" + rgb[0] + "," + rgb[1] + "," + rgb[2];
- colors[i] = new MyColor(name, new Color(rgb[0], rgb[1], rgb[2]));
- randomColors = true;
- }
- }
- /**
- * Make all the frames and panels.
- */
- private void buildGUI() {
- setupColors();
- frame = new JFrame("Sierpinski Visualizer");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new FlowLayout());
- // set up the Canvas
- superPanel = new JPanel(); // (new GridLayout(1, 2));
- superPanel.setLayout(new BoxLayout(superPanel, BoxLayout.X_AXIS));
- canvas = new Canvas();
- canvas.setSize(cSize, cSize);
- canvas.setBackground(Color.BLACK);
- superPanel.add(canvas);
- // Everything but the Canvas goes in this JPanel
- rightPanel = new JPanel();
- rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
- // set up the Recursive Depth entry
- textFieldPanel = new JPanel(new FlowLayout());
- textFieldLabel = new JLabel("Recursive Depth:");
- textField = new JTextField(10);
- textFieldPanel.add(textFieldLabel);
- textFieldPanel.add(textField);
- rightPanel.add(textFieldPanel);
- // set up the pickers
- pickersPanel = new JPanel();
- pickersPanel.setLayout(new GridLayout(5, 2));
- // pickersPanel.setLayout(new FlowLayout());
- colorSelector = new ColorSelector[5];
- for (int i = 0; i < 5; i++) {
- colorSelector[i] = new ColorSelector(i);
- // colorSelector[i].getComboBox().setvalue(i);
- pickersPanel.add(colorSelector[i].getText());
- pickersPanel.add(colorSelector[i].getComboBox());
- }
- rightPanel.add(pickersPanel);
- randomizeBox = new JCheckBox("Randomize colors at each level");
- randomizeBox.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- if (e.getStateChange() == ItemEvent.SELECTED) { // randomize colors
- for (ColorSelector c : colorSelector) {
- c.getComboBox().setEnabled(false);
- setupRandomColors();
- }
- } else {
- for (ColorSelector c : colorSelector) { // use colors from boxes.
- c.getComboBox().setEnabled(true);
- setupColors();
- }
- }
- }
- });
- drawButton = new JButton("Draw!");
- drawButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int depth;
- try {
- depth = Integer.parseInt(textField.getText()); // check 1-10 and number.
- if (depth < 1 || depth > 10)
- throw (new NumberFormatException());
- } catch (NumberFormatException x) {
- JOptionPane.showMessageDialog(frame, "Please input a number from 1 to 10.");
- return;
- }
- doDraw(depth);
- }
- });
- rightPanel.add(randomizeBox);
- rightPanel.add(drawButton);
- superPanel.add(rightPanel);
- frame.add(superPanel);
- frame.pack();
- frame.setVisible(true);
- }
- /**
- * Wrapper for the recursive draw() method
- * @param depth
- */
- private void doDraw(int depth) {
- Graphics g = canvas.getGraphics();
- if (randomColors == true)
- setupRandomColors();
- g.setColor(Color.black);
- g.fillRect(0, 0, cSize, cSize);
- triangles.clear();
- // System.out.println("------------------------------------------------------");
- this.totalDepth = depth; // for calculating the correct color
- // Draw the outside polygon outline
- Triangle outer = new Triangle(-1, 0, 0, cSize);
- g.setColor(Color.white);
- g.drawPolygon(outer.getPolygon());
- draw(depth, 0, 0, cSize); // create the triangles.
- for (Triangle t : triangles) { // draw the triangles.
- g.setColor(t.getColor());
- g.fillPolygon(t.getPolygon());
- }
- System.out.printf("%d triangles.%n",triangles.size());
- }
- /**
- * Constructor called from main
- *
- */
- SierpinskiGUI() {
- triangles = new ArrayList<Triangle>();
- buildGUI();
- }
- /**
- * Recursive function to create Triangles.
- * @param d
- * @param x
- * @param y
- * @param S
- */
- private void draw(int d, int x, int y, int S) {
- // Done drawing, stop.
- if (d == 0)
- return;
- // Otherwise, draw big triangle at this level, between the points
- // shown in the figure. You can use the fillPolygon() method of
- // the Graphics object of your Canvas. Make sure you get the color
- // right!
- /* <INSERT YOUR CODE HERE> */
- triangles.add(new Triangle(d, x, y, S));
- // Draw the subtriangles. The self-similarity of fractals means
- // that they are themselves Sierpinski triangles of depth d-1.
- draw(d - 1, x + S / 4, y, S / 2);
- draw(d - 1, x, y + S / 2, S / 2);
- draw(d - 1, x + S / 2, y + S / 2, S / 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement