Advertisement
thepowderguy

Exponentation

Feb 17th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8.  
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13.  
  14. public class Chart extends JFrame implements Runnable
  15. {
  16.     public static final int AREA_RANGE = 400;
  17.     public static final double COLOR_RANGE = 5.0;
  18.     public static final double MAX_AXIS = 10.0;
  19.    
  20.     private static final long serialVersionUID = -2607877189532852723L;
  21.  
  22.     public static void main(String[] args)
  23.     {
  24.         try{
  25.          (new Chart()).run();
  26.         } catch (Exception e) {e.printStackTrace();}
  27.     }
  28.    
  29.     PaintCanvas c;
  30.    
  31.     public Chart()
  32.     {
  33.         this.setSize(2*AREA_RANGE + 1, 2*AREA_RANGE + 1);
  34.         this.setVisible(true);
  35.         c = new PaintCanvas(this);
  36.         c.setPreferredSize(new Dimension(2*AREA_RANGE + 1, 2*AREA_RANGE + 1));
  37.         this.add(c);
  38.         this.pack();
  39.     }
  40.    
  41.     @Override
  42.     public void run()
  43.     {
  44.         while (true)
  45.         {
  46.             try {
  47.                 Thread.sleep(16);
  48.             } catch (InterruptedException e) {}
  49.             c.repaint();
  50.         }
  51.     }
  52. }
  53.  
  54. class PaintCanvas extends JPanel
  55. {
  56.     private static final long serialVersionUID = 4255091824970101848L;
  57.     Chart chart;
  58.     private BufferedImage display;
  59.    
  60.     public PaintCanvas(Chart c)
  61.     {
  62.         chart = c;
  63.         display = (BufferedImage) chart.createImage(2*Chart.AREA_RANGE + 1, 2*Chart.AREA_RANGE + 1);
  64.         init();
  65.     }
  66.    
  67.     public void init()
  68.     {
  69.         int AREA_RANGE = Chart.AREA_RANGE;
  70.         double COLOR_RANGE = Chart.COLOR_RANGE;
  71.         double DIV_VALUE  = Chart.AREA_RANGE / Chart.MAX_AXIS;
  72.        
  73.         Graphics g = display.getGraphics();
  74.         for (int x = -AREA_RANGE; x <= AREA_RANGE; x += 1)
  75.         {
  76.             for (int y = -AREA_RANGE; y <= AREA_RANGE; y += 1)
  77.             {
  78.                 double a = Math.pow(x/DIV_VALUE, y/DIV_VALUE);
  79.                 double b = Math.pow(y/DIV_VALUE, x/DIV_VALUE);
  80.                 double diff = a - b;
  81.                 diff = Clamp(-COLOR_RANGE, diff, COLOR_RANGE) * (127.0/COLOR_RANGE); //Math.copySign(1, diff) * Math.log(Math.abs(diff)+10)
  82.                 if (diff < 0.0) g.setColor(new Color(127, 127, (int)-(diff-127)));
  83.                 else if (diff > 0.0) g.setColor(new Color((int) diff+127, 127, 127));
  84.                 else g.setColor(new Color(127, 127, 127));
  85.                 if (Double.isNaN(a) || Double.isNaN(b)) g.setColor(new Color(63, 63, 63));
  86.                 g.fillRect(x+AREA_RANGE, AREA_RANGE-y, 1, 1);
  87.             }
  88.         }
  89.        
  90.         File outputfile = new File("/home/brandon/Exp.png");
  91.         try {
  92.             ImageIO.write(display, "png", outputfile);
  93.         } catch (IOException e) {e.printStackTrace();}
  94.     }
  95.    
  96.     @Override
  97.     public void paintComponent(Graphics rg)
  98.     {
  99.         rg.drawImage(display, 0, 0, chart);
  100.     }
  101.    
  102.     public double Clamp(double min, double val, double max)
  103.     {
  104.         if (val < min) return min;
  105.         if (val > max) return max;
  106.         return val;
  107.     }
  108.     /*
  109.     public int Clamp(int min, int val, int max)
  110.     {
  111.         if (val < min) return min;
  112.         if (val > max) return max;
  113.         return val;
  114.     }*/
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement