Proyecto

General

Perfil

Examples » Histórico » Versión 5

Federico Vera, 2018-07-09 19:32

1 3 Federico Vera
# Examples
2
3 4 Federico Vera
{{>toc}}
4 3 Federico Vera
5 1 Federico Vera
Here are some coding examples of `jdrawinglib`.
6 2 Federico Vera
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.
7 1 Federico Vera
8
## Usage Template
9 2 Federico Vera
The basic template to create a `CanvasFrame` and setting everything up is as follows:
10 1 Federico Vera
11
```java
12
import com.dkt.graphics.canvas.Canvas;
13
import com.dkt.graphics.canvas.CanvasFrame;
14
15
public class Template {
16
    public static void main(String[] args) {
17
        //////////////////////////////////////////////////////////
18
        //The name that appears in the window
19
        String title = "Template";
20
        //We need to create a new frame
21
        CanvasFrame frame = new CanvasFrame(title);
22
        //And make it visible
23
        frame.setVisible(true);
24
        //Set a size in pixels (whichever you want)
25
        frame.setSize(550/*width*/, 550/*height*/);
26
        //Extract a reference to the canvas from the frame
27
        Canvas canvas = frame.getCanvas();
28
        //Tell the canvas to center the origin of coordinates,  
29
        //which by default is located in the upper left corner
30
        canvas.setCenterOrigin(true);
31
        //Tell the canvas to invert the Y axis, this way we will
32
        //consider all positive Y increments from the origin of
33
        //coordinates to the title bar
34
        canvas.setInvertYAxis(true);
35
        //////////////////////////////////////////////////////////
36
37
        // -->         Application logic goes HERE            <--
38
39
        //In order for the elements to be drawn in the canvas, is
40
        //necessary to call 'canvas.repaint()' after we add the
41
        //elements to it.
42
        //PS: There's an autorepaint feature (disabled by default).
43
        canvas.repaint();
44
    }
45
}
46
```
47
48 2 Federico Vera
The basic application logic is pretty simple, you create a new graphical element, modify its attributes and add it to the canvas.
49 1 Federico Vera
50
## Basic graphical elements
51
There are three basic _graphical elements_ all of which inherit from [`GraphicE`]:
52
* `GraphicE`: The most basic elements, which are primarily defined by:
53
 * a color (more precisely a `Paint`)
54
 * a stroke, used to draw the element
55
 * and the possibility to be moved on the `(x,y)` plane
56
* `GFillableE`: This extend a bit `GraphicE` by adding:
57
 * a `fill paint` which will be the one used to fill the element
58
 * and a `fill` flag which if `false` (_default_) will make `GFillableE` act as
59
  `GraphicE` regardless of the fill paint.
60
* `Graphic`: This are containers for `GraphicE` objects, and allow the creation
61
 of complex graphics using affine transforms, clipping areas, etc.
62
63
All of this elements are contained in the `com.dkt.graphics.elements` package
64
65 5 Federico Vera
## Basic element list
66 1 Federico Vera
* `GArc`: An arc of an oval
67
* `GCircle`: A circle (an oval contained on a square box)
68
* `GImage`: An image, this object can also be used as `Paint`
69
* `GLine`: Represents a line segment
70
* `GOval`: An oval contained in a box
71
* `GPath`: Represents an ordered array of pixels printed joining the points
72
  in order
73
* `GPoint`: Represents a pixel in the screen, although it can be drawn as a
74
   cross.
75
* `GPointArray`: Represents an unordered array of pixels.
76
* `GPoly`: A closed `GPath`
77
* `GRegPoly`: A regular polygon with a given number of sides inscribed in a
78
   circle
79
* `GRectangle`: A rectangle,
80
* `GString`: Represents a String on the canvas
81
* `GVector`: A line segment with a small arrow on one end
82
* `Graphic`: `GraphicE` container
83
84
## Graphical elements extras
85 2 Federico Vera
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.
86 1 Federico Vera
All of this elements are contained in the `com.dkt.graphics.extras` package.
87
88
Three basic categories can be found in the `extras` package:
89
* Extensions of the `Graphic` class:
90
 * `GAxis`: Draws a set of Cartesian axes, with the possibility of having a
91
 grid.
92
 * `GGrid`: Draws a grid on the canvas.
93
 * `GPVector`: Represents a vector and it's projections.
94
 * `GVectorPolygon`: Represents a vectorial polygon.
95
 * `GPixMap`: Represents a pixmap (an image expressed pixel by pixel).
96
 * `GSprite`: An array of `GPixMap` used to create animations.
97
* Additional components for the `Graphic` class:
98
 * `GClip`: Wraps a clipping zone.
99
 * `GTransform`: Wraps an affine transform.
100
* `Formula` related classes:
101
 * `GFormula`: Solves an equation (`GCalculable`) for a given interval.
102
 * `GCalculable`: Wraps a formula in code.
103
 * `GCalculableTimer`: Solves an equation using multiple threads, this may sound silly, but check out [`ParametricFun`].
104 2 Federico Vera
105 3 Federico Vera
## Other examples
106
{{child_pages}}
107 1 Federico Vera
108
[`Canvas`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/canvas/Canvas.java
109
[`CanvasFrame`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/canvas/CanvasFrame.java
110
[`GraphicE`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/elements/GraphicE.java
111
[`ParametricFun`]: https://redmine.riddler.com.ar/attachments/193/ParametricFun.jar
Volver al inicio