To draw the red square and fill it with the specified background of yellow color it is need to use three methods such as “Rectangle()”, “fill()”, and “SetColor()”.
Rectangle() method:
- For two dimensional objects, the graphics2D class is used to draw the objects.
- The “Rectangle()” method contains the respective four co-ordinates such as “left, “top” , “right”, and “bottom”.
- The first parameter “left” represents the x-coordinate of top left corner.
- The second parameter “top” represents the y-coordinate of top left corner.
- The third parameter “right” represents the x-coordinate of right bottom corner.
- The fourth parameter “bottom” represents the y-coordinate of right bottom corner.
- The “Rectangle()” method follows the same co-ordinates of rectangle object. But for right and bottom co-ordinate it passes the same value as an argument to draw the square.
Let use the following syntax to draw the rectangle or square,
Rectangle(left,top,right,bottom);
Fill and set the color using “fill()”and “setColor()”:
Normally, the text strings and shapes drawn on the window are drawn with black color pen. To change the color of the string, the java uses the object type of “Color.Java” to set the Red, Green, and Blue (RGB) values.
Let use the following syntax to set the color,
// Set the color passed as argument
g2.setColor(Color.nameofcolor);
The method “setColor()” is used to set the specified color.
Let use the following syntax to fill the shape of the color,
// Fill the color inside of the shape
g2.fill(shape);
The method “fill()” is used to fill the color for specified shape passed as argument.
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Big Java, Binder Ready Version: Early Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education