Explanation of Solution
Modification of the program to draw one rectangle and one square:
Refer the example “section_9_2/ RectangleComponent.java” which describes about how to display the component. In that program, change the line number “21” of Rectangle Component which is highlighted as follows,
Code:
//Import the required java packages
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
//Define the method to draw the component
public class RectangleComponent extends JComponent
{
//Define the method paintComponent()
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;
// Construct a rectangle
Rectangle box = new Rectangle(5, 10, 20, 30);
//Draw the rectangle
g2.draw(box);
/* Move rectangle 15 units to the right and 25
units down*/
box = new Rectangle(5, 10, 20, 20);
// Draw moved rectangle
g2.draw(box);
}
}
File name: “Square.java”
//Import the required package
import javax.swing.JFrame;
//Declare the class "Square"
public class Square
{
//Define the main function
public static void main(String[] args)
{
//Create the object for JFrame class
JFrame frame = new JFrame();
//Set the size of the frame
frame.setSize(300, 400);
//Set the title of the frame
frame.setTitle("Two squares");
//Set the default operation to close the frame
frame...
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