/Users/lyon/j4p/src/net/stocks/QuotePanel.java

1    package net.stocks; 
2     
3    import gui.In; 
4     
5    import javax.swing.JPanel; 
6    import java.awt.Color; 
7    import java.awt.Dimension; 
8    import java.awt.FlowLayout; 
9    import java.awt.Frame; 
10   import java.awt.Graphics; 
11   import java.util.Vector; 
12    
13   /** 
14    * DocJava, Inc. 
15    * http://www.docjava.com 
16    * Programmer: dlyon 
17    * Date: Nov 10, 2004 
18    * Time: 6:31:35 PM 
19    */ 
20   public class QuotePanel extends JPanel { 
21       String symbol; 
22       Quote q; 
23       int x1 = 0; 
24       int y1 = 0; 
25       Vector history = new Vector(); 
26    
27       public void add(Quote q) { 
28           history.addElement(q); 
29       } 
30    
31       public Quote[] getQuotes() { 
32           Quote qa[] = new Quote[history.size()]; 
33           history.copyInto(qa); 
34           return qa; 
35       } 
36    
37       public QuotePanel(String symbol) { 
38           this.symbol = symbol; 
39           q = Quote.makeQuote(symbol); 
40           add(q); 
41           System.out.println(q); 
42           setLayout(new FlowLayout()); 
43       } 
44    
45       public void paint(Graphics g) { 
46           Quote qa [] = getQuotes(); 
47           for (int i = 0; i < qa.length; i++) { 
48               /// do your drawing here.... 
49               //drawQuote(qa[i]); 
50           } 
51           float p = q.getPrice(); 
52           p = (float) Math.random() * 100; 
53           int y2 = Math.round(p); 
54           int x2 = x1 + 1; 
55           g.drawLine(x1, y1, x2, y2); 
56           long v = q.getVolume() / 100000; // cause numbers come out in millions 
57           Dimension d = getSize(); 
58           int w = d.width; 
59           int h = d.height; 
60           g.drawLine(x1, h - 50, x1, (int) (h - 50 - Math.abs(p))); 
61           g.setColor(Color.green); 
62           g.drawLine(0, h - 50, w, h - 50); 
63           g.drawLine(0, h - 50, 0, 0); 
64           g.setColor(Color.black); 
65           x1 = x2; 
66           y1 = y2; 
67           q = Quote.makeQuote(symbol); 
68           add(q); 
69           repaint(); 
70       } 
71    
72       public Dimension getPreferredSize() { 
73           return new Dimension(400, 400); 
74       } 
75    
76       public static void main(String[] args) { 
77           QuotePanel qp = new QuotePanel(In.getString("enter quote")); 
78           Frame cf = new Frame(); 
79           cf.setLayout(new FlowLayout()); 
80           cf.add(qp); 
81           cf.setSize(400, 400); 
82           cf.pack(); 
83           cf.show(); 
84       } 
85   } 
86