Advertisement
Guest User

Untitled

a guest
May 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package hr.tel.fer.kp.lab3;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.event.InputEvent;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.util.Arrays;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14.  
  15. import javax.swing.*;
  16.  
  17. public class MandelBasic extends JFrame implements MouseListener {
  18.  
  19. private static final long serialVersionUID = 48567845485868634L;
  20.  
  21. private static final int DEFAULT_NUMBER_OF_THREADS = 2;
  22.  
  23. private static int MAX = 64;
  24. private double viewX = 0.0;
  25. private double viewY = 0.0;
  26. private double zoom = 1.0;
  27. private int mouseX;
  28. private int mouseY;
  29.  
  30. private static LinkedList<Integer> orderOfExecution;
  31.  
  32.  
  33. public MandelBasic(String string) {
  34. super(string);
  35. }
  36.  
  37. public MandelBasic() {
  38. super();
  39. }
  40.  
  41. public static void main(String[] args) {
  42.  
  43. if(args.length == 1){
  44. orderOfExecution = new LinkedList<>();
  45. List<String> params = Arrays.asList(args[0].split(";"));
  46. boolean isValid = true;
  47. int p;
  48. for(String param : params){
  49. try{
  50. p = Integer.parseInt(param);
  51. }
  52. catch(NumberFormatException e){
  53. isValid = false;
  54. break;
  55. }
  56. if(p != 0){
  57. if(p < 0){
  58. p *= -1;
  59. for(int i = 1; i <= p; i++){
  60. orderOfExecution.add(i);
  61. }
  62. } else {
  63. orderOfExecution.add(p);
  64. }
  65. }
  66. }
  67. if(!isValid || orderOfExecution.isEmpty()){
  68. orderOfExecution.clear();
  69. orderOfExecution.add(DEFAULT_NUMBER_OF_THREADS);
  70. }
  71. }
  72.  
  73. MandelBasic frame = new MandelBasic("Mandelbrot fractal demo");
  74. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75. frame.setSize(640, 480);
  76. frame.setVisible(true);
  77. frame.addMouseListener(frame);
  78. }
  79.  
  80. @Override
  81. public void paint(Graphics g) {
  82. Dimension size = getSize();
  83. long start = System.currentTimeMillis();
  84.  
  85. if(!orderOfExecution.isEmpty()){
  86. int num = orderOfExecution.pop();
  87. int partY = size.height/num;
  88. int currY = 0;
  89.  
  90. ExecutorService service = Executors.newFixedThreadPool(num);
  91. while(currY < size.height){
  92. GraphicThread gt = new GraphicThread(g, size, currY, 0, currY + partY, size.width, zoom, viewX, viewY);
  93. service.submit(gt);
  94. currY += partY;
  95. }
  96. while(!service.isTerminated());
  97. long end = System.currentTimeMillis();
  98. System.out.println(end - start);
  99. try {
  100. Thread.sleep(1000);
  101. } catch (InterruptedException e) {
  102. e.printStackTrace();
  103. }
  104. if(!orderOfExecution.isEmpty()){
  105. clear();
  106. }
  107. }
  108. }
  109.  
  110. public void clear() {
  111. Graphics g = getGraphics();
  112. Dimension d = getSize();
  113. Color c = Color.WHITE;
  114.  
  115. g.setColor(c);
  116. g.fillRect(0, 0, d.width, d.height);
  117. repaint();
  118. }
  119.  
  120. @Override
  121. public void update(Graphics g) {
  122. paint(g);
  123. }
  124.  
  125. private int mandel(double px, double py) {
  126. int value = 0;
  127. double zx = 0.0;
  128. double zy = 0.0;
  129. double zx2 = 0.0;
  130. double zy2 = 0.0;
  131. while (value < MAX && zx2 + zy2 < 4.0) {
  132. zy = 2.0 * zx * zy + py;
  133. zx = zx2 - zy2 + px;
  134. zx2 = zx * zx;
  135. zy2 = zy * zy;
  136. value++;
  137. }
  138. return value == MAX ? 0 : MAX - value;
  139. }
  140.  
  141. @Override
  142. public void mouseClicked(MouseEvent e) {
  143. }
  144.  
  145. @Override
  146. public void mousePressed(MouseEvent e) {
  147. mouseX = e.getX();
  148. mouseY = e.getY();
  149. }
  150.  
  151. @Override
  152. public void mouseReleased(MouseEvent e) {
  153. clear();
  154. int x = e.getX();
  155. int y = e.getY();
  156. if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
  157. if (x != mouseX && y != mouseY) {
  158. int w = getSize().width;
  159. int h = getSize().height;
  160. viewX += zoom * Math.min(x, mouseX) / Math.min(w, h);
  161. viewY += zoom * Math.min(y, mouseY) / Math.min(w, h);
  162. zoom *= Math.max((double) Math.abs(x - mouseX) / w,
  163. (double) Math.abs(y - mouseY) / h);
  164. }
  165. } else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
  166. MAX = 64;
  167. viewX = viewY = 0.0;
  168. zoom = 1.0;
  169. } else {
  170. MAX += MAX / 4;
  171. }
  172. repaint();
  173. }
  174.  
  175. @Override
  176. public void mouseEntered(MouseEvent e) {
  177. }
  178.  
  179. @Override
  180. public void mouseExited(MouseEvent e) {
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement