-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraph.html
More file actions
72 lines (59 loc) · 1.96 KB
/
Copy pathgraph.html
File metadata and controls
72 lines (59 loc) · 1.96 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
<html>
<head><title>BeanShell Example - Simple Graph Example</title></head>
<body bgcolor="#ffffff">
<a href="../home.html"><img border=0 src="../images/homebutton.gif"><br>Home</a>
<p>
<h1>Simple Graph Example</h1>
<img src="graph.gif">
<p>
This example shows the use of a scripted object - a "graph" - on which we
can call a method plot() to display points. We use a BshCanvas helper
class for the component so that our drawing will be buffered (won't be erased
when the window is redrawn). It's also possible to create a BshCanvas that
calls back a scripted paint() method in our object to render its appearance.
(another example coming)
<p>
Note in the snapshot above that we are able to refer to variables in the
graph for later use. i.e. we can refere to our graph's canvas, frame, or
graphics objects if we want to modify the graph's appearance, or do
additional drawing. (In the example we set the Frame title).
<p>
For a slightly more interesting version of this example with some
additional features <a href="graph2.bsh">see graph2.bsh</a>
<p>
<p>
<center>
<table width=100% cellpadding=5>
<tr><td bgcolor="#afefac">
<font size=+1><a href="graph.bsh">graph.bsh</a> - A simple graph</font>
</td></tr>
<tr><td bgcolor="#cfefcc">
<pre>
import bsh.util.BshCanvas; // BshCanvas simply buffers graphics
graph( int width, int height ) {
canvas=new BshCanvas();
canvas.setSize( width, height );
frame=frame( canvas );
graphics=canvas.getBufferedGraphics();
// draw axis
graphics.setColor( Color.red );
graphics.drawLine( 0, height/2, width, height/2 );
graphics.drawLine( width/2, 0, width/2, height );
graphics.setColor( Color.black );
plot(int x, int y) {
graphics.fillOval( (x+width/2-1), (y+height/2-1), 3, 3);
canvas.repaint();
}
return this;
}
drawSin( graph ) {
for (int x=-100; x<100; x++ ) {
y=(int)(50*Math.sin( x/10.0 ));
graph.plot( x, y );
}
}
</pre>
</td></tr>
</table>
</center>
<p>