Home Java Example Java Swing Graphics2D Show Stroke with Effect



Show Stroke with Effect
Posted on: October 7, 2008 at 12:00 AM
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

     

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

Related Tags for Show Stroke with Effect:
cideclassinterfacesedcolor2dvipatternintlineidairadshowifiegradientpainttorectanglelinescianglesheffecteilfilldeslsliusepeceinasntpntoutoutlinetradaceclesspechowprowhichsrectspcolatkisllivprepainstrreprttssthshostfefaceprolo


More Tutorials from this section

Ask Questions?    Discuss: Show Stroke with Effect  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.