AWT BasicStroke Class


Advertisements

Previous Page

Next Page

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:

  1. public class BasicStroke
  2. extends Object
  3. 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
1 BasicStroke() Constructs a new BasicStroke with defaults for all attributes.
2 BasicStroke(float width) Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.
3 BasicStroke(float width, int cap, int join) Constructs a solid BasicStroke with the specified attributes.
4 BasicStroke(float width, int cap, int join, float miterlimit) Constructs a solid BasicStroke with the specified attributes.
5 BasicStroke(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
1 Shape createStrokedShape(Shape s) Returns a Shape whose interior defines the stroked outline of a specified Shape.
2 boolean 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.
3 float[] getDashArray() Returns the array representing the lengths of the dash segments.
4 float getDashPhase() Returns the current dash phase.
5 int getEndCap() Returns the end cap style.
6 int getLineJoin() Returns the line join style.
7 float getLineWidth() Returns the line width.
8 float getMiterLimit() Returns the limit of miter joins.
9 int 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

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.geom.*;
  6.  
  7. public class AWTGraphicsDemo extends Frame {
  8.  
  9. public AWTGraphicsDemo(){
  10. super("Java AWT Examples");
  11. prepareGUI();
  12. }
  13.  
  14. public static void main(String[] args){
  15. AWTGraphicsDemo awtGraphicsDemo = new AWTGraphicsDemo();
  16. awtGraphicsDemo.setVisible(true);
  17. }
  18.  
  19. private void prepareGUI(){
  20. setSize(400,400);
  21. addWindowListener(new WindowAdapter() {
  22. public void windowClosing(WindowEvent windowEvent){
  23. System.exit(0);
  24. }
  25. });
  26. }
  27.  
  28. @Override
  29. public void paint(Graphics g) {
  30. Graphics2D g2 = (Graphics2D)g;
  31. g2.setStroke(new BasicStroke(3.0f));
  32. g2.setPaint(Color.blue);
  33.  
  34. Rectangle2D shape = new Rectangle2D.Float();
  35. shape.setFrame(100, 150, 200,100);
  36. g2.draw(shape);
  37.  
  38. Rectangle2D shape1 = new Rectangle2D.Float();
  39. shape1.setFrame(110, 160, 180,80);
  40. g2.setStroke(new BasicStroke(1.0f));
  41.  
  42. g2.draw(shape1);
  43. Font plainFont = new Font("Serif", Font.PLAIN, 24);
  44. g2.setFont(plainFont);
  45. g2.setColor(Color.DARK_GRAY);
  46. g2.drawString("TutorialsPoint", 130, 200);
  47. }
  48. }

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

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

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

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

Verify the following output

awt_basicstroke.jpg