awt

Draw using the stroking pen example

With this tutorial we shall show you how to draw using the stroking pen. This is a very handy feature if you want to create simple graphics, by yourself. You can also create some cool paint applications using that.

In short, to draw using the stroking pen, one should follow these steps:

  • Create a class tha extends Component and override the paint method.
  • Create a new BasicStroke with BasicStroke.CAP_BUTT as an argument.
  • Use BasicStroke.JOIN_MITER, ... ) to customize the stroke.
  • Use Graphics2D.setStroke to set up the stroke.

 
Let’s see the code snippets that follow.

package com.javacodegeeks.snippets.desktop;

import java.awt.BasicStroke;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class StrokingPenDrawing {

  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;

  // A solid stroke

  BasicStroke stroke = new BasicStroke(5.0f);

  g2d.setStroke(stroke);

  // Draw an oval that fills the window

  int x = 0;

  int y = 0;

  int width = getSize().width-1;

  int height = getSize().height-1;

   /**
    * 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.
    */

  g2d.drawOval(x, y, width, height);

  // A dashed stroke

  float miterLimit = 10f;

  float[] dashPattern = {10f};

  float dashPhase = 5f;

  stroke = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,

  BasicStroke.JOIN_MITER, miterLimit, dashPattern, dashPhase);

  g2d.setStroke(stroke);

  // Draw an oval that fills half window

  g2d.drawOval(width/4, height/4, width/2, height/2);

}

  }

}

 
This was an example on how to draw using the stroking pen.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button