Skip to content

GC Examples

A GC widget component.

Basic Snippet

GC  Basic Snippet

Demonstrates basic drawing operations using GC (Graphics Context).

This example shows drawing lines, ovals, rectangles, and copying canvas areas

using a paint listener on a canvas widget.

GCBasicSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates basic drawing operations using GC (Graphics Context).
 * This example shows drawing lines, ovals, rectangles, and copying canvas areas
 * using a paint listener on a canvas widget.
 */

public class GCBasicSnippet {
    public static void main (String [] args) {
        Config.forceEquo();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("GCBasicSnippet");
        shell.setSize(650, 650);

        Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.setBounds(50, 50, 600, 600);
        canvas.addPaintListener(e -> {
            GC gc = e.gc;
            gc.drawLine(10, 10, 110, 90);
            gc.drawOval(20, 20, 80, 60);
            gc.drawRectangle(30, 30, 60, 40);
            gc.copyArea(10, 10, 100, 80, 200, 10);

            gc.copyArea(10, 10, 100, 80, 50, 150);

        });

        shell.open();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
}

View on GitHub

Fill Oval Snippet

GC  Fill Oval Snippet

Demonstrates fillOval operations using GC (Graphics Context).

This example shows filled ovals of different sizes, colors, aspect ratios,

overlapping shapes, and handling of negative dimensions.

GCFillOvalSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates fillOval operations using GC (Graphics Context).
 * This example shows filled ovals of different sizes, colors, aspect ratios,
 * overlapping shapes, and handling of negative dimensions.
 */

public class GCFillOvalSnippet {

    public static void main (String [] args) {
        Config.forceEquo();
        final Display display = new Display ();
        final Shell shell = new Shell (display, SWT.SHELL_TRIM);
        shell.setText("GCFillOvalSnippet");
        shell.setSize (650, 650);

        Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.setBounds(50, 50, 600, 600);
        canvas.addPaintListener(e -> {
            GC gc = e.gc;
            // Demo 1: Basic filled ovals with different colors
            gc.setBackground(new Color(255, 0, 0)); // Red
            gc.fillOval(50, 50, 100, 80);

            gc.setBackground(new Color(0, 255, 0)); // Green
            gc.fillOval(200, 50, 120, 120); // Circle

            gc.setBackground(new Color(0, 0, 255)); // Blue
            gc.fillOval(350, 50, 80, 120); // Tall oval

            // Demo 2: Filled oval with outline
            gc.setBackground(new Color(255, 255, 0)); // Yellow
            gc.setForeground(new Color(0, 0, 0)); // Black
            gc.fillOval(50, 200, 150, 100);
            gc.drawOval(50, 200, 150, 100); // Add black outline

            // Demo 3: Overlapping filled ovals
            gc.setBackground(new Color(0, 255, 255)); // Cyan
            gc.fillOval(250, 200, 100, 100);

            gc.setBackground(new Color(255, 0, 255)); // Magenta
            gc.fillOval(280, 230, 100, 100);

            // Demo 4: Negative width/height handling
            gc.setBackground(new Color(0, 128, 0)); // Dark Green
            gc.fillOval(450, 250, -80, -60); // Should draw at (370, 190)

            // Add labels
            gc.setForeground(new Color(0, 0, 0)); // Black
            gc.drawText("Red Oval", 50, 140, true);
            gc.drawText("Green Circle", 200, 180, true);
            gc.drawText("Blue Tall", 350, 180, true);
            gc.drawText("Yellow + Outline", 50, 310, true);
            gc.drawText("Overlapping", 250, 310, true);
            gc.drawText("Negative coords", 370, 260, true);
        });

        shell.setSize (650, 650);
        shell.open ();

        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ())
                display.sleep ();
        }
        display.dispose ();
    }
}

View on GitHub

Shapes Gradient Snippet

GC  Shapes Gradient Snippet

Demonstrates filled shapes and gradients using GC (Graphics Context).

This example renders rectangles, ovals, polygons, and gradient effects,

including a composite landscape scene combining multiple drawing operations.

GCShapesGradientSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates filled shapes and gradients using GC (Graphics Context).
 * This example renders rectangles, ovals, polygons, and gradient effects,
 * including a composite landscape scene combining multiple drawing operations.
 */

public class GCShapesGradientSnippet {
    public static void main(String[] args) {
        Config.forceEquo();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("GCShapesGradientSnippet");
        shell.setSize(850, 850);

        Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.setBounds(50, 50, 800, 800);
        canvas.addPaintListener(e -> {
            GC gc = e.gc;
            gc.setForeground(new Color(0, 0, 0));

            // Demo 1: Basic filled shapes
            gc.drawText("Basic Filled Shapes:", 50, 30, true);

            // Filled rectangle
            gc.setBackground(new Color(255, 100, 100));
            gc.fillRectangle(50, 60, 80, 60);
            gc.drawText("Rectangle", 50, 130, true);

            // Filled oval
            gc.setBackground(new Color(100, 255, 100));
            gc.fillOval(150, 60, 80, 60);
            gc.drawText("Oval", 150, 130, true);

            // Filled round rectangle
            gc.setBackground(new Color(100, 100, 255));
            gc.fillRoundRectangle(250, 60, 80, 60, 20, 20);
            gc.drawText("Round Rect", 250, 130, true);

            // Filled arc
            gc.setBackground(new Color(255, 255, 100));
            gc.fillArc(350, 60, 80, 60, 45, 180);
            gc.drawText("Arc (45°-225°)", 350, 130, true);

            // Demo 2: Filled polygons
            gc.drawText("Filled Polygons:", 50, 170, true);

            // Triangle
            gc.setBackground(new Color(255, 0, 255));
            int[] triangle = {50, 250, 100, 200, 150, 250};
            gc.fillPolygon(triangle);
            gc.drawText("Triangle", 75, 260, true);

            // Diamond
            gc.setBackground(new Color(0, 255, 255));
            int[] diamond = {225, 200, 250, 225, 225, 250, 200, 225};
            gc.fillPolygon(diamond);
            gc.drawText("Diamond", 200, 260, true);

            // Star (approximation)
            gc.setBackground(new Color(255, 165, 0));
            int[] star = {
                300, 200,   // top
                310, 220,   // right inner
                330, 220,   // right outer
                315, 235,   // right bottom inner
                325, 255,   // bottom right
                300, 245,   // bottom inner
                275, 255,   // bottom left
                285, 235,   // left bottom inner
                270, 220,   // left outer
                290, 220    // left inner
            };
            gc.fillPolygon(star);
            gc.drawText("Star", 290, 260, true);

            // Demo 3: Horizontal gradients
            gc.drawText("Horizontal Gradients:", 50, 300, true);

            gc.setForeground(new Color(255, 0, 0));  // Start color (red)
            gc.setBackground(new Color(255, 255, 0)); // End color (yellow)
            gc.fillGradientRectangle(50, 330, 150, 40, false); // Horizontal
            gc.setForeground(new Color(0, 0, 0));
            gc.drawText("Red to Yellow", 50, 380, true);

            gc.setForeground(new Color(0, 0, 255));    // Start color (blue)
            gc.setBackground(new Color(255, 255, 255)); // End color (white)
            gc.fillGradientRectangle(220, 330, 150, 40, false); // Horizontal
            gc.drawText("Blue to White", 220, 380, true);

            gc.setForeground(new Color(0, 255, 0));   // Start color (green)
            gc.setBackground(new Color(0, 0, 0));     // End color (black)
            gc.fillGradientRectangle(390, 330, 150, 40, false); // Horizontal
            gc.drawText("Green to Black", 390, 380, true);

            // Demo 4: Vertical gradients
            gc.drawText("Vertical Gradients:", 50, 410, true);

            gc.setForeground(new Color(255, 0, 255));  // Magenta
            gc.setBackground(new Color(0, 255, 255));  // Cyan
            gc.fillGradientRectangle(50, 440, 60, 80, true); // Vertical
            gc.drawText("Magenta", 50, 525, true);
            gc.drawText("to Cyan", 50, 540, true);

            gc.setForeground(new Color(255, 165, 0));  // Orange
            gc.setBackground(new Color(128, 0, 128));  // Purple
            gc.fillGradientRectangle(130, 440, 60, 80, true); // Vertical
            gc.drawText("Orange", 130, 525, true);
            gc.drawText("to Purple", 130, 540, true);

            gc.setForeground(new Color(255, 255, 255)); // White
            gc.setBackground(new Color(100, 100, 100)); // Gray
            gc.fillGradientRectangle(210, 440, 60, 80, true); // Vertical
            gc.drawText("White", 210, 525, true);
            gc.drawText("to Gray", 210, 540, true);

            // Demo 5: Mixed shapes with gradients and fills
            gc.drawText("Complex Scene:", 350, 410, true);

            // Gradient background
            gc.setForeground(new Color(135, 206, 235)); // Sky blue
            gc.setBackground(new Color(255, 255, 255)); // White
            gc.fillGradientRectangle(350, 440, 200, 100, true);

            // Sun (filled circle)
            gc.setBackground(new Color(255, 255, 0));
            gc.fillOval(450, 450, 30, 30);

            // Mountains (filled triangles)
            gc.setBackground(new Color(139, 69, 19)); // Brown
            int[] mountain1 = {350, 540, 380, 480, 410, 540};
            gc.fillPolygon(mountain1);

            int[] mountain2 = {390, 540, 430, 470, 470, 540};
            gc.fillPolygon(mountain2);

            // Trees (filled shapes)
            gc.setBackground(new Color(34, 139, 34)); // Forest green
            gc.fillOval(500, 510, 20, 20); // Tree crown
            gc.setBackground(new Color(139, 69, 19)); // Brown
            gc.fillRectangle(507, 525, 6, 15); // Tree trunk

            gc.setBackground(new Color(34, 139, 34));
            gc.fillOval(525, 505, 25, 25); // Larger tree crown
            gc.setBackground(new Color(139, 69, 19));
            gc.fillRectangle(535, 525, 6, 15); // Tree trunk

            gc.drawText("Landscape scene", 350, 550, true);
            gc.drawText("with gradients", 350, 565, true);
        });

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

View on GitHub

Text Flags Snippet

GC  Text Flags Snippet

Demonstrates text drawing flags in GC (Graphics Context).

This example shows the effect of DRAW_TRANSPARENT, DRAW_DELIMITER, and DRAW_TAB flags

when rendering text, with side-by-side comparisons to illustrate the differences.

GCTextFlagsSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates text drawing flags in GC (Graphics Context).
 * This example shows the effect of DRAW_TRANSPARENT, DRAW_DELIMITER, and DRAW_TAB flags
 * when rendering text, with side-by-side comparisons to illustrate the differences.
 */

public class GCTextFlagsSnippet {
    public static void main(String[] args) {
        Config.forceEquo();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("GCTextFlagsSnippet");
        shell.setSize(800, 800);

        Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.setBounds(50, 50, 750, 750);
        canvas.addPaintListener(e -> {
            GC gc = e.gc;
            gc.setForeground(new Color(0, 0, 0));
            gc.setBackground(new Color(255, 255, 255));

            // Demo 1: DRAW_TRANSPARENT flag
            gc.drawText("1. SWT.DRAW_TRANSPARENT Flag Test:", 20, 50, SWT.DRAW_TRANSPARENT);
            gc.setBackground(new Color(255, 200, 200));
            gc.fillRectangle(20, 80, 400, 80);

            // Transparent text (default behavior)
            gc.setForeground(new Color(0, 0, 0));
            gc.drawText("This text is TRANSPARENT (background shows through)", 30, 90, SWT.DRAW_TRANSPARENT);

            // Non-transparent text 
            gc.setBackground(new Color(255, 255, 255)); // White background
            gc.drawText("This text is NON-TRANSPARENT (white background)", 30, 110, 0); // No flags = opaque

            // Compare side by side
            gc.setBackground(new Color(200, 255, 200)); // Light green
            gc.fillRectangle(30, 130, 200, 25);
            gc.drawText("Transparent", 35, 135, SWT.DRAW_TRANSPARENT);

            gc.setBackground(new Color(255, 255, 255));
            gc.drawText("vs Non-transparent", 150, 135, 0);

            // Demo 2: DRAW_DELIMITER flag
            gc.setForeground(new Color(0, 0, 0));
            gc.drawText("2. SWT.DRAW_DELIMITER Flag Test:", 20, 180, SWT.DRAW_TRANSPARENT);

            // Text with various newline characters - WITH DRAW_DELIMITER
            String textWithNewlines = "Line 1\nLine 2\r\nLine 3\rLine 4";
            gc.setForeground(new Color(0, 100, 0));
            gc.drawText("WITH DRAW_DELIMITER (preserves newlines):", 30, 210, SWT.DRAW_TRANSPARENT);
            gc.drawText(textWithNewlines, 30, 230, SWT.DRAW_DELIMITER | SWT.DRAW_TRANSPARENT);

            // Text with various newline characters - WITHOUT DRAW_DELIMITER  
            gc.setForeground(new Color(100, 0, 0));
            gc.drawText("WITHOUT DRAW_DELIMITER (converts to spaces):", 30, 310, SWT.DRAW_TRANSPARENT);
            gc.drawText(textWithNewlines, 30, 330, SWT.DRAW_TRANSPARENT);

            // Demo 3: DRAW_TAB flag
            gc.setForeground(new Color(0, 0, 0)); 
            gc.drawText("3. SWT.DRAW_TAB Flag Test:", 20, 370, SWT.DRAW_TRANSPARENT);
            String textWithTabs = "Column1\tColumn2\tColumn3\tColumn4";

            // WITH DRAW_TAB flag
            gc.setForeground(new Color(0, 0, 100));
            gc.drawText("WITH DRAW_TAB (preserves tab spacing):", 30, 400, SWT.DRAW_TRANSPARENT);
            gc.drawText(textWithTabs, 30, 420, SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT);

            // WITHOUT DRAW_TAB flag
            gc.setForeground(new Color(100, 0, 0));
            gc.drawText("WITHOUT DRAW_TAB (converts to spaces):", 30, 450, SWT.DRAW_TRANSPARENT);
            gc.drawText(textWithTabs, 30, 470, SWT.DRAW_TRANSPARENT);

        });

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

View on GitHub