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