Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 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.     GeneralPath path;
  25.    
  26.     private final Object lock = new Object();
  27.    
  28.     final JPanel panel = new JPanel(){
  29.         protected void paintComponent(Graphics g) {
  30.             super.paintComponent(g);
  31.            
  32.             Graphics2D g2 = (Graphics2D) g;
  33.            
  34.             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  35.            
  36.             g2.setPaint(Color.BLACK);
  37.             // x axis
  38.             g2.drawLine(0, (int) toCanvasY(0), panel.getWidth(), (int) toCanvasY(0));
  39.             // y axis
  40.             g2.drawLine((int) toCanvasX(0), 0, (int) toCanvasX(0), panel.getHeight());
  41.            
  42.  
  43.             g2.setPaint(Color.RED);
  44.             synchronized(lock) {
  45.                 if(path != null) g2.draw(path);
  46.             }
  47.         }
  48.     };
  49.    
  50.     public static void main(String[] args) {
  51.         SwingUtilities.invokeLater(() -> {
  52.             Sine sine = new Sine();
  53.            
  54.             sine.createAndShowGUI();
  55.            
  56.             sine.start();
  57.         });
  58.     }
  59.    
  60.     void createAndShowGUI() {
  61.         JFrame frame = new JFrame();
  62.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63.         frame.setTitle("Sine wave");
  64.        
  65.         panel.setBackground(Color.WHITE);
  66.        
  67.         frame.add(panel);
  68.         frame.setSize(500,300);
  69.         frame.setVisible(true);
  70.     }
  71.    
  72.     void start() {
  73.         Thread th = new Thread(() -> {
  74.             long count = 0;
  75.            
  76.             while( true ) {
  77.                 fillPath(count++ * X_SHIFT);
  78.                 panel.repaint();
  79.                
  80.                 try {
  81.                     Thread.sleep(X_SHIFT_TIME);
  82.                 } catch (Exception e) {
  83.                     e.printStackTrace();
  84.                 }
  85.             }
  86.         });
  87.         th.setDaemon(true);
  88.        
  89.         th.start();
  90.     }
  91.    
  92.     void fillPath(double xShift) {
  93.         GeneralPath p = new GeneralPath();
  94.        
  95.         p.moveTo(toCanvasX(X_MIN), toCanvasY(Math.sin(X_MIN + xShift)));
  96.        
  97.         for(int i=1; i<=N_POINTS; i++) {
  98.             double x = X_MIN + DX * i;
  99.             double y = Math.sin(x + xShift);
  100.            
  101.             p.lineTo(toCanvasX(x), toCanvasY(y));
  102.         }
  103.        
  104.         synchronized(lock) {
  105.             path = p;
  106.         }
  107.     }
  108.    
  109.     double toCanvasX(double x) {
  110.         double wPanel = (double) panel.getWidth();
  111.         double xw = X_MAX - X_MIN;
  112.        
  113.         return (x+(xw/2))*(wPanel/xw);
  114.     }
  115.    
  116.     double toCanvasY(double y) {
  117.         double hPanel = (double) panel.getHeight();
  118.         double yh = Y_MAX - Y_MIN;
  119.        
  120.         return (-y+(yh/2))*(hPanel/yh);
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement