Please write the code for the following programme using JAVA (including the UML!
ID: 669273 • Letter: P
Question
Please write the code for the following programme using JAVA (including the UML!!!)
And make sure the code compiles and programme runs successfully!
Create a subclass of JPanel, named RegularPolygonPanel, to paint an n-sided regular polygon. The class contains a property named numberOfSides, which specifies the number of sides in the polygon. The polygon is centered at the center of the panel. The size of the polygon is proportional to the size of the panel. Create a pentagon, hexagon, heptagon, and octagon, nonagon, and decagon fromRegularPolygonPanel and display them in a frame, as shown in the figure below.
Your Task:
1. Create a class named RegularPolygonPanel to paint an n-sided regular polygon. (So, if n is 3, it paints a triangle, if n is 4, it paints a square, etc.)
2. Create a frame class that contains a pentagon, hexagon, heptagon, and octagon, nonagon, and decagon. These objects are created fromRegularPolygonPanel.
3. Draw a UML diagram for RegularPolygonPanel class and the frame class.
Explanation / Answer
Hi,
Below is the solution to your question:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class practicePoly extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
// setLayout(new GridLayout(1, 4, 5, 5));
//draw poly > 5 sides > 120
g.setColor(Color.red);
Polygon polygon1 = new Polygon();
for (int i = 0; i < 6; i++){
polygon1.addPoint((int) (50 + 50 * Math.cos(i * 2 * Math.PI / 5)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 5)));
}
g.drawPolygon(polygon1);
//draw Pentagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon2 = new Polygon();
for (int i = 0; i < 5; i++){
polygon2.addPoint((int) (160 + 50 * Math.cos(i * 2 * Math.PI / 5)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 5)));
}
g.drawPolygon(polygon1);
//draw hexagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon2 = new Polygon();
for (int i = 0; i < 6; i++){
polygon2.addPoint((int) (160 + 50 * Math.cos(i * 2 * Math.PI / 6)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 6)));
}
g.drawPolygon(polygon2);
//draw heptagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon3 = new Polygon();
for (int i = 0; i < 7; i ++) {
polygon3.addPoint((int) (270 + 50 * Math.cos(i * 2 * Math.PI / 7)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 7)));
}
g.drawPolygon(polygon3);
//draw octagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon4 = new Polygon();
for (int i = 0; i < 8; i ++) {
polygon4.addPoint((int) (380 + 50 * Math.cos(i * 2 * Math.PI / 8)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 8)));
}
g.drawPolygon(polygon4);
//draw nonagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon5 = new Polygon();
for (int i = 0; i < 9; i ++) {
polygon5.addPoint((int) (490 + 50 * Math.cos(i * 2 * Math.PI / 9)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 9)));
}
g.drawPolygon(polygon5);
//draw decagon: color BLACK
g.setColor(Color.BLACK);
Polygon polygon6 = new Polygon();
for (int i = 0; i < 10; i ++) {
polygon6.addPoint((int) (600 + 50 * Math.cos(i * 2 * Math.PI / 10)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 10)));
}
g.drawPolygon(polygon6);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Show Different Polygons");
frame.setSize(700, 300);
Container contentPane = frame.getContentPane();
contentPane.add(new practicePoly());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Hope that helps...HAPPY ANSWERING!!!!!!!!!!!!