/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/phasor/PlotGraph.java

1    package bookExamples.ch26Graphics.carl.phasor; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5     
6    /** 
7     * Created by IntelliJ IDEA. 
8     */ 
9    public class PlotGraph extends JFrame { 
10       JPanel plotPanel; 
11       JPanel controlPanel; 
12       JButton controlButton; 
13       Container c; 
14       double yArray[]; 
15       double xArray[]; 
16    
17       int iyArray[]; 
18       int ixArray[]; 
19    
20       public PlotGraph(String str) { 
21           super(str); 
22           Container cc = getContentPane(); 
23           setBackground(Color.red); 
24           setSize(100, 500); 
25           setVisible(true); 
26       } 
27    
28       /** 
29        * @param title 
30        * @param yArr 
31        * @param xStart 
32        * @param xStop 
33        * @param scale 
34        */ 
35    
36    
37       public PlotGraph(String title, double yArr[], double xStart, double xStop, double scale) { 
38           super(title); 
39           c = getContentPane(); 
40           setBackground(Color.yellow); 
41    
42           convertInputs(yArr, xStart, xStop, scale); 
43    
44           setSize(300, 300); 
45           setBackground(Color.MAGENTA); 
46           setVisible(true); 
47       } 
48    
49       private void convertInputs(double y[], double xStar, double xStop, double sca) { 
50           int n = y.length; 
51           int nSegs = y.length - 1; 
52           double fSegs = (float) nSegs; 
53           xArray = new double[n]; 
54           yArray = new double[n]; 
55           ixArray = new int[n]; 
56           iyArray = new int[n]; 
57    
58           xArray[0] = xStar; 
59           yArray[0] = y[0]; 
60           double increment = (xStop - xStar) / fSegs; 
61           for (int i = 1; i < n; i++) { 
62               xArray[i] = xArray[i - 1] + increment; 
63               yArray[i] = y[i]; 
64           } 
65    
66           for (int i = 0; i < n; i++) { 
67               ixArray[i] = (int) (xArray[i] * sca); 
68               iyArray[i] = (int) (yArray[i] * sca); 
69               System.out.println("ixArray = " + ixArray[i]); 
70               System.out.println("iyArray = " + iyArray[i]); 
71           } 
72       } 
73    
74    
75       public void paint(Graphics g) { 
76           g.setColor(Color.BLACK); 
77           g.drawPolyline(ixArray, iyArray, ixArray.length); 
78       } 
79    
80       public static void main(String[] args) { 
81           double sine[] = new double[50]; 
82           for (int i = 0; i < sine.length; i++) { 
83               sine[i] = 2. + Math.sin(i * 2 * Math.PI / 50); 
84               System.out.println("sine[i] = " + sine[i]); 
85           } 
86    
87           PlotGraph pg = new PlotGraph("This is PlotGraph", sine, 0, 2 * Math.PI, 50); 
88       } 
89    
90   } 
91