我的工作任务是:
创建JPanel的子类,名为RegularPolygonPanel,以绘制n边正多边形。该类包含一个名为numberOfSides的属性,该属性指定多边形中的边数。多边形以面板中心为中心。多边形的大小与面板的大小成比例。从RegularPolygonPanel创建五边形,六边形,七边形和八边形,九边形和十边形,并将它们显示在一个框架中。
你的任务: 1.创建一个名为RegularPolygonPanel的类来绘制n边正多边形。 (因此,如果n为3,则绘制一个三角形,如果n为4,则绘制正方形等) 2.创建一个包含五边形,六边形,七边形和八边形,九边形和十边形的框架类。这些对象是从RegularPolygonPanel创建的。 3.为RegularPolygonPanel类和框架类绘制UML图。
到目前为止,我已经能够制作gui并显示六边形,我假设(形状有6个边) !http://imgur.com/qOk4OPI
老实说,如果是指导请求而不是特定问题,则更多。因此,在基本术语中,我需要让gui显示6种不同的多边形形状,而另一种形状是5种常规形状,我将在以后专注于常规形状。谢谢!
我写的代码是:
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