Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 5.80 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.awt.color.*;
  6. import java.awt.event.*;
  7. class Main extends JPanel
  8. {
  9.  
  10.     BufferedImage image; // RYSUJEMY W TYM BUFORZE A WYNIK KOPIUJEMY NA EKRAN
  11.     static Graphics2D gc; // "KONTEKST GRAFICZNY" ZWIĄZANY Z RYSOWANIEM W BUFORZE
  12.     JFrame ramka;
  13.    
  14.     public Main(int width, int height)
  15.     {
  16.         ramka = new JFrame();
  17.         Dimension d = new Dimension(width, height);
  18.         setPreferredSize(d);
  19.         setSize(d);
  20.  
  21.         image = new BufferedImage(getWidth(), getHeight(),
  22.                 BufferedImage.TYPE_INT_ARGB);
  23.         gc = image.createGraphics();
  24.         gc.setComposite(AlphaComposite.Src);
  25.  
  26.         this.setBackground(Color.white);
  27.         ramka.add(this);
  28.         ramka.pack();
  29.         ramka.setVisible(true);
  30.         this.gc.setColor(Color.WHITE);
  31.         this.gc.fillRect(0, 0, this.getWidth(), this.getHeight());
  32.         this.gc.setColor(Color.black);  
  33.     }
  34.  
  35.    
  36.     public static void putPixel(int x, int y)
  37.     {
  38.         gc.drawRect(x, y, 1, 1);
  39.    }
  40.  
  41.    public static void putPixe2(int x, int y)
  42.    {
  43.         gc.fillRect(x, y, 2, 2);
  44.    }
  45.    
  46.    
  47.     public void paintComponent(Graphics g)
  48.     {
  49.         super.paintComponent(g);
  50.         Graphics2D g2 = (Graphics2D) g;
  51.         g2.setComposite(AlphaComposite.Src);
  52.         g2.drawImage(image, null, 0, 0);
  53.     }
  54.    
  55.     public void Fraktal(Graphics g)
  56.     {
  57.         double xc   = 0;
  58.         double yc   = 0;
  59.         double size = 4;
  60.         Complex c = new Complex(-0.123,0.745);
  61.         int picturesize   = 600;
  62.         int max = 255;
  63.  
  64.         for (int i = 0; i < picturesize; i++)
  65.         {
  66.             for (int j = 0; j < picturesize; j++)
  67.             {
  68.                 System.out.println("jestem");
  69.  
  70.                 double x0 = xc - size/2 + size*i/picturesize;
  71.                 double y0 = yc - size/2 + size*j/picturesize;
  72.                 Complex z0 = new Complex(x0, y0);
  73.                 int gray = max - juli(z0, max,c);
  74.                 //int gray = max - mand(z0,max);
  75.                 Color color = new Color(gray, (gray+150)%255, (gray+300)%255);
  76.                 g.setColor(color);
  77.                 g.drawRect(i, picturesize-1-j, 1, 1);
  78.             }
  79.         }
  80.     }
  81.    
  82.     public int mand(Complex z0, int max)
  83.     {
  84.         Complex z = z0;
  85.         for (int t = 0; t < max; t++)
  86.         {
  87.             if (z.abs() > 2.0) return t;
  88.             z = z.times(z).plus(z0);
  89.         }
  90.         return max;
  91.     }
  92.    
  93.     public int juli(Complex z0, int max,Complex c)
  94.     {
  95.         Complex z = z0;
  96.         for (int t = 0; t < max; t++)
  97.         {
  98.             if (z.abs() > 2.0) return t;
  99.             z = z.times(z).plus(c);
  100.         }
  101.         return max;
  102.        
  103.     }
  104.    
  105.     public static void main(String args[])
  106.     {
  107.         Main m = new Main(600,600);
  108.         m.Fraktal(gc);
  109.         m.repaint();
  110.     }
  111.    
  112. }import javax.swing.*;
  113.  
  114. import java.awt.*;
  115. import java.awt.image.*;
  116. import java.awt.color.*;
  117. import java.awt.event.*;
  118. class Main extends JPanel
  119. {
  120.  
  121.     BufferedImage image; // RYSUJEMY W TYM BUFORZE A WYNIK KOPIUJEMY NA EKRAN
  122.     static Graphics2D gc; // "KONTEKST GRAFICZNY" ZWIĄZANY Z RYSOWANIEM W BUFORZE
  123.     JFrame ramka;
  124.    
  125.     public Main(int width, int height)
  126.     {
  127.         ramka = new JFrame();
  128.         Dimension d = new Dimension(width, height);
  129.         setPreferredSize(d);
  130.         setSize(d);
  131.  
  132.         image = new BufferedImage(getWidth(), getHeight(),
  133.                 BufferedImage.TYPE_INT_ARGB);
  134.         gc = image.createGraphics();
  135.         gc.setComposite(AlphaComposite.Src);
  136.  
  137.         this.setBackground(Color.white);
  138.         ramka.add(this);
  139.         ramka.pack();
  140.         ramka.setVisible(true);
  141.         this.gc.setColor(Color.WHITE);
  142.         this.gc.fillRect(0, 0, this.getWidth(), this.getHeight());
  143.         this.gc.setColor(Color.black);  
  144.     }
  145.  
  146.    
  147.     public static void putPixel(int x, int y)
  148.     {
  149.         gc.drawRect(x, y, 1, 1);
  150.    }
  151.  
  152.    public static void putPixe2(int x, int y)
  153.    {
  154.         gc.fillRect(x, y, 2, 2);
  155.    }
  156.    
  157.    
  158.     public void paintComponent(Graphics g)
  159.     {
  160.         super.paintComponent(g);
  161.         Graphics2D g2 = (Graphics2D) g;
  162.         g2.setComposite(AlphaComposite.Src);
  163.         g2.drawImage(image, null, 0, 0);
  164.     }
  165.    
  166.     public void Fraktal(Graphics g)
  167.     {
  168.         double xc   = 0;
  169.         double yc   = 0;
  170.         double size = 4;
  171.         Complex c = new Complex(-0.123,0.745);
  172.         int picturesize   = 600;
  173.         int max = 255;
  174.  
  175.         for (int i = 0; i < picturesize; i++)
  176.         {
  177.             for (int j = 0; j < picturesize; j++)
  178.             {
  179.                 System.out.println("jestem");
  180.  
  181.                 double x0 = xc - size/2 + size*i/picturesize;
  182.                 double y0 = yc - size/2 + size*j/picturesize;
  183.                 Complex z0 = new Complex(x0, y0);
  184.                 int gray = max - juli(z0, max,c);
  185.                 //int gray = max - mand(z0,max);
  186.                 Color color = new Color(gray, (gray+150)%255, (gray+300)%255);
  187.                 g.setColor(color);
  188.                 g.drawRect(i, picturesize-1-j, 1, 1);
  189.             }
  190.         }
  191.     }
  192.    
  193.     public int mand(Complex z0, int max)
  194.     {
  195.         Complex z = z0;
  196.         for (int t = 0; t < max; t++)
  197.         {
  198.             if (z.abs() > 2.0) return t;
  199.             z = z.times(z).plus(z0);
  200.         }
  201.         return max;
  202.     }
  203.    
  204.     public int juli(Complex z0, int max,Complex c)
  205.     {
  206.         Complex z = z0;
  207.         for (int t = 0; t < max; t++)
  208.         {
  209.             if (z.abs() > 2.0) return t;
  210.             z = z.times(z).plus(c);
  211.         }
  212.         return max;
  213.        
  214.     }
  215.    
  216.     public static void main(String args[])
  217.     {
  218.         Main m = new Main(600,600);
  219.         m.Fraktal(gc);
  220.         m.repaint();
  221.     }
  222.    
  223. }