Proyecto

General

Perfil

Simple Color Gradient and Save » Histórico » Versión 4

Federico Vera, 2018-07-09 19:30

1 1 Federico Vera
In this example we'll be creating a simple color gradient using a lot of circles (the recommended way is using `java.awt.GradientPaint`), and then save the resulting image as a `png` file.
2
3
All the code examples assume you are using the [[Examples#Usage-Template|usage template]].
4
5 4 Federico Vera
## Import the classes
6 1 Federico Vera
7
```java
8
import com.dkt.graphics.elements.GCircle;
9
import com.dkt.graphics.utils.Utils;
10
import java.awt.Color;
11
```
12
13 4 Federico Vera
## Create the circles
14 1 Federico Vera
The gradient _effect_ will result from incrementing the value of one of the color coordinates, in this example we'll change the red and blue coordinates simultaneously (of course you are encouraged to experiment with the code), and at the same time decrementing the radius of the circle.
15
16
The code for this is quite simple:
17
```java
18
final int RADIUS = 125;
19
for (int i = 0; i < RADIUS; i++) {
20
    Color color = new Color(i, 0, i);
21
    GCircle circle = new GCircle(0, 0, RADIUS - i);
22
    circle.setFillPaint(color);
23
    circle.setPaint(color);
24
    circle.setFill(true);
25
    canvas.add(circle);
26
}
27
```
28
29
And the result should be as follows:
30
31
![Screenshot](https://raw.githubusercontent.com/wiki/dktcoding/jdrawinglib/examples/circle_gradient/trimmed_gradient.png)
32
33 4 Federico Vera
## Save it to a file
34 1 Federico Vera
There are several options to save the contents of a `Canvas` into a file as an image (it can also be saved as text), but the recommended way is with the  help of the `Utils` class included in the `com.dkt.graphics.utils` package.
35
There are two basic options with or without background, the main difference being that the later will ignore the background paint and let it transparent.
36
37
The code is very simple:
38
39
```java
40
try {
41
    // This method requires a canvas, the name of the file, and a
42
    // boolean value which represents whether you want the
43
    // background or not.
44
    Utils.saveScreenshot(canvas, "gradient.png", false);
45
} catch (java.io.IOException ex) {
46
    System.out.println("Unable to write the file!");
47
}
48
```
49
50 4 Federico Vera
## Trim the image (alternative approach)
51 1 Federico Vera
There's an alternative approach for this process which includes trimming the image to an optimum size (that is without all the extra space around the drawing).
52
53
There are three steps to achieve this:
54
55 4 Federico Vera
### Create an image in memory from the canvas
56 1 Federico Vera
```java
57
// import BufferedImage from java.awt.image
58
BufferedImage image = Utils.getImage(canvas, false);
59
```
60 4 Federico Vera
### Trim the image
61 1 Federico Vera
```java
62
// This method requires the color to omit, if the value
63
// is null then it will omit r=0 g=0 b=0 a=0 which is
64
// the background set by Utils.getImage(...)
65
image = Utils.trimImage(image, null);
66
```
67 4 Federico Vera
## Save the image to a file
68 1 Federico Vera
```java
69
// import File from java.io
70
// import ImageIO from javax.imageio
71
try {
72
    File file = new File("trimmed_gradient.png");
73
    ImageIO.write(image, "png", file);
74
} catch (java.io.IOException ex) {
75
    System.out.println("Unable to write the file!");
76
}
77
```
78 4 Federico Vera
## Save the image as text
79
You can checkout [`GraphicCreator`](https://redmine.riddler.com.ar/projects/jdrawinglib/repository/revisions/master/show/src/main/java/com/dkt/graphics/extras/GraphicCreator.java)
80
~~~java
81
for2 18 1 255 125 1 0 "
82
    color  %1$d 0 %1$d;
83
    colorf %1$d 0 %1$d;
84
    circle 0 0 %2$d
85
"
86
~~~
87 1 Federico Vera
88 3 Federico Vera
The final code can be downloaded from here `->` attachment:CircularGradient.java
Volver al inicio