Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.RenderingHints;
  5. import java.awt.geom.GeneralPath;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.SwingUtilities;
  10.  
  11. public class Sine {
  12.     final double X_MIN = -2*Math.PI;
  13.     final double X_MAX = 2*Math.PI;
  14.    
  15.     final double Y_MIN = -1.2;
  16.     final double Y_MAX = 1.2;
  17.    
  18.     final double X_SHIFT      = 0.2;
  19.     final long   X_SHIFT_TIME = 50; //ms
  20.    
  21.     final double N_POINTS = 1000;
  22.     final double DX    = (X_MAX-X_MIN)/(double) N_POINTS;
  23.    
  24.     final GeneralPath path = new GeneralPath();
  25.    
  26.     final JPanel panel = new JPanel(){
  27.         protected void paintComponent(Graphics g) {
  28.             super.paintComponent(g);
  29.            
  30.             Graphics2D g2 = (Graphics2D) g;
  31.            
  32.             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  33.            
  34.             g2.setPaint(Color.RED);
  35.            
  36.             g2.draw(path);
  37.         }
  38.     };
  39.    
  40.     public static void main(String[] args) {
  41.         SwingUtilities.invokeLater(() -> {
  42.             Sine sine = new Sine();
  43.            
  44.             sine.createAndShowGUI();
  45.            
  46.             sine.start();
  47.         });
  48.     }
  49.    
  50.     void createAndShowGUI() {
  51.         JFrame frame = new JFrame();
  52.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53.         frame.setTitle("Sine wave");
  54.        
  55.         panel.setBackground(Color.WHITE);
  56.        
  57.         frame.add(panel);
  58.         frame.setSize(500,300);
  59.         frame.setVisible(true);
  60.     }
  61.    
  62.     void start() {
  63.         Thread th = new Thread(() -> {
  64.             long count = 0;
  65.            
  66.             while( true ) {
  67.                 fillPath(count++ * X_SHIFT);
  68.                 panel.repaint();
  69.                
  70.                 try {
  71.                     Thread.sleep(X_SHIFT_TIME);
  72.                 } catch (Exception e) {
  73.                     e.printStackTrace();
  74.                 }
  75.             }
  76.         });
  77.         th.setDaemon(true);
  78.        
  79.         th.start();
  80.     }
  81.    
  82.     void fillPath(double xShift) {
  83.         path.reset();
  84.        
  85.         path.moveTo(toCanvasX(X_MIN), toCanvasY(Math.sin(X_MIN + xShift)));
  86.        
  87.         for(int i=1; i<N_POINTS; i++) {
  88.             double x = X_MIN + DX * i;
  89.             double y = Math.sin(x + xShift);
  90.            
  91.             path.lineTo(toCanvasX(x), toCanvasY(y));
  92.         }
  93.     }
  94.    
  95.     double toCanvasX(double x) {
  96.         double wPanel = (double) panel.getWidth();
  97.         double xw = X_MAX - X_MIN;
  98.        
  99.         return (x+(xw/2))*(wPanel/xw);
  100.     }
  101.    
  102.     double toCanvasY(double y) {
  103.         double hPanel = (double) panel.getHeight();
  104.         double yh = Y_MAX - Y_MIN;
  105.        
  106.         return (-y+(yh/2))*(hPanel/yh);
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement