Concept explainers
Explanation of Solution
Refer the example “section_9_2/ RectangleComponent.java” which describes about how to display the component. In that program, let modify the line numbers “18” and “24” which contains the graphics2D object “g2” to “g”, then the following code displays the compiler error.
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
g.draw(box);
/* Move rectangle 15 units to the right and 25
units down*/
box.translate(15, 25);
// Draw moved rectangle
g.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...
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