Java Reference
In-Depth Information
QuadCurve2D.Double quadCurve; // Quadratic curve
CubicCurve2D.Double cubicCurve; // Cubic curve
CurvePane pane = new CurvePane(); // Pane to contain curves
}
You will need an HTML file to run the applet. The contents can be something like:
<applet code="CurveApplet.class" width=300 height=300></applet>
This will display the applet in
appletviewer
. If you want to display it in your browser, you need to
convert the HTML using the
HTMLConverter
program. If you don't already have it you can download
it from the
http://java.sun.com
web site.
If you run the applet using
appletviewer
, you will get a
window looking like that here.
How It Works
We need an object of our own class type so that we can implement the
paint()
method for it. We
define the inner class
CurvePane
for this purpose with
JComponent
as the base class so it is a Swing
component. We create an object of this class (which is a member of the
CurveApplet
class) and add it
to the content pane for the applet using its inherited
add()
method. The layout manager for the
content pane is
BorderLayout
, and the default positioning is
BorderLayout.CENTER
so the
CurvePane
object fills the content pane.
The points defining the quadratic and cubic curves are defined as fields in the
CurveApplet
class and
these are referenced in the
paint()
method for the
CurvePane
class to create the objects
representing curves. These points are used in the
CurvePane
class constructor to create the curves. We
draw the curves by calling the
draw()
method for the
Graphics2D
object and passing a reference to a
curve object as the argument.
It's hard to see how the control points affect the shape of the curve, so let's add some code to draw the
control points.
Try It Out - Displaying the Control Points
We will mark the position of each control point by drawing a small circle around it. We can define a
marker using an inner class of
CurveApplet
that we can define as:
// Inner class defining a control point marker
class Marker {