Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 27, 2024 09:37
Show Gist options
  • Save aspose-com-gists/b6d2124e9bea9c6ad94fc58b90ded395 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b6d2124e9bea9c6ad94fc58b90ded395 to your computer and use it in GitHub Desktop.
Draw a Line in Java
public class Main {
public static void main(String[] args) throws java.io.IOException {
String dir = "/Desktop/";
String outpath = dir+"line.bmp";
// Create an instance of the BmpOptions class.
BmpOptions saveOptions = new BmpOptions();
// Invoke the setBitsPerPixel method to set the Bits per Pixel.
saveOptions.setBitsPerPixel(32);
// Initialize an object of Image class with an instance of the PsdImage class.
try (Image image = new PsdImage(100, 100)) {
// Create and initialize an object of the Graphics class and set the image background color.
Graphics graphic = new Graphics(image);
graphic.clear(Color.getYellow());
// Call the drawLine method to draw two dotted diagonal lines by specifying the Pen object having blue color and coordinate Points.
graphic.drawLine(new Pen(Color.getBlue()), 19, 19, 90, 90);
graphic.drawLine(new Pen(Color.getBlue()), 19, 90, 90, 9);
// The save method will save the file on disk.
image.save(outpath, saveOptions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment