Project: Displaying Regular Polygons Problem Description: Create a subclass of J
ID: 3545007 • Letter: P
Question
Project: Displaying Regular Polygons
Problem Description:
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 from RegularPolygonPanel 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 classes that contains pentagon, hexagon, heptagon, and octagon, nonagon, and decagon. These objects are created from RegularPolygonPanel.
3. Draw a UML diagram for RegularPolygonPanel class and the frame class.
2. Fill in self-evaluation:
Explanation / Answer
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class PolygonTry 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 triangle = new Polygon();
for (int i = 0; i < 3; i++){
triangle.addPoint((int) (50 + 50 * Math.cos(i * 2 * Math.PI / 3)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 3)));
}
g.fillPolygon(triangle);
g.setColor(Color.red);
Polygon polygon1 = new Polygon();
for (int i = 0; i < 5; i++){
polygon1.addPoint((int) (150 + 50 * Math.cos(i * 2 * Math.PI / 5)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 5)));
}
g.fillPolygon(polygon1);
//draw hexagon: color BLUE
g.setColor(Color.BLUE);
Polygon polygon2 = new Polygon();
for (int i = 0; i < 6; i++){
polygon2.addPoint((int) (260 + 50 * Math.cos(i * 2 * Math.PI / 6)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 6)));
}
g.fillPolygon(polygon2);
//draw heptagon: color GREEN
g.setColor(Color.GREEN);
Polygon polygon3 = new Polygon();
for (int i = 0; i < 7; i ++) {
polygon3.addPoint((int) (370 + 50 * Math.cos(i * 2 * Math.PI / 7)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 7)));
}
g.fillPolygon(polygon3);
//draw octagon: color PINK
g.setColor(Color.PINK);
Polygon polygon4 = new Polygon();
for (int i = 0; i < 8; i ++) {
polygon4.addPoint((int) (480 + 50 * Math.cos(i * 2 * Math.PI / 8)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 8)));
}
g.fillPolygon(polygon4);
//draw nonagon: color MAGENTA
g.setColor(Color.MAGENTA);
Polygon polygon5 = new Polygon();
for (int i = 0; i < 9; i ++) {
polygon5.addPoint((int) (590 + 50 * Math.cos(i * 2 * Math.PI / 9)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 9)));
}
g.fillPolygon(polygon5);
//draw decagon: color ORANGE
g.setColor(Color.ORANGE);
Polygon polygon6 = new Polygon();
for (int i = 0; i < 10; i ++) {
polygon6.addPoint((int) (700 + 50 * Math.cos(i * 2 * Math.PI / 10)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 10)));
}
g.fillPolygon(polygon6);
g.fillPolygon(polygon5);
//draw decagon: color ORANGE
g.setColor(Color.BLACK);
Polygon square = new Polygon();
for (int i = 0; i < 4; i ++) {
square.addPoint((int) (810 + 50 * Math.cos(i * 2 * Math.PI / 4)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 4)));
}
g.fillPolygon(square);
}
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 PolygonTry());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.