AWT BasicStroke Class

Advertisements


Introduction

The BasicStroke class states colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.

Class declaration

Following is the declaration for java.awt.BasicStroke class:

public class BasicStroke
   extends Object
      implements Stroke

Field

Following are the fields for java.awt.geom.Arc2D class:

  • static int CAP_BUTT -- Ends unclosed subpaths and dash segments with no added decoration.

  • static int CAP_ROUND -- Ends unclosed subpaths and dash segments with a round decoration that has a radius equal to half of the width of the pen.

  • static int CAP_SQUARE -- Ends unclosed subpaths and dash segments with a square projection that extends beyond the end of the segment to a distance equal to half of the line width.

  • static int JOIN_BEVEL -- Joins path segments by connecting the outer corners of their wide outlines with a straight segment.

  • static int JOIN_MITER -- Joins path segments by extending their outside edges until they meet.

  • static int JOIN_ROUND -- Joins path segments by rounding off the corner at a radius of half the line width.

Class constructors

S.N.Constructor & Description
1BasicStroke()
Constructs a new BasicStroke with defaults for all attributes.
2BasicStroke(float width)
Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.
3BasicStroke(float width, int cap, int join)
Constructs a solid BasicStroke with the specified attributes.
4BasicStroke(float width, int cap, int join, float miterlimit)
Constructs a solid BasicStroke with the specified attributes.
5BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)
Constructs a new BasicStroke with the specified attributes.

Class methods

S.N.Method & Description
1Shape createStrokedShape(Shape s)
Returns a Shape whose interior defines the stroked outline of a specified Shape.
2boolean equals(Object obj)
Tests if a specified object is equal to this BasicStroke by first testing if it is a BasicStroke and then comparing its width, join, cap, miter limit, dash, and dash phase attributes with those of this BasicStroke.
3float[] getDashArray()
Returns the array representing the lengths of the dash segments.
4float getDashPhase()
Returns the current dash phase.
5int getEndCap()
Returns the end cap style.
6int getLineJoin()
Returns the line join style.
7float getLineWidth()
Returns the line width.
8float getMiterLimit()
Returns the limit of miter joins.
9int hashCode()
Returns the hashcode for this stroke.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

BasicStroke Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      g2.setStroke(new BasicStroke(3.0f));
      g2.setPaint(Color.blue);

      Rectangle2D shape = new Rectangle2D.Float();
      shape.setFrame(100, 150, 200,100);
      g2.draw(shape);

      Rectangle2D shape1 = new Rectangle2D.Float();
      shape1.setFrame(110, 160, 180,80);
      g2.setStroke(new BasicStroke(1.0f));
   
      g2.draw(shape1);
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.setColor(Color.DARK_GRAY);
      g2.drawString("TutorialsPoint", 130, 200);
   }
}

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

D:\AWT>javac com\tutorialspoint\gui\AwtGraphicsDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.tutorialspoint.gui.AwtGraphicsDemo

Verify the following output

AWT BasicStroke

Advertisements
Advertisements