### Basic CodeDraw Program Setup
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
This is a foundational example demonstrating how to set up a CodeDraw window, set colors, draw basic shapes like rectangles and circles, and display the output. Ensure CodeDraw is imported and the `show()` method is called at the end.
```java
import codedraw.*;
// Imports CodeDraw and all its classes.
// Without this, CodeDraw cannot be used in your program.
public class MyProgram {
public static void main(String[] args) {
// Instantiates a new CodeDraw window with the size of 600x600 pixel
CodeDraw cd = new CodeDraw();
// The created window can now be accessed through the cd variable.
// By calling the method *setColor* the rectangle
// and the square will be drawn in the color red.
cd.setColor(Palette.RED);
// If setColor is called, all shapes that are drawn after
// will have the given color until *setColor* is called again
// with a different color.
cd.drawRectangle(100, 100, 200, 100);
// drawRectangle draws the outline of a rectangle,
// offset by 100 pixel from the top left corner.
// The Rectangle will have a width of 200 pixel
// and a height of 100 pixel.
cd.fillSquare(180, 150, 80);
// The filled square will be offset from the left by 180 pixel
// and 150 pixel from the top. Its size will be 80x80 pixel.
// The next line changes the color to light blue.
cd.setColor(Palette.LIGHT_BLUE);
// fillCircle draws a filled circle with its center at
// the (300, 200) coordinate, around which the circle
// will be drawn with a radius of 50 pixel.
cd.fillCircle(300, 200, 50);
// Shapes that are drawn later will be drawn over
// the shapes that are drawn earlier.
cd.show();
// Finally, the method show() must be called
// to display the drawn shapes in the CodeDraw window.
}
}
```
--------------------------------
### Complete Maven pom.xml Example
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
An example of a complete pom.xml file with CodeDraw as a dependency.
```xml
4.0.0
com.mycompany.app
my-app
1
jitpack.io
https://jitpack.io
com.github.Krassnig
CodeDraw
RELEASE
```
--------------------------------
### Complete Gradle Kotlin build.gradle.kts Example
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
An example of a complete build.gradle.kts file with CodeDraw as a dependency for Kotlin projects.
```kotlin
plugins {
id 'application'
}
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.Krassnig:CodeDraw:latest.release'
}
application {
mainClass = 'demo.App' //Enter path to your Main class
}
```
--------------------------------
### Draw Curve, Arc, and Set Line Style in CodeDraw
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Demonstrates drawing a curve with default settings, then a second curve with modified line width and corner style. Also shows how to draw an arc with specified start and sweep radians. Ensure CodeDraw and Corner are imported.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
cd.drawCurve(100, 100, 250, 50, 200, 200);
cd.setLineWidth(10);
cd.setCorner(Corner.ROUND);
// (100, 300) = start point
// (250, 250) = control point
// (200, 400) = end point
cd.drawCurve(100, 300, 250, 250, 200, 400);
// -Math.PI / 2 = going back a quarter of a circle (default starts is at 3 o'clock)
// Math.PI going forward half a circle
cd.drawArc(300, 300, 100, -Math.PI / 2, Math.PI);
cd.show();
}
}
```
--------------------------------
### Combining Animations for GUI Components
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Shows how to create a simple GUI by combining multiple Animation implementations (MyButton, MyTextBox) into a single animation using Animation.combine. Each component implements the Animation interface independently.
```java
import codedraw.*;
public class MyGUI implements Animation {
public static void main(String[] args) {
MyButton button = new MyButton(100, 100); // implements the Animation interface
MyTextBox textBox = new MyTextBox(100, 200); // implements the Animation interface
CodeDraw.run(Animation.combine(button, textBox));
}
}
```
--------------------------------
### Create a Static Image with CodeDraw
Source: https://github.com/krassnig/codedraw/blob/master/README.md
This snippet demonstrates how to create a static image by drawing basic shapes like rectangles and circles. Ensure to call `.show()` at the end to display the drawing.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
// Creates a new CodeDraw window with a size of 400x400 pixel.
CodeDraw cd = new CodeDraw(400, 400);
// Sets the drawing color to red.
cd.setColor(Palette.RED);
// Draws the outline of a rectangle.
cd.drawRectangle(100, 100, 200, 100);
// Draws a filled square.
cd.fillSquare(180, 150, 80);
// Changes the color to light blue.
cd.setColor(Palette.LIGHT_BLUE);
cd.fillCircle(300, 200, 50);
// Finally, the method "show" must be called
// to display the drawn shapes in the CodeDraw window.
cd.show();
}
}
```
--------------------------------
### Add Gradle Kotlin Repository
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Configure your build.gradle.kts file to include the JitPack repository for Gradle Kotlin projects.
```kotlin
repositories {
maven { url = "https://jitpack.io" }
}
```
--------------------------------
### Initialize CodeDraw Window and Set Title
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Creates a CodeDraw window with a specified canvas size and sets the window title. Use this to set up your initial drawing environment.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(300, 100);
cd.setTitle("Hello World!");
}
}
```
--------------------------------
### Add Gradle Groovy Repository
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Configure your build.gradle file to include the JitPack repository for Gradle Groovy projects.
```groovy
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```
--------------------------------
### Enable InstantDraw and AlwaysOnTop for Debugging
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Configures CodeDraw for debugging by enabling instant drawing and keeping the window on top. This allows interaction with the CodeDraw window while stepping through code.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
cd.setInstantDraw(true);
cd.setAlwaysOnTop(true);
cd.drawCircle(300, 300, 100);
// The circle is displayed without calling
// cd.show();
}
}
```
--------------------------------
### Create an Animation with CodeDraw
Source: https://github.com/krassnig/codedraw/blob/master/README.md
This snippet shows how to create an animation by drawing frames in a loop and using `.show(1000)` to pause for 1 second between frames. The animation continues until the CodeDraw window is closed.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(400, 400);
for (double sec = -Math.PI / 2; !cd.isClosed(); sec += Math.PI / 30) {
// Clears the entire canvas.
cd.clear();
// Draws the second hand of the clock.
cd.drawLine(200, 200, Math.cos(sec) * 100 + 200, Math.sin(sec) * 100 + 200);
// Draws the twelve dots.
for (double j = 0; j < Math.PI * 2; j += Math.PI / 6) {
cd.fillCircle(Math.cos(j) * 100 + 200, Math.sin(j) * 100 + 200, 4);
}
// Displays the drawn objects and waits 1 second.
cd.show(1000);
}
}
}
```
--------------------------------
### Add Maven Repository
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Add the JitPack repository to your pom.xml to use CodeDraw as a Maven dependency.
```xml
jitpack.io
https://jitpack.io
```
--------------------------------
### Load, Draw, and Save Image
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Loads an image from a file, draws it onto a CodeDraw canvas in three different ways (original size, rescaled, rescaled with specific interpolation), and saves the canvas content to a new image file.
```Java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
Image image = Image.fromFile("./path_to/image.png");
cd.drawImage(50, 50, image);
cd.drawImage(200, 50, 200, 200, image);
cd.drawImage(200, 250, 200, 200, image, Interpolation.NEAREST_NEIGHBOR);
cd.show();
Image.save(cd, "./path_to/new_image.png", ImageFormat.PNG);
}
}
```
--------------------------------
### Image Editing Operations
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Demonstrates common image editing operations: cropping, rotating clockwise, and mirroring vertically. These operations are performed sequentially on an image loaded from a file.
```Java
Image myImage = Image.fromFile("plant.png");
Image cropped = Image.crop(myImage, 100, 100, 200, 100);
Image rotated = Image.rotateClockwise(cropped);
Image mirrored = Image.mirrorVertically(rotated);
```
--------------------------------
### Handle Mouse and Click Events
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
This snippet shows how to handle mouse movement and click events to update application state. It tracks mouse position and click count, then displays this information on the canvas. Use this for creating interactive applications that respond to user input.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
int mouseX = 0;
int mouseY = 0;
int clickCount = 0;
while (!cd.isClosed()) {
for (var e : cd.getEventScanner()) {
switch (e) {
case MouseMoveEvent a -> {
mouseX = a.getX();
mouseY = a.getY();
}
case MouseClickEvent a -> clickCount++;
default -> { }
}
}
cd.clear();
cd.drawText(100, 100, "Position: " + mouseX + " " + mouseY + "\nClick: " + clickCount);
cd.show(16);
}
}
}
```
--------------------------------
### Draw on Image and Save
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Loads an image, draws an orange circle onto it using the Image's drawing capabilities, and saves the modified image to a new file.
```Java
import codedraw.*;
public class Main {
public static void main(String[] args) {
Image image = Image.fromFile("./path_to/image.png");
image.setColor(Palette.ORANGE);
image.fillCircle(100, 100, 50);
Image.save(image, "./path_to/edited_image.png", ImageFormat.PNG);
}
}
```
--------------------------------
### Create a Clock Animation
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
This snippet demonstrates how to create an animation of a clock's second hand. It clears the canvas, draws the second hand and hour dots, and displays each frame with a 1-second delay. Use this for creating time-based visual effects.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(400, 400);
for (double sec = -Math.PI / 2; !cd.isClosed(); sec += Math.PI / 30) {
// clears the entire canvas
cd.clear();
// draws the second hand
cd.drawLine(200, 200, Math.cos(sec) * 100 + 200, Math.sin(sec) * 100 + 200);
// draws the twelve dots
for (double j = 0; j < Math.PI * 2; j += Math.PI / 6) {
cd.fillCircle(Math.cos(j) * 100 + 200, Math.sin(j) * 100 + 200, 4);
}
// displays the drawn objects and waits 1 second
cd.show(1000);
}
}
}
```
--------------------------------
### Add Gradle Kotlin Dependency
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Add the CodeDraw dependency to your build.gradle.kts file for Kotlin projects.
```kotlin
dependencies {
implementation("com.github.Krassnig:CodeDraw:latest.release")
}
```
--------------------------------
### Set Font Name with Fallbacks
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Sets a font for text rendering, providing fallback font names if the primary choice is not available on the system.
```Java
String[] installedFonts = TextFormat.getAllAvailableFontNames();
// installedFonts = new String[] { "Verdana", "Arial" };
textFormat.setFontName("JetBrains Mono", "Arial", "Verdana");
// The font Arial is set
```
--------------------------------
### Add Maven Dependency
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Include the CodeDraw Maven dependency in your pom.xml file.
```xml
com.github.Krassnig
CodeDraw
RELEASE
```
--------------------------------
### Draw Text with Chained Formatting
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Draws text using chained method calls for text formatting, providing a concise way to set multiple text properties.
```Java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(400, 400);
cd.getTextFormat()
.setTextOrigin(TextOrigin.TOP_MIDDLE)
.setFontSize(20)
.setItalic(true);
cd.drawText(200, 100, "Hello World!\nMulti lines!");
cd.setColor(Palette.RED);
cd.fillCircle(200, 100, 5);
cd.show();
}
}
```
--------------------------------
### Add Gradle Groovy Dependency
Source: https://github.com/krassnig/codedraw/blob/master/INSTALL.md
Add the CodeDraw dependency to your build.gradle file for Groovy projects.
```groovy
dependencies {
implementation 'com.github.Krassnig:CodeDraw:latest.release'
}
```
--------------------------------
### Draw Rectangle with Styling
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Draws a rectangle with custom color, line width, and rounded corners. Use this to create visually distinct shapes.
```Java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(400, 400);
cd.setColor(Palette.GREEN);
cd.setLineWidth(5);
cd.setCorner(Corner.ROUND);
cd.setCornerRadius(10);
cd.drawRectangle(100, 100, 200, 100);
cd.show();
}
}
```
--------------------------------
### WASD Key Controlled Animation
Source: https://github.com/krassnig/codedraw/blob/master/README.md
Controls a circle's position using WASD keys and redraws it 60 times per second. Implements the Animation interface for event handling and drawing.
```Java
import codedraw.*;
public class MyAnimation implements Animation {
public static void main(String[] args) {
CodeDraw.run(new MyAnimation());
}
private int x = 50;
private int y = 50;
@Override
public void onKeyDown(KeyDownEvent event) {
if (event.getKey() == Key.W) {
y -= 20;
}
else if (event.getKey() == Key.A) {
x -= 20;
}
else if (event.getKey() == Key.S) {
y += 20;
}
else if (event.getKey() == Key.D) {
x += 20;
}
}
@Override
public void draw(Image canvas) {
canvas.clear();
canvas.fillCircle(x, y, 10);
}
}
```
--------------------------------
### Draw Text with Formatting
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Draws text with specified formatting, including alignment, size, and italic style. The origin point is marked with a red dot.
```Java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw(400, 400);
TextFormat format = cd.getTextFormat();
format.setTextOrigin(TextOrigin.TOP_MIDDLE);
format.setFontSize(20);
format.setItalic(true);
cd.drawText(200, 100, "Hello World!\nMulti lines!");
cd.setColor(Palette.RED);
cd.fillCircle(200, 100, 5);
cd.show();
}
}
```
--------------------------------
### Process Events with EventScanner
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Use this snippet to process all available events from the EventScanner in a loop. It checks for mouse move and click events, updating the position and click count accordingly. Ensure to call the corresponding 'next' method for each event type to avoid infinite loops.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
EventScanner es = cd.getEventScanner();
int x = 0;
int y = 0;
int clickCount = 0;
while (!cd.isClosed()) {
while (es.hasEventNow()) {
if (es.hasMouseMoveEvent()) {
MouseMoveEvent a = es.nextMouseMoveEvent();
x = a.getX();
y = a.getY();
}
else if (es.hasMouseClickEvent()) {
es.nextMouseClickEvent();
clickCount++;
}
else {
es.nextEvent();
}
}
cd.clear();
cd.drawText(100, 100, "Position: " + x + " " + y + "\nClick: " + clickCount);
cd.show(16);
}
}
}
```
--------------------------------
### Draw in Fullscreen Mode
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Utilizes CodeDraw's fullscreen mode to display content. This is useful for immersive graphical experiences.
```java
FullScreen fs = new FullScreen();
fs.drawText(100, 100, "Hello World!");
fs.show();
```
--------------------------------
### Interactive Mouse Drawing
Source: https://github.com/krassnig/codedraw/blob/master/README.md
Draws red squares at the mouse cursor's location in response to mouse movement events. Requires the CodeDraw library.
```java
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
cd.drawText(200, 200, "Move your mouse over here.");
cd.show();
cd.setColor(Palette.RED);
// Creates an endless loop (until you close the window).
while (!cd.isClosed()) {
// Creates a loop that consumes all the currently available events.
for (var e : cd.getEventScanner()) {
switch (e) {
// If the event is a mouse move event, a red square will be drawn at its location.
case MouseMoveEvent a ->
cd.fillSquare(a.getX() - 5, a.getY() - 5, 10);
default -> { }
}
}
// Display the red squares that have been drawn up to this point.
cd.show(16);
}
}
}
```
--------------------------------
### Draw in Borderless Window Mode
Source: https://github.com/krassnig/codedraw/blob/master/INTRODUCTION.md
Uses CodeDraw's borderless window mode for a clean, unobtrusive display. This mode is suitable for applications where window decorations are not desired.
```java
BorderlessWindow bw = new BorderlessWindow();
bw.drawText(100, 100, "Hello World!");
bw.show();
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.