/* Laura Toma csci 101 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.Math; public class FractalDriver extends JComponent { private int pad = 5; //space between images private int preferredNumImages; static Sierpinski s; public FractalDriver(int preferredNumImages, int pad) { if (preferredNumImages > 0) { this.preferredNumImages = preferredNumImages; } if (pad >= 0) { this.pad = pad; } //Set a reasonable default border. if (this.pad > 0) { setBorder(BorderFactory.createEmptyBorder(this.pad, this.pad, this.pad, this.pad)); } } public Dimension getPreferredSize() { return new Dimension(700, 500); } public Dimension getMinimumSize() { return new Dimension(0,0); } private int min(int a, int b) { if(a < b) { return a; } else { return b; } } protected void paintComponent(Graphics g) { Insets insets = getInsets(); //must not mess up the graphics context, so copy it Graphics2D g2d = (Graphics2D)g.create(); int width = getWidth() - insets.left - insets.right; int height = getHeight() - insets.top - insets.bottom; g2d.translate(insets.left, insets.bottom); g2d.setColor(Color.black); //compute coordinates of triangle based on window size double d, h; double x = width/ 2; double y = height / 2; if(height > width * Math.sin(Math.PI/3)) { // width limited d = width; h = d * Math.sin(Math.PI/3); } else { d = height / Math.sin(Math.PI/3); h = height; } int x1, x2, x3, y1, y2, y3; x1 = (int)(x); x2 = (int)(x - d / 2); x3 = (int)(x + d / 2); y1 = (int)(y - h / 2); y2 = y3 = (int)(y + h / 2); s.main(x1, y1, x2, y2, x3, y3, g2d); g2d.dispose(); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("FractalDriver"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. FractalDriver id = new FractalDriver(5, -1); frame.getContentPane().add(id, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { s = new Sierpinski(args); //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }