Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. //package triangles;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. /**
  11. * Class SierpinskiStarterCode is a demo for this assignment.
  12. */
  13. public class recursive {
  14. static double area;
  15. static Graphics2D graphics;
  16.  
  17. /**
  18. * Writes a sample image to file "sierpinski-demo.png"
  19. *
  20. * @param args unused
  21. */
  22. public static void main(String[] args) {
  23. int width = Integer.parseInt(args[0]);
  24. int height = Integer.parseInt(args[1]);
  25. area = Double.parseDouble(args[2]);
  26.  
  27. Color x = Color.decode(args[3]);
  28. int red = 255 - x.getRed();
  29. int green = 255 - x.getGreen();
  30. int blue = 255 - x.getBlue();
  31. Color background = new Color(red, green, blue);
  32.  
  33. // A 320x240-pixel RGB image
  34. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  35. // Graphics2D is our "drawing" class
  36. graphics = image.createGraphics();
  37. // Any clearing operations will use a yellow color
  38. graphics.setBackground(background);
  39. // Clear the image to the background color
  40. graphics.clearRect(0, 0, image.getWidth(), image.getHeight());
  41. // Any drawing operations will use a blue color
  42. graphics.setColor(x);
  43. // Draw a line from the top (width/2, 0) to the lower-right corner (width - 1,
  44. // height - 1):
  45. // graphics.drawLine(width/2, 0, image.getWidth() - 1, image.getHeight() - 1);
  46. // Draw a line from the lower-left corner (0, height - 1) to the top (width/2,
  47. // 0):
  48. // graphics.drawLine(0, image.getHeight() - 1, width/2, 0);
  49. // Draw a line from the Lower-left corner to the lower-right corner
  50. // graphics.drawLine(0, height - 1, width - 1, height -1);
  51. fractal((width / 2.0), 0.0, 0.0, (height - 1.0), (width - 1.0), (height - 1.0));
  52. // Save the image as a PNG file named "sierpinski-demo.png"
  53. try {
  54. ImageIO.write(image, "png", new File(args[4]));
  55. } catch (Exception e) {
  56. System.err.println("Unable to write image file!: " + e.getMessage());
  57. }
  58. }
  59.  
  60. static void fractal(double topx, double topy, double leftx, double lefty, double rightx, double righty) {
  61.  
  62. double base = rightx - leftx;
  63. double height = lefty - topy;
  64. if ((base * height) / 2 < area) {
  65. return;
  66. }
  67. graphics.drawLine((int) Math.round(topx), (int) Math.round(topy), (int) Math.round(leftx),
  68. (int) Math.round(lefty));
  69. graphics.drawLine((int) Math.round(leftx), (int) (int) Math.round(lefty), (int) (int) Math.round(rightx),
  70. (int) Math.round(righty));
  71. graphics.drawLine((int) Math.round(rightx), (int) Math.round(righty), (int) Math.round(topx),
  72. (int) Math.round(topy));
  73. fractal(topx, topy, (topx + leftx) / 2, (topy + lefty) / 2, ((topx + rightx) / 2), (topy + righty) / 2);
  74. fractal((topx + leftx) / 2, (topy + lefty) / 2, leftx, lefty, (leftx + rightx) / 2, (lefty + righty) / 2);
  75. fractal((topx + rightx) / 2, (topy + righty) / 2, (leftx + rightx) / 2, (lefty + righty) / 2, rightx, righty);
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement