I have a work assignment which is:
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.
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.
Now so far I have been able to make the gui and display a hexagon, I assume(the shape has 6 sides) !https://i.stack.imgur.com/Qgr7W.jpg
In all honestly this is more if a request for guidance than a specific question. So in basic terms I need to make the gui show the 6 different polygon shapes and in another one 5 regular shapes, I will concentrate on the regular shapes later. Thanks!
My code I have written is:
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RegularPolygonPanel extends JPanel {
private Polygon pentagon;
private JFrame framePolygon;
public RegularPolygonPanel()
{
draw();
}
public void draw()
{
framePolygon = new JFrame();
framePolygon.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int xPoly[] = {40, 70, 60, 45, 20};
int yPoly[] = {20, 40, 80, 45, 60};
pentagon = new Polygon(xPoly, yPoly, xPoly.length);
JPanel p = new JPanel()
{
@Override
protected void paintComponent (Graphics g)
{
super.paintComponent(g);
g.drawPolygon(pentagon);
}
};
framePolygon.add(p);
framePolygon.pack();
framePolygon.setSize(300, 200);
framePolygon.setVisible(true);
}// end of draw
class Hexagon extends RegularPolygonPanel
{
private Polygon hexagon;
public Hexagon()
{
draw();
}
public void draw()
{
int xHex[] = {30, 20, 60, 45, 20};
int yHex[] = {20, 40, 65, 45, 60};
hexagon = new Polygon(xHex, yHex, xHex.length);
JPanel p = new JPanel()
{
@Override
protected void paintComponent (Graphics g)
{
super.paintComponent(g);
g.drawPolygon(hexagon);
}
};
framePolygon.add(p);
framePolygon.pack();
framePolygon.setSize(300, 200);
framePolygon.setVisible(true);
}// end of draw
}//end of hexagon class
public static void main(String[] args)
{
new RegularPolygonPanel();
//Polygon hexagon = new Polygon();
}
}//end of main class