Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hr.tel.fer.kp.lab3;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.InputEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import javax.swing.*;
- public class MandelBasic extends JFrame implements MouseListener {
- private static final long serialVersionUID = 48567845485868634L;
- private static final int DEFAULT_NUMBER_OF_THREADS = 2;
- private static int MAX = 64;
- private double viewX = 0.0;
- private double viewY = 0.0;
- private double zoom = 1.0;
- private int mouseX;
- private int mouseY;
- private static LinkedList<Integer> orderOfExecution;
- public MandelBasic(String string) {
- super(string);
- }
- public MandelBasic() {
- super();
- }
- public static void main(String[] args) {
- if(args.length == 1){
- orderOfExecution = new LinkedList<>();
- List<String> params = Arrays.asList(args[0].split(";"));
- boolean isValid = true;
- int p;
- for(String param : params){
- try{
- p = Integer.parseInt(param);
- }
- catch(NumberFormatException e){
- isValid = false;
- break;
- }
- if(p != 0){
- if(p < 0){
- p *= -1;
- for(int i = 1; i <= p; i++){
- orderOfExecution.add(i);
- }
- } else {
- orderOfExecution.add(p);
- }
- }
- }
- if(!isValid || orderOfExecution.isEmpty()){
- orderOfExecution.clear();
- orderOfExecution.add(DEFAULT_NUMBER_OF_THREADS);
- }
- }
- MandelBasic frame = new MandelBasic("Mandelbrot fractal demo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(640, 480);
- frame.setVisible(true);
- frame.addMouseListener(frame);
- }
- @Override
- public void paint(Graphics g) {
- Dimension size = getSize();
- long start = System.currentTimeMillis();
- if(!orderOfExecution.isEmpty()){
- int num = orderOfExecution.pop();
- int partY = size.height/num;
- int currY = 0;
- ExecutorService service = Executors.newFixedThreadPool(num);
- while(currY < size.height){
- GraphicThread gt = new GraphicThread(g, size, currY, 0, currY + partY, size.width, zoom, viewX, viewY);
- service.submit(gt);
- currY += partY;
- }
- while(!service.isTerminated());
- long end = System.currentTimeMillis();
- System.out.println(end - start);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if(!orderOfExecution.isEmpty()){
- clear();
- }
- }
- }
- public void clear() {
- Graphics g = getGraphics();
- Dimension d = getSize();
- Color c = Color.WHITE;
- g.setColor(c);
- g.fillRect(0, 0, d.width, d.height);
- repaint();
- }
- @Override
- public void update(Graphics g) {
- paint(g);
- }
- private int mandel(double px, double py) {
- int value = 0;
- double zx = 0.0;
- double zy = 0.0;
- double zx2 = 0.0;
- double zy2 = 0.0;
- while (value < MAX && zx2 + zy2 < 4.0) {
- zy = 2.0 * zx * zy + py;
- zx = zx2 - zy2 + px;
- zx2 = zx * zx;
- zy2 = zy * zy;
- value++;
- }
- return value == MAX ? 0 : MAX - value;
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- mouseX = e.getX();
- mouseY = e.getY();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- clear();
- int x = e.getX();
- int y = e.getY();
- if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
- if (x != mouseX && y != mouseY) {
- int w = getSize().width;
- int h = getSize().height;
- viewX += zoom * Math.min(x, mouseX) / Math.min(w, h);
- viewY += zoom * Math.min(y, mouseY) / Math.min(w, h);
- zoom *= Math.max((double) Math.abs(x - mouseX) / w,
- (double) Math.abs(y - mouseY) / h);
- }
- } else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
- MAX = 64;
- viewX = viewY = 0.0;
- zoom = 1.0;
- } else {
- MAX += MAX / 4;
- }
- repaint();
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement