Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. public class Mandelbrot extends JFrame implements ActionListener {
  7.  
  8. private JPanel ctrlPanel;
  9. private JPanel btnPanel;
  10. private int numIter = 50;
  11. private double zoom = 130;
  12. private double zoomIncrease = 100;
  13. private int colorIter = 20;
  14. private BufferedImage I;
  15. private double zx, zy, cx, cy, temp;
  16. private int xMove, yMove = 0;
  17. private JButton[] ctrlBtns = new JButton[9];
  18. private Color themeColor = new Color(150,180,200);
  19.  
  20. public Mandelbrot() {
  21. super("Mandelbrot Set");
  22. setBounds(100, 100, 800, 600);
  23. setResizable(false);
  24. setDefaultCloseOperation(EXIT_ON_CLOSE);
  25. plotPoints();
  26.  
  27. Container contentPane = getContentPane();
  28.  
  29. contentPane.setLayout(null);
  30.  
  31.  
  32.  
  33.  
  34. ctrlPanel = new JPanel();
  35. ctrlPanel.setBounds(600,0,200,600);
  36. ctrlPanel.setBackground(themeColor);
  37. ctrlPanel.setLayout(null);
  38.  
  39. btnPanel = new JPanel();
  40. btnPanel.setBounds(0,200,200,200);
  41. btnPanel.setLayout(new GridLayout(3,3));
  42. btnPanel.setBackground(themeColor);
  43.  
  44. ctrlBtns[1] = new JButton("up");
  45. ctrlBtns[7] = new JButton("down");
  46. ctrlBtns[3] = new JButton ("left");
  47. ctrlBtns[5] = new JButton("right");
  48. ctrlBtns[2] = new JButton("+");
  49. ctrlBtns[0] = new JButton("-");
  50. ctrlBtns[8] = new JButton(">");
  51. ctrlBtns[6] = new JButton("<");
  52. ctrlBtns[4] = new JButton();
  53.  
  54. contentPane.add(ctrlPanel);
  55. contentPane.add(new imgPanel());
  56. ctrlPanel.add(btnPanel);
  57.  
  58. for (int x = 0; x<ctrlBtns.length;x++){
  59. btnPanel.add(ctrlBtns[x]);
  60. ctrlBtns[x].addActionListener(this);
  61. }
  62.  
  63. validate();
  64.  
  65. }
  66.  
  67. public class imgPanel extends JPanel{
  68. public imgPanel(){
  69. setBounds(0,0,600,600);
  70.  
  71. }
  72.  
  73. @Override
  74. public void paint (Graphics g){
  75. super.paint(g);
  76. g.drawImage(I, 0, 0, this);
  77. }
  78. }
  79.  
  80. public void plotPoints(){
  81. I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  82. for (int y = 0; y < getHeight(); y++) {
  83. for (int x = 0; x < getWidth(); x++) {
  84. zx = zy = 0;
  85. cx = (x - 320+xMove) / zoom;
  86. cy = (y - 290+yMove) / zoom;
  87. int iter = numIter;
  88. while (zx * zx + zy * zy < 4 && iter > 0) {
  89. temp = zx * zx - zy * zy + cx;
  90. zy = 2 * zx * zy + cy;
  91. zx = temp;
  92. iter--;
  93. }
  94. I.setRGB(x, y, iter | (iter << colorIter));
  95. }
  96. }
  97. }
  98.  
  99. public void actionPerformed(ActionEvent ae){
  100. String event = ae.getActionCommand();
  101.  
  102. switch (event){
  103. case "up":
  104. yMove-=100;
  105. break;
  106. case "down":
  107. yMove+=100;
  108. break;
  109. case "left":
  110. xMove-=100;
  111. break;
  112. case "right":
  113. xMove+=100;
  114. break;
  115. case "+":
  116. zoom+=zoomIncrease;
  117. zoomIncrease+=100;
  118. break;
  119. case "-":
  120. zoom-=zoomIncrease;
  121. zoomIncrease-=100;
  122. break;
  123. case ">":
  124. colorIter++;
  125. break;
  126. case "<":
  127. colorIter--;
  128. break;
  129. }
  130.  
  131.  
  132.  
  133. plotPoints();
  134. validate();
  135. repaint();
  136. }
  137.  
  138.  
  139.  
  140.  
  141. public static void main(String[] args) {
  142. new Mandelbrot().setVisible(true);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement