daily pastebin goal
61%
SHARE
TWEET

function graphs

a guest Apr 12th, 2014 44 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.image.BufferedImage;
  8.  
  9. public class Graph extends JPanel {
  10.     public static void main(String[] args) {
  11.         JFrame frame = new JFrame();
  12.         Graph graph = new Graph(800, 600);
  13.         graph.setPreferredSize(new Dimension(800, 600));
  14.         frame.add(graph);
  15.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         frame.pack();
  17.         frame.setVisible(true);
  18.     }
  19.    
  20.     public static Func func = power(2);
  21.    
  22.     public Graph(int width, int height) {
  23.         setPreferredSize(new Dimension(width, height));
  24.        
  25.         Func easeInCubic = power(3);
  26.         Func easeOutCubic = easeInCubic.flipXY();
  27.         Func easeInOutCubic = easeInCubic.inOut();
  28.         Func easeInCirc = circ;
  29.         Func easeOutCirc = easeInCirc.flipXY();
  30.         Func easeInOutCirc = easeInCirc.inOut();
  31.         Func easeOutSine = sine;
  32.         Func easeInSine = easeOutSine.flipXY();
  33.         Func easeInOutSine = easeInSine.inOut();
  34.         func = easeInOutSine;
  35.     }
  36.    
  37.     public static Func power(final double e) {
  38.         return new Func() {
  39.             public double apply(double x) {
  40.                 return Math.pow(x, e);
  41.             }
  42.         };
  43.     }
  44.    
  45.     public static Func circ = new Func() {
  46.         public double apply(double x) {
  47.             return 1-Math.sqrt(1-x*x);
  48.         }
  49.     };
  50.    
  51.     public static Func sine = new Func() {
  52.         public double apply(double x) {
  53.             return Math.sin(x*Math.PI/2);
  54.         }
  55.     };
  56.    
  57.     public void setFunc(Func func) {
  58.         this.func = func;
  59.     }
  60.    
  61.     @Override
  62.     public void paint(Graphics _g) {
  63.         Graphics2D g = (Graphics2D)_g;
  64.         g.setColor(Color.WHITE);
  65.         g.fillRect(0, 0, getWidth(), getHeight());
  66.         g.setColor(Color.BLACK);
  67.         int lastX = 0, lastY = 0;
  68.         for(int ix = 0; ix < getWidth(); ix++) {
  69.             double x = ((double)ix)/getWidth();
  70.             double y = func.apply(x);
  71.             int iy = getHeight() - (int)(y*getHeight());
  72.             g.drawLine(lastX, lastY, ix, iy);
  73.             lastX = ix; lastY = iy;
  74.         }
  75.     }
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top