Advertisement
Guest User

function graphs

a guest
Apr 12th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  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.     private final BufferedImage image;
  21.    
  22.     public static Func func = power(2);
  23.    
  24.     public Graph(int width, int height) {
  25.         setPreferredSize(new Dimension(width, height));
  26.         image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  27.        
  28.         Func easeInCubic = power(3);
  29.         Func easeOutCubic = easeInCubic.flipXY();
  30.         Func easeInOutCubic = easeInCubic.inOut();
  31.         Func easeInCirc = circ;
  32.         Func easeOutCirc = easeInCirc.flipXY();
  33.         Func easeInOutCirc = easeInCirc.inOut();
  34.         Func easeOutSine = sine;
  35.         Func easeInSine = easeOutSine.flipXY();
  36.         Func easeInOutSine = easeInSine.inOut();
  37.         func = easeInOutSine;
  38.     }
  39.    
  40.     public static Func power(final double e) {
  41.         return new Func() {
  42.             public double apply(double x) {
  43.                 return Math.pow(x, e);
  44.             }
  45.         };
  46.     }
  47.    
  48.     public static Func circ = new Func() {
  49.         public double apply(double x) {
  50.             return 1-Math.sqrt(1-x*x);
  51.         }
  52.     };
  53.    
  54.     public static Func sine = new Func() {
  55.         public double apply(double x) {
  56.             return Math.sin(x*Math.PI/2);
  57.         }
  58.     };
  59.    
  60.     public void setFunc(Func func) {
  61.         this.func = func;
  62.     }
  63.    
  64.     @Override
  65.     public void paint(Graphics _g) {
  66.         Graphics2D g = (Graphics2D)_g;
  67.         g.setColor(Color.WHITE);
  68.         g.fillRect(0, 0, getWidth(), getHeight());
  69.         g.setColor(Color.BLACK);
  70.         int lastX = 0, lastY = 0;
  71.         for(int ix = 0; ix < getWidth(); ix++) {
  72.             double x = ((double)ix)/getWidth();
  73.             double y = func.apply(x);
  74.             int iy = getHeight() - (int)(y*getHeight());
  75.             g.drawLine(lastX, lastY, ix, iy);
  76.             lastX = ix; lastY = iy;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement