Java 使用JFrame的图形用户界面&;JPanel绘制自定义形状

Java 使用JFrame的图形用户界面&;JPanel绘制自定义形状,java,swing,graphics,jframe,jpanel,Java,Swing,Graphics,Jframe,Jpanel,我几乎创建了一个形状类,其中包含矩形,圆形,三角形扩展形状,以及一个方形类扩展圆形。我有处理这个主类的代码,但我很难将它转换成GUI,因为我不知道如何做第3步来将其结合起来,以及如何制作g.drawOval(给定x,y和半径)和绘制三角形(给定x,y,底和高度) Project6类必须扩展JFrame类 Project6构造函数必须设置GUI窗口 一种新的抽象方法:publicsvoiddisplay(graphicsg)添加到基类和派生类中 必须使用paintComponent方法设置自定义J

我几乎创建了一个
形状
类,其中包含
矩形
圆形
三角形
扩展
形状
,以及一个
方形
类扩展
圆形
。我有处理这个主类的代码,但我很难将它转换成GUI,因为我不知道如何做第3步来将其结合起来,以及如何制作
g.drawOval(给定x,y和半径)
绘制三角形(给定x,y,底和高度)

  • Project6类必须扩展JFrame类
  • Project6构造函数必须设置GUI窗口
  • 一种新的抽象方法:
    publicsvoiddisplay(graphicsg)添加到基类和派生类中
  • 必须使用
    paintComponent
    方法设置自定义
    JPanel
  • 新的
    display(Graphics g)
    方法必须在GUI窗口上绘制形状,并从
    paintComponent
    方法中的循环调用


  • 我无法更改数组的设置方式,但这不应该是扩展JFrame的类吗

     public Project6() {
     JFrame frame = new JFrame();
     frame.setSize(800, 700);                           
     frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
     frame.setLocationRelativeTo(null);                 //Center Frame
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     }  
    
    我是GUI新手,所以这有点难,但这对绘制形状有用吗?但是我得到一个错误,说不能从静态上下文引用非静态方法get()

    class NewPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(Triangle.getXPos(), 0, 0, Triangle.getYPos());
    g.drawLine(Triangle.getXPos(), 0, Triangle.getXPos, Triangle.getYPos());
    g.drawLine(Triangle.getXPos(), Triangle.getYPos, 0, Triangle.getYPos());
    g.drawRect(Rectangle.getXPos(), Rectangle.getYPos(), Rectangle.getWidth(), Rectangle.getHeight());
    g.drawRect(Square.getXPos(), Square.getYPos(), Square.getWidth(), Square.getHeight());
    g.drawOval(Circle.getXPos(), Circle.getYPos(), Circle.getRadius(), 10);
    
    for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
    thearray[i].display();
    }
    } 
    
    class NewPanel扩展了JPanel{
    @凌驾
    公共组件(图形g){
    超级组件(g);
    g、 绘制线(Triangle.getXPos(),0,0,Triangle.getYPos());
    g、 绘制线(Triangle.getXPos(),0,Triangle.getXPos,Triangle.getYPos());
    g、 绘制线(Triangle.getXPos(),Triangle.getYPos,0,Triangle.getYPos());
    g、 drawRect(Rectangle.getXPos(),Rectangle.getYPos(),Rectangle.getWidth(),Rectangle.getHeight());
    g、 drawRect(Square.getXPos(),Square.getYPos(),Square.getWidth(),Square.getHeight());
    g、 drawOval(Circle.getXPos(),Circle.getYPos(),Circle.getRadius(),10);
    对于(int i=0;i
  • 您的类扩展了一个从不显示的JFrame
  • 您应该使用JPanel的paintComponent方法绘制图形,该方法添加到JFrame中
  • 我会使用
    ArrayList
    ,而不是数组,因为这样我就可以向我的集合中添加尽可能多或尽可能少的形状,而不必担心空项
  • 然后在paintComponent方法override中遍历集合,并使用Graphics2D对象绘制每个形状
  • 关于您的最后一个问题,
    “我是否在每个类的末尾添加类似的内容?即(圆形、方形、三角形、矩形类?…”
    不,不需要“绘制”方法,因为您将使用paintComponent方法进行绘制

  • 我不能改变数组,我只需要添加JFrame和JPanel并显示形状,但不需要t@FalafelMan:你上面的陈述让我非常困惑。请澄清。好的,这是说明,我已经完成了除第4阶段之外的所有工作,我不知道如何做[link]@请删除你的两个非答案。改为编辑和改进你的原始问题。它的正方形延伸矩形*而不是圆形
     public Project6() {
     JFrame frame = new JFrame();
     frame.setSize(800, 700);                           
     frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
     frame.setLocationRelativeTo(null);                 //Center Frame
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     }  
    
    class NewPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(Triangle.getXPos(), 0, 0, Triangle.getYPos());
    g.drawLine(Triangle.getXPos(), 0, Triangle.getXPos, Triangle.getYPos());
    g.drawLine(Triangle.getXPos(), Triangle.getYPos, 0, Triangle.getYPos());
    g.drawRect(Rectangle.getXPos(), Rectangle.getYPos(), Rectangle.getWidth(), Rectangle.getHeight());
    g.drawRect(Square.getXPos(), Square.getYPos(), Square.getWidth(), Square.getHeight());
    g.drawOval(Circle.getXPos(), Circle.getYPos(), Circle.getRadius(), 10);
    
    for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
    thearray[i].display();
    }
    }