Show Different Strokes

To give the the impressive and stylistic outline, class BasicStroke is used. The value passed in the constructor of the class BasicStroke shows the thickness of outline.

Show Different Strokes

To give the the impressive and stylistic outline, class BasicStroke is used. The value passed in the constructor of the class BasicStroke shows the thickness of outline.

Show Different Strokes

Show Different Strokes

     

In this section, you will see different types of strokes.

To give the the impressive and stylistic outline, class BasicStroke is used. The value passed in the constructor of the class BasicStroke shows the thickness of outline. The class GlyphVector provides the collection of glyphs. A glyph is a shape of a character. The method createGlyphVector() creates a glyphVector by mapping characters to glyphs. The method getOutline() of class GlyphVector returns a shape. 

We have defined three types of strokes. The first one is the predefined stroke, Stroke1 strokes twice, Stroke2 shows the vertices and control points. The shape, we are taken is 'HELLO'. 

The stroke1 uses the class BasicStroke twice. Here we are outlining the outline of the shape. The stroke2 strokes the shape using a thin line and displays the end points and control points.

The method createStrokesShape() returns the stroked outline of a specified shape. The class PathIterator retrieves the path and provides some fields. The SEG_CLOSE, closed the path by appending a line segment. The SEG_CUBICTO specifies a cubic parametric curve. The SEG_LINETO specifies the end point of a line. The SEG_MOVETO specifies the starting location for a new path. The SEG_QUADTO specify a quadratic parametric curve.

Here is the code of ShowDifferentStrokes.java 

import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;

public class ShowDifferentStrokes extends JPanel{
  Stroke[] stroke = new Stroke[] { new BasicStroke(3.0f),
 new Stroke1(7.0f1.0f), new Stroke2(2.0f) };

  public void paint(Graphics g1) {
  Graphics2D g2d = (Graphics2D)g1;
  Font font = new Font("Arial Narrow", Font.BOLD, 40);
  GlyphVector glyphVector = font.createGlyphVector
  (g2d.getFontRenderContext(), "HELLO");
  Shape shape = glyphVector.getOutline();
  g2d.setColor(Color.red);
  g2d.translate(10175);


  for (int i = 0; i < stroke.length; i++) {
  g2d.setStroke(stroke[i]); 
  g2d.draw(shape); 
  g2d.translate(1400); 
  }
  }
  public static void main(String[] args) {
  JFrame frame = new JFrame("Show Different Strokes");
  frame.setContentPane(new ShowDifferentStrokes());
  frame.setSize(450,300);
  frame.show();
  }
}
class Stroke1 implements Stroke {
  BasicStroke basicStroke1, basicStroke2;
  public Stroke1(float w1, float w2) {
  basicStroke1 = new BasicStroke(w1); 
  basicStroke2 = new BasicStroke(w2); 
  }
public Shape createStrokedShape(Shape sh) {
  Shape shape = basicStroke1.createStrokedShape(sh);
  return basicStroke2.createStrokedShape(shape);
  }
}
class Stroke2 implements Stroke {
  float radius; 
  public Stroke2(float radius) {
  this.radius = radius;
  }
  public Shape createStrokedShape(Shape sh) {
  GeneralPath path = new GeneralPath(new BasicStroke(1.0f)
  .createStrokedShape(sh));
  float[] fl = new float[6];
  for (PathIterator iterator = sh.getPathIterator(null); 
   !iterator.isDone();iterator.next()) {

  int type = iterator.currentSegment(fl);
  switch (type) {
  case PathIterator.SEG_CUBICTO:
  coordinate1(path, fl[4], fl[5]); 
  case PathIterator.SEG_QUADTO:
  coordinate1(path, fl[2], fl[3]);
  case PathIterator.SEG_MOVETO:
  case PathIterator.SEG_LINETO:
  coordinate1(path, fl[0], fl[1]);
  case PathIterator.SEG_CLOSE:
  break;
  }
  }
 return path;
  }
  void coordinate1(GeneralPath path, float x, float y) {
  path.moveTo(x - radius, y - radius); 
  path.lineTo(x + radius, y - radius); 
  path.lineTo(x + radius, y + radius);
  path.lineTo(x - radius, y + radius); 
  path.closePath(); 
  }
}

Output will be displayed as:

Download Source Code