0% found this document useful (0 votes)
11 views1 page

TwoSqureComponent Java

Uploaded by

prathambhambi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

TwoSqureComponent Java

Uploaded by

prathambhambi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;

//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
[Link](JFrame.EXIT_ON_CLOSE);

// Add an instance of TwoSquareComponent to the frame


[Link](new TwoSquareComponent());

// Set the size of the frame


[Link](300, 300);

// Make the frame visible


[Link](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
[Link](g);

// Cast Graphics to Graphics2D for more advanced drawing


capabilities
Graphics2D g2d = (Graphics2D) g;

// Draw pink square using standard color


[Link]([Link]);
[Link](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


[Link](customPurple);
[Link](150, 150, 100, 100);
}
}

You might also like