77
88public class GraphingData extends JPanel {
99
10- public int [] getData () {
11- int [] data = new int [100000 ];
12- for (int i = 0 ; i < data .length ; i ++) {
13- data [i ] = GraphingData .getRandInt (0 ,99999999 );
14- };
15- return data ;
16- }
17-
1810 final int LMARGIN = 30 ;
1911 final int RMARGIN = 10 ;
2012 final int TMARGIN = 10 ;
@@ -24,27 +16,11 @@ public int[] getData() {
2416 float [] xdata = new float [199 ];
2517 float [] ydata = new float [199 ];
2618
27- protected void paintComponent (Graphics g ) {
28- Scanner sc = new Scanner (System .in );
29- int idx = 0 ;
30- while (sc .hasNextFloat ()) {
31- xdata [idx ] = sc .nextFloat ();
32- ydata [idx ] = sc .nextFloat ();
33- idx ++;
34- }
35- //System.out.println(Arrays.toString(xdata));
36- //System.out.println(Arrays.toString(ydata));
37- int [] data = getData ();
38- super .paintComponent (g );
39- Graphics2D g2 = (Graphics2D )g ;
40- g2 .setRenderingHint (RenderingHints .KEY_ANTIALIASING ,
41- RenderingHints .VALUE_ANTIALIAS_ON );
19+ protected void drawOrdinate (Graphics2D g2 ) {
4220 int w = getWidth ();
4321 int h = getHeight ();
44- System .out .println ("width=" + w + " height " + h );
4522 Font font = g2 .getFont ();
4623 FontRenderContext frc = g2 .getFontRenderContext ();
47- // Draw ordinate.
4824 g2 .draw (new Line2D .Double (LMARGIN , TMARGIN , LMARGIN , h -BMARGIN ));
4925 int tick_label = 0 ;
5026 for (int i = h - BMARGIN ; i >= TMARGIN ; i -=(h /30 )) {
@@ -57,7 +33,13 @@ protected void paintComponent(Graphics g) {
5733 g2 .drawString (letter , sx , sy );
5834 tick_label += 10 ;
5935 }
60- // Draw abcissa.
36+ }
37+
38+ protected void drawAbcissa (Graphics2D g2 ) {
39+ int w = getWidth ();
40+ int h = getHeight ();
41+ Font font = g2 .getFont ();
42+ FontRenderContext frc = g2 .getFontRenderContext ();
6143 g2 .draw (new Line2D .Double (LMARGIN , h -LMARGIN , w -RMARGIN , h -LMARGIN ));
6244 int step = 0 ;
6345 float x_tick_label = xdata [step ];
@@ -73,8 +55,28 @@ protected void paintComponent(Graphics g) {
7355 step += 2 ;
7456 x_tick_label = xdata [step ];
7557 }
76- // Draw Labels
77- // Ordinate label.
58+
59+ }
60+
61+ protected void drawLine (Graphics2D g2 ) {
62+ int w = getWidth ();
63+ int h = getHeight ();
64+ g2 .setPaint (Color .blue );
65+ for (int i = 0 ; i < 198 ; i ++) {
66+ double x1 = LMARGIN + (i * 10 ) ;
67+ double y1 = h - BMARGIN - (ydata [i ] * h /300 );
68+ double x2 = LMARGIN + (i + 1 ) * 10 ;
69+ double y2 = h - BMARGIN - (ydata [i +1 ] * h /300 );
70+ //System.out.println("x1=" + x1 + " y1=" + y1 + " x2=" + x2 + " y2=" + y2);
71+ g2 .draw (new Line2D .Double (x1 , y1 , x2 , y2 ));
72+ }
73+ }
74+
75+ protected void drawOrdinateLabel (Graphics2D g2 ) {
76+ int w = getWidth ();
77+ int h = getHeight ();
78+ Font font = g2 .getFont ();
79+ FontRenderContext frc = g2 .getFontRenderContext ();
7880 String s = "y axis" ;
7981 LineMetrics lm = font .getLineMetrics ("0" , frc );
8082 float sh = lm .getAscent () + lm .getDescent ();
@@ -87,25 +89,26 @@ protected void paintComponent(Graphics g) {
8789 g2 .drawString (letter , sx , sy );
8890 sy += sh ;
8991 }
90- // Abcissa label.
91- s = "x axis" ;
92- sy = h - PAD + (PAD - sh )/2 + lm .getAscent ();
92+ }
93+
94+ protected void drawAbcissaLabel (Graphics2D g2 ) {
95+ int w = getWidth ();
96+ int h = getHeight ();
97+ Font font = g2 .getFont ();
98+ FontRenderContext frc = g2 .getFontRenderContext ();
99+ String s = "x axis" ;
100+ LineMetrics lm = font .getLineMetrics ("0" , frc );
101+ float sh = lm .getAscent () + lm .getDescent ();
102+ float sy = h - PAD + (PAD - sh )/2 + lm .getAscent ();
93103 float sw = (float )font .getStringBounds (s , frc ).getWidth ();
94104 float sx = (w - sw )/2 ;
95105 g2 .setFont (new Font ("SanSerif" , Font .PLAIN , 12 ));
96106 g2 .drawString (s , sx , sy );
97- // Draw Line
98- g2 .setPaint (Color .blue );
99- for (int i = 0 ; i < 198 ; i ++) {
100- double x1 = LMARGIN + (i * 10 ) ;
101- double y1 = h - BMARGIN - (ydata [i ] * h /300 );
102- double x2 = LMARGIN + (i + 1 ) * 10 ;
103- double y2 = h - BMARGIN - (ydata [i +1 ] * h /300 );
104-
105- //System.out.println("x1=" + x1 + " y1=" + y1 + " x2=" + x2 + " y2=" + y2);
106- g2 .draw (new Line2D .Double (x1 , y1 , x2 , y2 ));
107- }
108- // Mark data points.
107+ }
108+
109+ protected void drawDataPoints (Graphics2D g2 ) {
110+ int w = getWidth ();
111+ int h = getHeight ();
109112 g2 .setPaint (Color .red );
110113 for (int i = 0 ; i < 199 ; i ++) {
111114 double x = LMARGIN + i * 10 ;
@@ -115,27 +118,48 @@ protected void paintComponent(Graphics g) {
115118 }
116119 }
117120
118- private int getMax () {
119- int [] data = getData ();
120- int max = -Integer .MAX_VALUE ;
121- for (int i = 0 ; i < data .length ; i ++) {
122- if (data [i ] > max )
123- max = data [i ];
121+ protected void paintComponent (Graphics g ) {
122+ Scanner sc = new Scanner (System .in );
123+ int idx = 0 ;
124+ while (sc .hasNextFloat ()) {
125+ xdata [idx ] = sc .nextFloat ();
126+ ydata [idx ] = sc .nextFloat ();
127+ idx ++;
124128 }
125- return max ;
129+ //System.out.println(Arrays.toString(xdata));
130+ //System.out.println(Arrays.toString(ydata));
131+ super .paintComponent (g );
132+ Graphics2D g2 = (Graphics2D )g ;
133+ g2 .setRenderingHint (RenderingHints .KEY_ANTIALIASING ,
134+ RenderingHints .VALUE_ANTIALIAS_ON );
135+ int w = getWidth ();
136+ int h = getHeight ();
137+ System .out .println ("width=" + w + " height " + h );
138+ Font font = g2 .getFont ();
139+ FontRenderContext frc = g2 .getFontRenderContext ();
140+ // Draw ordinate.
141+ drawOrdinate (g2 );
142+ // Draw abcissa.
143+ drawAbcissa (g2 );
144+ // Draw Labels
145+ // Ordinate label.
146+ drawOrdinateLabel (g2 );
147+ // Abcissa label.
148+ drawAbcissaLabel (g2 );
149+ // Draw Line
150+ drawLine (g2 );
151+ // Draw data points.
152+ drawDataPoints (g2 );
126153 }
127154
128155 public static void main (String [] args ) {
129156 JFrame f = new JFrame ();
157+ f .setTitle ("Total Sun Spot Number From 1818: Source SILSO" );
130158 f .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
131159 f .add (new GraphingData ());
132160 f .setExtendedState ( f .getExtendedState ()|JFrame .MAXIMIZED_BOTH );
133161 f .setSize (2048 ,1152 );
134162 f .setLocation (0 ,0 );
135163 f .setVisible (true );
136164 }
137-
138- public static int getRandInt (int min , int max ) {
139- return (int ) (Math .random () * max + min );
140- }
141165}
0 commit comments