Repository navigation
Expand file tree
/
Copy pathSinPlot.java
More file actions
executable file
·224 lines (191 loc) · 6.81 KB
/
Copy pathSinPlot.java
File metadata and controls
executable file
·224 lines (191 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
File: SinPlot.java
Description: This program plots the sinusouidal function f(x) = sin(x) + 2.0 * cos(x)
Author: Brian Frank
*/
import javax.swing.*;
import java.awt.*;
public class SinPlot extends JFrame
{
private static double[]x;
private static double[]y;
private static final int NUM_SAMPLE_POINT = 100;
static double interval = (2 * Math.PI) / NUM_SAMPLE_POINT;
public SinPlot () //default constructor
{
setTitle ( "SinPlot" );
getContentPane().add ( new DrawingPanel() );
}
public static void getFunctionValue() //creates an array of x and y coordinates for the given sinusoidal function
{
double xValue = -Math.PI;
x = new double[NUM_SAMPLE_POINT];
y = new double[NUM_SAMPLE_POINT];
for (int i=0; i < NUM_SAMPLE_POINT; i++)
{
x[i] = xValue;
xValue += interval;
y[i] = Math.sin(xValue) + 2.0 * Math.cos(xValue);
//y[i] = xValue*xValue*xValue;
//System.out.println(x[i]+ " " + y[i]);
}
}
public static void main ( String[] args ) // main method
{
Plot myPlot = new Plot ("SinPlot");
myPlot.setSize ( 500, 500 );
myPlot.setVisible ( true );
getFunctionValue();
myPlot.drawGraph(x,y,"f(x) = sin(x) + 2.0 * cos(x)", "x", "y", "random"); //define the graph function and color here
}
}
class Plot extends JFrame
{
DrawingPanel panel = new DrawingPanel();
public Plot()
{
getContentPane().add(panel);
}
public Plot (String FrameTitle)
{
setTitle(FrameTitle);
getContentPane().add(panel);
}
public void drawGraph ( double[] x, double[] y, String graphTitle, String xLabel, String yLabel, String color )
{
panel.plotGraph( x, y, graphTitle, xLabel, yLabel, color );
}
}
class DrawingPanel extends JPanel
{
private double[]x,y;
private String graphTitle;
private String xLabel;
private String yLabel;
private Color color;
private Color randcolor;
public void plotGraph( double[] xValue, double[] yValue, String title, String xlbl, String ylbl, String col )
{
x = xValue;
y = yValue;
graphTitle = title;
xLabel = xlbl;
yLabel = ylbl;
color = myGetColor(col);
}
public Color myGetColor( String col )
{
Color color = Color.black;
if( col.equals("blue"))
color = Color.blue;
else if(col.equals("cyan"))
color = Color.cyan;
else if(col.equals("darkGray"))
color = Color.darkGray;
else if(col.equals("gray"))
color = Color.gray;
else if(col.equals("green"))
color = Color.green;
else if(col.equals("lightGray"))
color = Color.lightGray;
else if(col.equals("magenta"))
color = Color.magenta;
else if(col.equals("orange"))
color = Color.orange;
else if(col.equals("pink"))
color = Color.pink;
else if(col.equals("red"))
color = Color.red;
else if(col.equals("white"))
color = Color.white;
else if(col.equals("yellow"))
color = Color.yellow;
else if(col.equals("random")) // grabs random color from colorCycle
{
colorCycle();
color = randcolor;
}
return color;
}
public void paintComponent ( Graphics g )
{
super.paintComponent ( g );
// Set up a Cartesian coordinate system
// get the size of the drawing area
Dimension size = this.getSize();
// place the origin at the middle
g.translate (size.width / 2, size.height / 2);
// draw the x and y axes
drawXYAxes (g);
// draw the graph
myPlotGraph (g);
}
private void drawXYAxes (Graphics g)
{
Dimension size = this.getSize();
int hBound = size.width / 2;
int vBound = size.height / 2;
int tic = size.width / 100;
// draw the x-axis
g.drawLine (-hBound, 0, hBound, 0);
// draw the tic marks along the x axis
for (int k = -hBound; k <= hBound; k += 10)
g.drawLine (k, tic, k, -tic);
// draw the y-axis
g.drawLine (0, vBound, 0, -vBound);
// draw the tic marks along the y axis
for (int k = -vBound; k <= vBound; k += 10)
g.drawLine (-tic , k, +tic, k);
}
public Color colorCycle() //under development
{
double randR = Math.random();
double randG = Math.random();
double randB = Math.random();
//double randA = Math.random(); //Alpha channel
//randcolor = new Color( (float)randR, (float)randG, (float)randB, (float)randA ); //RGB model w/ Alpha
randcolor = new Color( (float)randR, (float)randG, (float)randB ); //RGB model
return randcolor;
}
public void myPlotGraph( Graphics g )
{
Dimension size = this.getSize();
int hBound = size.width/2;
int vBound = size.height/2;
int xTitlePosition = -230;
int yTitlePosition = -220;
double maxY = Math.abs(x[0]);
double relativeX = size.width/(x.length * Math.abs(x[1] - x[0]));
double relativeY = size.height/(maxY * 2);
g.setColor(color);
g.drawString(graphTitle, xTitlePosition, yTitlePosition);
g.drawString(xLabel, 220, 20);
g.drawString(yLabel, 5, 220);
/* for( int i=0; i < y.length; i++)
if(Math.abs(x[i]) > maxY)
maxY = Math.abs(x[i]);
*/
if(color == randcolor) //if-else *experimental*
{
for(int i=0; i < (x.length-1); i++)
{
colorCycle();
g.setColor(randcolor);
g.drawLine((int)(x[i] * relativeX), (int)-(y[i] * relativeY), (int)(x[i+1]), (int)-(y[i+1])); //uncomment this line for a 3D effect!
g.drawLine((int)(x[i] * relativeX), (int)-(y[i] * relativeY), (int)(x[i+1] * relativeX), (int)-(y[i+1] * relativeY));
}
}
else
for(int i=0; i < (x.length-1); i++)
{
/*if(color == randcolor)
{
colorCycle();
g.setColor(randcolor);
}
*/
g.drawLine((int)(x[i] * relativeX), (int)-(y[i] * relativeY), (int)(x[i+1]), (int)-(y[i+1])); //uncomment this line for a 3D effect!
g.drawLine((int)(x[i] * relativeX), (int)-(y[i] * relativeY), (int)(x[i+1] * relativeX), (int)-(y[i+1] * relativeY));
}
}
}