Create a shape from lines and curves

With this tutorial we shall show you how to swap RGB values in an Image. This is very useful when you are trying to create some effects to your Application.

Basically all you have to do in order to swapp RGD values of an Image is:

  • Create a GeneralPath class instance
  • Use moveTo, lineTo, quadTo, curveTo and closePath to draw the basic lines and curves (their names are pretty representative of the functions they perform)
  • And simply paint the shape in a new Frame

Let’s see how the code looks like:

packace com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;

public class CreateShapes {

    public static void main(String[] args) {

  // Create a frame

  Frame frame = new Frame();

  // Add a component with a custom paint method

  frame.add(new CustomPaintComponent());

  // Display the frame

  int frameWidth = 300;

  int frameHeight = 300;

  frame.setSize(frameWidth, frameHeight);



     * To draw on the screen, it is first necessary to subclass a Component 
     * and override its paint() method. The paint() method is automatically called 
     * by the windowing system whenever component's area needs to be repainted.
    static class CustomPaintComponent extends Component {

  public void paint(Graphics g) {

    // Retrieve the graphics context; this object is used to paint shapes

    Graphics2D g2d = (Graphics2D)g;


     * The coordinate system of a graphics context is such that the origin is at the 

     * northwest corner and x-axis increases toward the right while the y-axis increases 

     * toward the bottom.


    int x = 0;

    int y = 0;

    int w = getSize().width-1;

    int h = getSize().height-1;

    GeneralPath shape = new GeneralPath();

    shape.moveTo(x, y);

    shape.lineTo(w/4, h/2);

    shape.quadTo(3*w/2, 4*h/3, x/2, y/3);

    shape.curveTo(w, h, w/2, h/4, x, y);






This was an example on how to create a Shape from Lines and Curves

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button