import javax.swing.
*;
import java.awt.*;
//This class creates the main window and sets up the component for
drawing.
public class TwoSquareViewer {
//The main method to run the program
public static void main(String[] args) {
// Create a new JFrame (window) with a title
JFrame frame = new JFrame("Two Squares");
// Set the close operation to exit the application when the
window is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add an instance of TwoSquareComponent to the frame
frame.add(new TwoSquareComponent());
// Set the size of the frame
frame.setSize(300, 300);
// Make the frame visible
frame.setVisible(true);
}
}
//This class extends JComponent and is responsible for drawing the two
squares.
class TwoSquareComponent extends JComponent {
//the paintComponent method to perform custom drawing
protected void paintComponent(Graphics g) {
// Call the superclass method to ensure proper rendering
super.paintComponent(g);
// Cast Graphics to Graphics2D for more advanced drawing
capabilities
Graphics2D g2d = (Graphics2D) g;
// Draw pink square using standard color
g2d.setColor(Color.PINK);
g2d.fillRect(50, 50, 100, 100);
// Create a custom purple color using RGB values
Color customPurple = new Color(128, 0, 128); // RGB values for
purple
// Draw purple square using custom color
g2d.setColor(customPurple);
g2d.fillRect(150, 150, 100, 100);
}
}