Package diva.canvas.tutorial
Class SimpleTutorial
- java.lang.Object
-
- diva.canvas.tutorial.SimpleTutorial
-
public class SimpleTutorial extends java.lang.Object
This tutorial shows how to construct a JCanvas and place figures on it.
In this example, we create figures with a few different shapes, using the BasicFigure class in diva.canvas.toolbox, which takes an instance of Shape (an interface defined in the Java AWT) and draws it on the screen using given colors and strokes.
Each JCanvas contains by default an instance of GraphicsPane. To get the graphics pane from a JCanvas:
GraphicsPane graphicsPane = (GraphicsPane)canvas.getCanvasPane();
The pane contains several layers, one of which is a foreground FigureLayer upon which figures are drawn and interacted with. To get the figure layer:FigureLayer layer = graphicsPane.getForegroundLayer();
There are three figures on the screen, each created using a different kind of shape. The code to create a rectangle and add it to the figure layer is:Figure rectangle = new BasicRectangle(10,10,100,100,Color.blue); layer.add(rectangle);
The code to create the curved shape is more complex, and uses an instance of java.awt.geom.GeneralPath.GeneralPath path = new GeneralPath(); path.moveTo(120,240); path.lineTo(240,240); path.quadTo(180,120,120,240); path.closePath(); Figure semi = new BasicFigure(path, Color.green); layer.add(semi);
The third figure is much the same, but uses an instance of diva.util.java2d.Polyline2D. Polyline2D is more efficient than GeneralPath, and should be used anytime only straight-line segments are needed.- Version:
- $Id$
- Author:
- John Reekie
-
-
Constructor Summary
Constructors Constructor Description SimpleTutorial()
Create a JCanvas and put it into a window
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createBasicFigure()
Create an odd-shaped figure.void
createBasicRectangle()
Create a rectangle figure.void
createPolyline()
Create a polyline.static void
main(java.lang.String[] argv)
Main function
-
-
-
Method Detail
-
createBasicRectangle
public void createBasicRectangle()
Create a rectangle figure. The rectangle is an instance of the BasicRectangle class. This class, together with a number other useful predefined figure classes, is contained in the package diva.canvas.toolbox.
-
createBasicFigure
public void createBasicFigure()
Create an odd-shaped figure. The rectangle is an instance of the BasicShape class, which draws itself using any instance of the Java2D interface, java.awt.Shape. In this example, we use an instance of GeneralPath.
-
createPolyline
public void createPolyline()
Create a polyline. Again, this uses the BasicFigure class, but this time the shape is an instance of diva.util.Polyline2D.
-
main
public static void main(java.lang.String[] argv)
Main function
-
-