Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Chart extends JFrame implements Runnable
- {
- public static final int AREA_RANGE = 400;
- public static final double COLOR_RANGE = 5.0;
- public static final double MAX_AXIS = 10.0;
- private static final long serialVersionUID = -2607877189532852723L;
- public static void main(String[] args)
- {
- try{
- (new Chart()).run();
- } catch (Exception e) {e.printStackTrace();}
- }
- PaintCanvas c;
- public Chart()
- {
- this.setSize(2*AREA_RANGE + 1, 2*AREA_RANGE + 1);
- this.setVisible(true);
- c = new PaintCanvas(this);
- c.setPreferredSize(new Dimension(2*AREA_RANGE + 1, 2*AREA_RANGE + 1));
- this.add(c);
- this.pack();
- }
- @Override
- public void run()
- {
- while (true)
- {
- try {
- Thread.sleep(16);
- } catch (InterruptedException e) {}
- c.repaint();
- }
- }
- }
- class PaintCanvas extends JPanel
- {
- private static final long serialVersionUID = 4255091824970101848L;
- Chart chart;
- private BufferedImage display;
- public PaintCanvas(Chart c)
- {
- chart = c;
- display = (BufferedImage) chart.createImage(2*Chart.AREA_RANGE + 1, 2*Chart.AREA_RANGE + 1);
- init();
- }
- public void init()
- {
- int AREA_RANGE = Chart.AREA_RANGE;
- double COLOR_RANGE = Chart.COLOR_RANGE;
- double DIV_VALUE = Chart.AREA_RANGE / Chart.MAX_AXIS;
- Graphics g = display.getGraphics();
- for (int x = -AREA_RANGE; x <= AREA_RANGE; x += 1)
- {
- for (int y = -AREA_RANGE; y <= AREA_RANGE; y += 1)
- {
- double a = Math.pow(x/DIV_VALUE, y/DIV_VALUE);
- double b = Math.pow(y/DIV_VALUE, x/DIV_VALUE);
- double diff = a - b;
- diff = Clamp(-COLOR_RANGE, diff, COLOR_RANGE) * (127.0/COLOR_RANGE); //Math.copySign(1, diff) * Math.log(Math.abs(diff)+10)
- if (diff < 0.0) g.setColor(new Color(127, 127, (int)-(diff-127)));
- else if (diff > 0.0) g.setColor(new Color((int) diff+127, 127, 127));
- else g.setColor(new Color(127, 127, 127));
- if (Double.isNaN(a) || Double.isNaN(b)) g.setColor(new Color(63, 63, 63));
- g.fillRect(x+AREA_RANGE, AREA_RANGE-y, 1, 1);
- }
- }
- File outputfile = new File("/home/brandon/Exp.png");
- try {
- ImageIO.write(display, "png", outputfile);
- } catch (IOException e) {e.printStackTrace();}
- }
- @Override
- public void paintComponent(Graphics rg)
- {
- rg.drawImage(display, 0, 0, chart);
- }
- public double Clamp(double min, double val, double max)
- {
- if (val < min) return min;
- if (val > max) return max;
- return val;
- }
- /*
- public int Clamp(int min, int val, int max)
- {
- if (val < min) return min;
- if (val > max) return max;
- return val;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement