Examples » History » Version 1
Federico Vera, 2018-07-08 04:36
| 1 | 1 | Federico Vera | Here are some coding examples of `jdrawinglib`. |
|---|---|---|---|
| 2 | Basically we have a [`Canvas`], and a lot of geometric elements. The canvas itself |
||
| 3 | is a `JPanel`, so you can use it however you see fit, but for beginners sake we |
||
| 4 | also have a [`CanvasFrame`] which is the one that we'll use in the examples. |
||
| 5 | |||
| 6 | ## Usage Template |
||
| 7 | The basic template to create a `CanvasFrame` and setting everything up is as |
||
| 8 | follows: |
||
| 9 | |||
| 10 | ```java |
||
| 11 | import com.dkt.graphics.canvas.Canvas; |
||
| 12 | import com.dkt.graphics.canvas.CanvasFrame; |
||
| 13 | |||
| 14 | public class Template { |
||
| 15 | public static void main(String[] args) { |
||
| 16 | ////////////////////////////////////////////////////////// |
||
| 17 | //The name that appears in the window |
||
| 18 | String title = "Template"; |
||
| 19 | //We need to create a new frame |
||
| 20 | CanvasFrame frame = new CanvasFrame(title); |
||
| 21 | //And make it visible |
||
| 22 | frame.setVisible(true); |
||
| 23 | //Set a size in pixels (whichever you want) |
||
| 24 | frame.setSize(550/*width*/, 550/*height*/); |
||
| 25 | //Extract a reference to the canvas from the frame |
||
| 26 | Canvas canvas = frame.getCanvas(); |
||
| 27 | //Tell the canvas to center the origin of coordinates, |
||
| 28 | //which by default is located in the upper left corner |
||
| 29 | canvas.setCenterOrigin(true); |
||
| 30 | //Tell the canvas to invert the Y axis, this way we will |
||
| 31 | //consider all positive Y increments from the origin of |
||
| 32 | //coordinates to the title bar |
||
| 33 | canvas.setInvertYAxis(true); |
||
| 34 | ////////////////////////////////////////////////////////// |
||
| 35 | |||
| 36 | // --> Application logic goes HERE <-- |
||
| 37 | |||
| 38 | //In order for the elements to be drawn in the canvas, is |
||
| 39 | //necessary to call 'canvas.repaint()' after we add the |
||
| 40 | //elements to it. |
||
| 41 | //PS: There's an autorepaint feature (disabled by default). |
||
| 42 | canvas.repaint(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | ``` |
||
| 46 | |||
| 47 | The basic application logic is pretty simple, you create a new graphical |
||
| 48 | element, modify its attributes and add it to the canvas. |
||
| 49 | |||
| 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 | ### Basic element list |
||
| 66 | * `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 | There are some extra classes in this library, some of them are pretty handy, and |
||
| 86 | the others... not so much. The idea behind this classes was to show what can be |
||
| 87 | done with a few graphical objects, but time passed, and some of them are pretty |
||
| 88 | neat. |
||
| 89 | All of this elements are contained in the `com.dkt.graphics.extras` package. |
||
| 90 | |||
| 91 | Three basic categories can be found in the `extras` package: |
||
| 92 | * Extensions of the `Graphic` class: |
||
| 93 | * `GAxis`: Draws a set of Cartesian axes, with the possibility of having a |
||
| 94 | grid. |
||
| 95 | * `GGrid`: Draws a grid on the canvas. |
||
| 96 | * `GPVector`: Represents a vector and it's projections. |
||
| 97 | * `GVectorPolygon`: Represents a vectorial polygon. |
||
| 98 | * `GPixMap`: Represents a pixmap (an image expressed pixel by pixel). |
||
| 99 | * `GSprite`: An array of `GPixMap` used to create animations. |
||
| 100 | * Additional components for the `Graphic` class: |
||
| 101 | * `GClip`: Wraps a clipping zone. |
||
| 102 | * `GTransform`: Wraps an affine transform. |
||
| 103 | * `Formula` related classes: |
||
| 104 | * `GFormula`: Solves an equation (`GCalculable`) for a given interval. |
||
| 105 | * `GCalculable`: Wraps a formula in code. |
||
| 106 | * `GCalculableTimer`: Solves an equation using multiple threads, this may sound silly, but check out [`ParametricFun`]. |
||
| 107 | |||
| 108 | |||
| 109 | ``` |
||
| 110 | //@TODO add code examples |
||
| 111 | ``` |
||
| 112 | |||
| 113 | [`Canvas`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/canvas/Canvas.java |
||
| 114 | [`CanvasFrame`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/canvas/CanvasFrame.java |
||
| 115 | [`GraphicE`]: https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/entry/src/main/java/com/dkt/graphics/elements/GraphicE.java |
||
| 116 | [`ParametricFun`]: https://redmine.riddler.com.ar/attachments/193/ParametricFun.jar |