Working with Pens
Aspose.Drawing for Java API empowers you to create graphic elements. Pens play a crucial role in graphic operations, allowing you to draw various line objects like ellipses, circles, etc., with specified colors.
Set Pen Width to Draw Graphics in Java
To draw graphics with a specific pen width, follow these steps:
- Create an object of
Bitmap
class. - Initialize an object of
Graphics
class from this bitmap - Define a
Pen
object with desired parameters - Utilize the
drawLine()
method to draw a line with the specified width. - Save the output in any desired image format.
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
for (int i = 1; i < 8; ++i) | |
{ | |
Pen pen = new Pen(Color.getBlue(), i); | |
graphics.drawLine(pen, 100, i * 100, 900, i * 100); | |
} | |
bitmap.save("Width.png"); |
Set Pen Color to Draw Graphics in Java
To draw graphics with a certain pen color, the following steps can be used.
- Create an object of
Bitmap
class. - Initialize an object of
Graphics
class from this bitmap - Define a
Pen
object with desired parameters - Use the
drawLine()
method to draw a line - Save the output to any desired output image format
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
Pen bluePen = new Pen(Color.getBlue(), 2); | |
graphics.drawLine(bluePen, 100, 100, 900, 100); | |
Pen redPen = new Pen(Color.fromArgb(255, 255, 0, 0), 2); | |
graphics.drawLine(redPen, 100, 200, 900, 200); | |
bitmap.save("Colors.png"); |
Join Lines to Create Path in Java
Multiple lines can be joined to create a path. To join lines using Java, the following steps can be used.
- Create an object of
Bitmap
class. - Initialize an object of
Graphics
class from this bitmap - Create a
GraphicsPath
object to establish a path - Use the
setLineJoin()
method to join lines
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
import com.aspose.drawing.drawing2d; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
drawPath(graphics, LineJoin.Bevel, 200); | |
drawPath(graphics, LineJoin.Round, 400); | |
bitmap.save("Join.png"); | |
void drawPath(Graphics graphics, int join, int y) | |
{ | |
Pen pen = new Pen(Color.getBlue(), 30); | |
GraphicsPath path = new GraphicsPath(); | |
path.startFigure(); | |
path.addLine(100, y, 200, y); | |
path.addLine(200, y, 200, y + 100); | |
pen.setLineJoin(join); | |
graphics.drawPath(pen, path); | |
} |