Examples¶
Here are some coding examples of jdrawinglib
.
Basically we have a Canvas
, and a lot of geometric elements. The canvas itself is a JPanel
, so you can use it however you see fit, but for beginners sake we also have a CanvasFrame
which is the one that we'll use in the examples.
Usage Template¶
The basic template to create a CanvasFrame
and setting everything up is as follows:
import com.dkt.graphics.canvas.Canvas;
import com.dkt.graphics.canvas.CanvasFrame;
public class Template {
public static void main(String[] args) {
//////////////////////////////////////////////////////////
//The name that appears in the window
String title = "Template";
//We need to create a new frame
CanvasFrame frame = new CanvasFrame(title);
//And make it visible
frame.setVisible(true);
//Set a size in pixels (whichever you want)
frame.setSize(550/*width*/, 550/*height*/);
//Extract a reference to the canvas from the frame
Canvas canvas = frame.getCanvas();
//Tell the canvas to center the origin of coordinates,
//which by default is located in the upper left corner
canvas.setCenterOrigin(true);
//Tell the canvas to invert the Y axis, this way we will
//consider all positive Y increments from the origin of
//coordinates to the title bar
canvas.setInvertYAxis(true);
//////////////////////////////////////////////////////////
// --> Application logic goes HERE <--
//In order for the elements to be drawn in the canvas, is
//necessary to call 'canvas.repaint()' after we add the
//elements to it.
//PS: There's an autorepaint feature (disabled by default).
canvas.repaint();
}
}
The basic application logic is pretty simple, you create a new graphical element, modify its attributes and add it to the canvas.
Basic graphical elements¶
There are three basic graphical elements all of which inherit from GraphicE
:
-
GraphicE
: The most basic elements, which are primarily defined by:
- a color (more precisely a
Paint
)
- a stroke, used to draw the element
- and the possibility to be moved on the
(x,y)
plane
-
GFillableE
: This extend a bit GraphicE
by adding:
- a
fill paint
which will be the one used to fill the element
- and a
fill
flag which if false
(default) will make GFillableE
act as
GraphicE
regardless of the fill paint.
-
Graphic
: This are containers for GraphicE
objects, and allow the creation
of complex graphics using affine transforms, clipping areas, etc.
All of this elements are contained in the com.dkt.graphics.elements
package
Basic element list¶
-
GArc
: An arc of an oval
-
GCircle
: A circle (an oval contained on a square box)
-
GImage
: An image, this object can also be used as Paint
-
GLine
: Represents a line segment
-
GOval
: An oval contained in a box
-
GPath
: Represents an ordered array of pixels printed joining the points
in order
-
GPoint
: Represents a pixel in the screen, although it can be drawn as a
cross.
-
GPointArray
: Represents an unordered array of pixels.
-
GPoly
: A closed GPath
-
GRegPoly
: A regular polygon with a given number of sides inscribed in a
circle
-
GRectangle
: A rectangle,
-
GString
: Represents a String on the canvas
-
GVector
: A line segment with a small arrow on one end
-
Graphic
: GraphicE
container
Graphical elements extras¶
There are some extra classes in this library, some of them are pretty handy, and the others... not so much. The idea behind this classes was to show what can be done with a few graphical objects, but time passed, and some of them are pretty neat.
All of this elements are contained in the com.dkt.graphics.extras
package.
Three basic categories can be found in the extras
package:
- Extensions of the
Graphic
class:
-
GAxis
: Draws a set of Cartesian axes, with the possibility of having a
grid.
-
GGrid
: Draws a grid on the canvas.
-
GPVector
: Represents a vector and it's projections.
-
GVectorPolygon
: Represents a vectorial polygon.
-
GPixMap
: Represents a pixmap (an image expressed pixel by pixel).
-
GSprite
: An array of GPixMap
used to create animations.
- Additional components for the
Graphic
class:
-
GClip
: Wraps a clipping zone.
-
GTransform
: Wraps an affine transform.
-
Formula
related classes:
-
GFormula
: Solves an equation (GCalculable
) for a given interval.
-
GCalculable
: Wraps a formula in code.
-
GCalculableTimer
: Solves an equation using multiple threads, this may sound silly, but check out ParametricFun
.
Other examples¶