|
import com.dkt.graphics.canvas.Canvas;
|
|
import com.dkt.graphics.canvas.CanvasFrame;
|
|
import com.dkt.graphics.elements.GCircle;
|
|
import com.dkt.graphics.utils.Utils;
|
|
import java.awt.Color;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import javax.imageio.ImageIO;
|
|
|
|
public class CircularGradient {
|
|
public static void main(String[] args) {
|
|
//////////////////////////////////////////////////////////
|
|
//The name that appears in the window
|
|
String title = "Circular Gradient";
|
|
//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);
|
|
//////////////////////////////////////////////////////////
|
|
final int RADIUS = 125;
|
|
for (int i = 0; i < RADIUS; i++) {
|
|
Color color = new Color(i, 0, i);
|
|
GCircle circle = new GCircle(0, 0, RADIUS - i);
|
|
circle.setFillPaint(color);
|
|
circle.setPaint(color);
|
|
circle.setFill(true);
|
|
canvas.add(circle);
|
|
}
|
|
|
|
BufferedImage image = Utils.getImage(canvas, false);
|
|
image = Utils.trimImage(image, null);
|
|
try {
|
|
File file = new File("trimmed_gradient.png");
|
|
ImageIO.write(image, "png", file);
|
|
} catch (java.io.IOException ex) {
|
|
System.out.println("Unable to write the file!");
|
|
}
|
|
|
|
//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();
|
|
}
|
|
}
|