Show Stroke with Effect

The interface Stroke gives the stylish way of representing the outlines. To show the effect, class GradientPaint is used which fills the specified figure in a color gradient pattern. The class Rectangle2D provides the rectangle figure.

Show Stroke with Effect

The interface Stroke gives the stylish way of representing the outlines. To show the effect, class GradientPaint is used which fills the specified figure in a color gradient pattern. The class Rectangle2D provides the rectangle figure.

Show Stroke with Effect

Show Stroke with Effect

     

This section illustrates you how to show stroke with effect.

The interface Stroke gives the stylish way of representing the outlines. To show the effect, class GradientPaint is used which fills the specified figure in a color gradient pattern. The class Rectangle2D provides the rectangle figure.

Following code shows the simple rectangle filled with color gradient pattern:

g2d.setPaint(gradientPaint);
g2d.fill(rectangle);

Following code shows the stroke inside the rectangle:

rectangle.setFrame(p + 90, q, width, height);
g2d.setPaint(gradientPaint);
g2d.setStroke(new BasicStroke(10));
g2d.draw(rectangle);

Following code strokes with gradient:

rectangle.setFrame(p + 180, q, width, height);
g2d.setPaint(Color.black);
g2d.draw(rectangle);

The method setFrame() sets the size of the rectangle. The setPaint(gradientPaint) paints the rectangle in a gradient pattern. The method setStroke() sets the stroke settings for the Graphics2D context, when you draw the shape. The integer value passed into the constructor of class BasicStroke represents the thickness of the outline.

Here is the code of EffectsOfStroke.java

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

public class EffectsOfStroke extends JPanel{
  public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  double p = 12, q = 45, width = 65, height = 65;
  Rectangle2D rectangle = new Rectangle2D.Double(p, q, width, height);
  GradientPaint gradientPaint= new GradientPaint(6565, Color.red,
    
9090,Color.cyan, true);
  g2d.setPaint(gradientPaint);
  g2d.fill(rectangle);
  rectangle.setFrame(p + 90, q, width, height);
  g2d.setPaint(gradientPaint);
    g2d.setStroke(new BasicStroke(10));
  g2d.draw(rectangle);
  rectangle.setFrame(p + 180, q, width, height);
  g2d.setPaint(Color.black);
  g2d.draw(rectangle);
  }

  public static void main(String[] args) {
  JFrame frame = new JFrame("Show Effect");
  frame.getContentPane().add(new EffectsOfStroke());
  frame.setSize(350200);
  frame.show();
  }
}

Output will be displayed as:

Download Source Code