Java.awt is a package library in Java that provides a set of tools to create graphical user interfaces (GUIs) for applications. One of the key classes in this package is the Graphics class, which is used to draw and manipulate 2D shapes and images.
The Graphics.drawRect method is used to draw a rectangle onto the current graphics context. This method takes four parameters that specify the x and y coordinates of the rectangle's upper-left corner, as well as its width and height.
// create a Graphics object from a canvas or JFrame Graphics g = canvas.getGraphics();
// draw a rectangle at (10,10) with a width and height of 50 g.drawRect(10, 10, 50, 50);
// fill a rectangle at (20,20) with a width and height of 30 g.fillRect(20, 20, 30, 30);
In the above example, we have created a graphics object 'g'. The first drawRect function call will draw a rectangle with a black outline, positioned at coordinate (10,10), and having width and height of 50 pixels each. The second fillRect function will fill a rectangle independent of the outline.
These methods are useful in 2D graphics programming and can be used in various applications like games, image editing tools, and animation software.
Java Graphics.drawRect - 30 examples found. These are the top rated real world Java examples of java.awt.Graphics.drawRect extracted from open source projects. You can rate examples to help us improve the quality of examples.