Home » Desktop Java » awt » geom » Create a shape from lines and curves

About Byron Kiourtzoglou

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.

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);

  frame.setVisible(true);

    }

    /**
     * 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);

    shape.closePath();

    g2d.draw(shape);

  }

    }

}

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

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of