Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package triangles;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- /**
- * Class SierpinskiStarterCode is a demo for this assignment.
- */
- public class recursive {
- static double area;
- static Graphics2D graphics;
- /**
- * Writes a sample image to file "sierpinski-demo.png"
- *
- * @param args unused
- */
- public static void main(String[] args) {
- int width = Integer.parseInt(args[0]);
- int height = Integer.parseInt(args[1]);
- area = Double.parseDouble(args[2]);
- Color x = Color.decode(args[3]);
- int red = 255 - x.getRed();
- int green = 255 - x.getGreen();
- int blue = 255 - x.getBlue();
- Color background = new Color(red, green, blue);
- // A 320x240-pixel RGB image
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- // Graphics2D is our "drawing" class
- graphics = image.createGraphics();
- // Any clearing operations will use a yellow color
- graphics.setBackground(background);
- // Clear the image to the background color
- graphics.clearRect(0, 0, image.getWidth(), image.getHeight());
- // Any drawing operations will use a blue color
- graphics.setColor(x);
- // Draw a line from the top (width/2, 0) to the lower-right corner (width - 1,
- // height - 1):
- // graphics.drawLine(width/2, 0, image.getWidth() - 1, image.getHeight() - 1);
- // Draw a line from the lower-left corner (0, height - 1) to the top (width/2,
- // 0):
- // graphics.drawLine(0, image.getHeight() - 1, width/2, 0);
- // Draw a line from the Lower-left corner to the lower-right corner
- // graphics.drawLine(0, height - 1, width - 1, height -1);
- fractal((width / 2.0), 0.0, 0.0, (height - 1.0), (width - 1.0), (height - 1.0));
- // Save the image as a PNG file named "sierpinski-demo.png"
- try {
- ImageIO.write(image, "png", new File(args[4]));
- } catch (Exception e) {
- System.err.println("Unable to write image file!: " + e.getMessage());
- }
- }
- static void fractal(double topx, double topy, double leftx, double lefty, double rightx, double righty) {
- double base = rightx - leftx;
- double height = lefty - topy;
- if ((base * height) / 2 < area) {
- return;
- }
- graphics.drawLine((int) Math.round(topx), (int) Math.round(topy), (int) Math.round(leftx),
- (int) Math.round(lefty));
- graphics.drawLine((int) Math.round(leftx), (int) (int) Math.round(lefty), (int) (int) Math.round(rightx),
- (int) Math.round(righty));
- graphics.drawLine((int) Math.round(rightx), (int) Math.round(righty), (int) Math.round(topx),
- (int) Math.round(topy));
- fractal(topx, topy, (topx + leftx) / 2, (topy + lefty) / 2, ((topx + rightx) / 2), (topy + righty) / 2);
- fractal((topx + leftx) / 2, (topy + lefty) / 2, leftx, lefty, (leftx + rightx) / 2, (lefty + righty) / 2);
- fractal((topx + rightx) / 2, (topy + righty) / 2, (leftx + rightx) / 2, (lefty + righty) / 2, rightx, righty);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement