Java Reference
In-Depth Information
}
/** Called when the window needs painting.
* Computes X and Y range, scales.
*/
@Override
public
public
void
void
paintComponent
(
Graphics g
) {
super
super
.
paintComponent
(
g
);
Dimension s
=
getSize
();
iif
(
data
.
size
() <
2
) {
g
.
drawString
(
"Insufficient data: "
+
data
.
size
(),
10
,
40
);
return
return
;
}
// Compute scale factors
double
double
xfact
=
s
.
width
/
xrange
;
double
double
yfact
=
s
.
height
/
yrange
;
// Scale and plot the data
for
for
(
int
int
i
=
0
;
i
<
data
.
size
();
i
++) {
Point2D d
= (
Point2D
)
data
.
get
(
i
);
double
double
x
= (
d
.
getX
() -
minx
) *
xfact
;
double
double
y
= (
d
.
getY
() -
miny
) *
yfact
;
Debug
.
println
(
"point"
,
"AT "
+
i
+
" "
+
d
+
"; "
+
"x = "
+
x
+
"; y = "
+
y
);
// Draw a 5-pixel rectangle centered, so -2 both x and y.
// AWT numbers Y from 0 down, so invert:
g
.
drawRect
(((
int
int
)
x
)-
2
,
s
.
height
-
2
-(
int
int
)
y
,
5
,
5
);
}
}
@Override
public
public
Dimension
getPreferredSize
() {
return
return new
new
Dimension
(
150
,
150
);
}
public
public static
static
void
void
main
(
String
[]
args
)
throws
throws
IOException
{
final
final
JFrame f
=
new
new
JFrame
(
"Grapher"
);
f
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
Grapher grapher
=
new
new
Grapher
();
f
.
setContentPane
(
grapher
);
f
.
setLocation
(
100
,
100
);
f
.
pack
();
List
<
Point2D
>
data
=
null
null
;
iif
(
args
.
length
==
0
)
data
=
GraphReader
.
read
(
"Grapher.txt"
);