Advertisement
Guest User

proof of concept fractal animation generation

a guest
Mar 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.90 KB | None | 0 0
  1. //This paste contains two files. Fraktale.java and BildBerechnung.java. The whole file is for testing purposes only and does not show "clean code" in any way. Goal of this proof of concept is to test ExecutorService to generate .png files which show a animation of "fractals".
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.Timer;
  5. import javax.swing.WindowConstants;
  6. import java.awt.Color;
  7. import java.awt.FlowLayout;
  8. import java.awt.Graphics;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.concurrent.Executors;
  13.  
  14. public class Fraktale extends JFrame implements ActionListener {
  15.  
  16.     public static int K = 2;
  17.     public static int WIDTH = 960 * K;
  18.     public static int HEIGHT = 540 * K;
  19.  
  20.     double cX = -0.6, cY = 0.6; // Julia Menge
  21.  
  22.     public static int GENAUIGKEIT = 100;
  23.     int fps = 62;
  24.     int ani_zeit = 10;
  25.     double varphi = 0;
  26.     double Loading_Bar = 0;
  27.  
  28.     int frames = fps * ani_zeit;
  29.     double wvelo = (2 * Math.PI) / frames;
  30.     double n = 3;
  31.  
  32.  
  33.     double millis = 0;
  34.     double secs = 0;
  35.     int indi = 0;
  36.  
  37.     private Timer timer = new Timer(15, this);
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.        new Fraktale();
  41.     }
  42.  
  43.     public Fraktale() throws Exception {
  44.  
  45.         ExecutorService executor = Executors.newFixedThreadPool(16);
  46.  
  47.         setTitle("Fraktale Mengen");
  48.         setSize(1080, 150);
  49.         setResizable(true);
  50.         setBackground(Color.WHITE);
  51.         setLocationRelativeTo(null);
  52.         setVisible(true);
  53.         setLayout(new FlowLayout());
  54.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  55.  
  56.         String qualitaet = getQualitaet();
  57.  
  58.         double dt = 0;
  59.         double t_akt = 0;
  60.         double verbeibende_spalten = frames * WIDTH;
  61.  
  62.         for (double u = 1; u <= frames; u++) {
  63.             BildBerechnung bildBerechnung = new BildBerechnung(timer, millis, varphi, dt, t_akt, verbeibende_spalten, u);
  64.             executor.execute(bildBerechnung);
  65.  
  66.             varphi += wvelo;
  67.  
  68. //            Loading_Bar = ((u / frames) * 980);
  69. //            repaint(); // Loading_Bar
  70.  
  71. //            System.out.print('\u000C');
  72. //            System.out.println("Animation Info: " + WIDTH + "x" + HEIGHT + "px" + "(" + K + "K)");
  73. //            System.out.println("Qualität: " + qualitaet);
  74. //            System.out.println("Farbtiefe: 24bit");
  75. //            System.out.println("Fps: " + fps);
  76. //            System.out.println("Frames (Anzahl): " + frames);
  77. //            System.out.println("Länge: " + ani_zeit + "s");
  78. //            System.out.println("Verbeleibende Frames:");
  79. //            System.out.println(frames - u);
  80.         }
  81.         dispose();
  82.         executor.shutdown();
  83.         while (!executor.isTerminated()) {
  84.             Thread.sleep(10000);
  85.             System.out.println("Wait on all threads to finish them.");
  86.         }
  87.         System.out.println("All threads finished");
  88.  
  89.     }
  90.  
  91.     private String getQualitaet() {
  92.         String qua = "";
  93.         if (GENAUIGKEIT > 0 && GENAUIGKEIT < 6) {
  94.             qua = "sehr schlecht";
  95.         } else if (GENAUIGKEIT > 5 && GENAUIGKEIT < 15) {
  96.             qua = "schlecht";
  97.         } else if (GENAUIGKEIT > 14 && GENAUIGKEIT < 30) {
  98.             qua = "mittel";
  99.         } else if (GENAUIGKEIT > 29 && GENAUIGKEIT < 50) {
  100.             qua = "hoch";
  101.         } else if (GENAUIGKEIT > 49 && GENAUIGKEIT < 100) {
  102.             qua = "sehr hoch";
  103.         } else if (GENAUIGKEIT > 99 && GENAUIGKEIT < 1000) {
  104.             qua = "Ultra hoch";
  105.         } else {
  106.             qua = "EXTREM Hoch";
  107.         }
  108.         return qua;
  109.     }
  110.  
  111.  
  112.     public void paint(Graphics g) {
  113.  
  114.         g.setColor(Color.GRAY);                   // Loading_Bar
  115.         g.drawRect(50, 75, 980, 15);
  116.         g.setColor(Color.GREEN);
  117.         g.fillRect(50, 75, (int) Loading_Bar, 15);
  118.  
  119.     }
  120.  
  121.  
  122.     public void actionPerformed(ActionEvent e) {
  123.         millis += 15;
  124.     }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. import javax.imageio.ImageIO;
  132. import javax.swing.Timer;
  133. import java.awt.Color;
  134. import java.awt.image.BufferedImage;
  135. import java.io.File;
  136. import java.io.IOException;
  137.  
  138. import static de.appsonoid.playground.fractal.Fraktale.GENAUIGKEIT;
  139. import static de.appsonoid.playground.fractal.Fraktale.HEIGHT;
  140. import static de.appsonoid.playground.fractal.Fraktale.WIDTH;
  141.  
  142. public class BildBerechnung implements Runnable {
  143.     private final double millis;
  144.     private double varphi;
  145.     private double dt;
  146.     private double t_akt;
  147.     private double verbeibende_spalten;
  148.     private double u;
  149.     private int indi = 0;
  150.  
  151.     private final int a = 200;
  152.     private final double[] fraktal = new double[2];
  153.  
  154.  
  155.     private Timer timer;
  156.     private double verbleibend_mil = 0;
  157.  
  158.     public BildBerechnung(Timer timer, double millis, double varphi, double dt, double t_akt, double verbeibende_spalten, double u) {
  159.         this.timer = timer;
  160.         this.millis = millis;
  161.         this.varphi = varphi;
  162.         this.dt = dt;
  163.         this.t_akt = t_akt;
  164.         this.verbeibende_spalten = verbeibende_spalten;
  165.         this.u = u;
  166.     }
  167.  
  168.     @Override
  169.     public void run() {
  170.         try {
  171.             this.invoke();
  172.         } catch (IOException e) {
  173.             e.printStackTrace();
  174.         }
  175.     }
  176.  
  177.     public double getDt() {
  178.         return dt;
  179.     }
  180.  
  181.     public double getT_akt() {
  182.         return t_akt;
  183.     }
  184.  
  185.     public double getVerbeibende_spalten() {
  186.         return verbeibende_spalten;
  187.     }
  188.  
  189.     private BildBerechnung invoke() throws IOException {
  190.         timer.start();
  191.  
  192.         BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  193.  
  194.         for (int x = 0; x < WIDTH; x++) {                     // Frame berechnung
  195.             for (int y = 0; y < HEIGHT; y++) {
  196.                 int dotcolor = punkt(GENAUIGKEIT, WIDTH, HEIGHT, x, y);
  197.                 image.setRGB(x, y, dotcolor);
  198.  
  199.                 if (x % a == 0 && y == 0) {
  200.                     indi++;
  201.                     dt = millis - t_akt + dt;
  202.                     double dtt = dt / indi;
  203.                     t_akt = millis;
  204.                     verbleibend_mil = (dtt * verbeibende_spalten) / a;
  205.  
  206.                     int j = SEC_To_HMS(verbleibend_mil, "j");
  207.                     int m = SEC_To_HMS(verbleibend_mil, "mo");
  208.                     int t = SEC_To_HMS(verbleibend_mil, "t");
  209.                     int h = SEC_To_HMS(verbleibend_mil, "h");
  210.                     int min = SEC_To_HMS(verbleibend_mil, "m");
  211.                     int sec = SEC_To_HMS(verbleibend_mil, "s");
  212.  
  213.  
  214.                     verbeibende_spalten -= a;
  215.                 }
  216.  
  217.             }
  218.         }
  219.  
  220.  
  221.  
  222.         ImageIO.write(image, "png", new File("Mandelbrot_" + u + ".png"));  // Bild als .png schreiben
  223.  
  224.         return this;
  225.     }
  226.  
  227.     private int punkt(int iter, int width, int height, int xcoor, int ycoor) {
  228.  
  229.         double moveX = 0, moveY = 0, zoom = 0.64;
  230.  
  231.         double a = 1.5 * (xcoor - width / 2) / (0.5 * zoom * width) + moveX;//(xcoor - width/2)*4.0/width;
  232.         double b = (ycoor - height / 2) / (0.5 * zoom * height) + moveY;//(ycoor - height/2)*4.0/width;
  233.  
  234.         double x = 0, y = 0;
  235.         int actual_iter = 0;
  236.  
  237.  
  238.         while (actual_iter < iter && a * a + b * b < 4) {
  239.  
  240.             double cX = Math.cos(varphi);
  241.             double cY = Math.sin(varphi);
  242.             fraktal[0] = (a * a) - (b * b) + cX; // Fraktal 0: f(z) = z² + c
  243.             fraktal[1] = (2 * a * b) + cY;
  244.  
  245.             double x_new;
  246.  
  247.             x_new = fraktal[0];
  248.             b = fraktal[1];
  249.             a = x_new;
  250.  
  251.             actual_iter++;
  252.         }
  253.  
  254.         if (actual_iter < iter) {
  255.             return farbverteilung(actual_iter);
  256.         }
  257.  
  258.         return 0;
  259.     }
  260.  
  261.     private int SEC_To_HMS(double secinput, String einheit) {
  262.         int secinpu = (int) (secinput / 1000);
  263.         int sec = secinpu % 60;
  264.         int h = (int) Math.floor(secinpu / 3600);
  265.         int min = (secinpu - (3600 * h) - sec) / 60;
  266.         int t = (int) Math.floor(h / 24);
  267.         h = h % 24;
  268.         int m = (int) Math.floor(t / 30);
  269.         t = t % 30;
  270.         int j = (int) Math.floor(m / 12);
  271.         m = m % 12;
  272.  
  273.         if (einheit == "h") {
  274.             return h;
  275.         } else if (einheit == "m") {
  276.             return min;
  277.         } else if (einheit == "j") {
  278.             return j;
  279.         } else if (einheit == "mo") {
  280.             return m;
  281.         } else if (einheit == "t") {
  282.             return t;
  283.         } else if (einheit == "s") {
  284.             return sec;
  285.         }
  286.         return 0;
  287.     }
  288.  
  289.     private int farbverteilung(int index) {
  290.  
  291.         int colors[] = new int[GENAUIGKEIT];
  292.  
  293.         for (int i = 0; i < GENAUIGKEIT; i++) {
  294.             colors[i] = Color.HSBtoRGB(i / 256f, 1, i / (i + 8f)); //farbverteilung nach HSB Prinzip und der maximalen Iterationen
  295.         }
  296.  
  297.         return colors[index];
  298.     }
  299.  
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement