Java how to draw Graphic

Java how to draw Graphic

To draw in Java, you can use the Graphics class and its various drawing methods. The Graphics class provides a set of methods for drawing shapes, text, and images on a graphical user interface (GUI) or on a BufferedImage.

Here are some examples of how to use the Graphics class to draw in Java:

  • Draw a line:
graphics.drawLine(x1, y1, x2, y2);
S‮ruo‬ce:www.lautturi.com

This method draws a line between the points (x1, y1) and (x2, y2).

  • Draw a rectangle:
graphics.drawRect(x, y, width, height);

This method draws a rectangle with the top-left corner at (x, y) and the specified width and height.

  • Draw an oval:
graphics.drawOval(x, y, width, height);

This method draws an oval inscribed in the rectangle with the top-left corner at (x, y) and the specified width and height.

  • Draw a string:
graphics.drawString(string, x, y);

This method draws the specified string at the point (x, y).

To display the drawings on a GUI, you can create a custom component that extends JPanel and overrides the paintComponent() method. Then, you can add the custom component to a JFrame or any other container.

Created Time:2017-11-03 22:21:06  Author:lautturi