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:
graphics.drawLine(x1, y1, x2, y2);Sruoce:www.lautturi.com
This method draws a line between the points (x1, y1) and (x2, y2).
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.
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.
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.