为什么组件没有绘制任何东西



抱歉,这可能是一个非常基本的问题,但我有一个名为 Butterfly 的类,其中包含用于绘制蝴蝶对象的代码,如下所示:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BasicStroke;
public class butterfly extends JPanel {
public butterfly() {
}
     public void draw(Graphics2D g2){
    //the width of the oval. A petal is an oval.
    //TODO: Your code goes IN HERE (not in main), ALL OF IT. Replace the code below.
    //draw one oval of length 300pixels from center to edge
    g2.setColor(Color.blue);
    g2.fillOval(500,55, 70, 70);
    g2.fillOval(450, 40, 70, 70);
    g2.fillOval(460,100, 40, 40);
    g2.fillOval(495, 110, 40, 40);

    g2.setColor(Color.black);
    g2.setStroke(new BasicStroke(13));
    g2.drawLine(520, 50, 490, 150);
 }
}

然后我尝试将其放入此组件中,以便我可以放置更多对象,但它甚至不会画蝴蝶?

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RippingOffofAmy extends JComponent
{
    private butterfly yay;
    public RippingOffofAmy()
    {
        yay = new butterfly();
    }
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
       //checks to see if rectangle has moves 100 places yet
       //if has, then adds new obstacle and resets count
       yay.draw(g2);
        //draws obstacle and avatar
    }
}

为什么?(忽略我使用的一些朋友代码的类名和注释)如果我错过了什么,有人可以非常具体地说明要添加什么 bc 我在 java 上很糟糕,谢谢!

您应该阅读一些教程,但是这是您尝试执行的操作。

这是我要做的:

package de;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
@SuppressWarnings("serial")
public class Butterfly extends JPanel {
@Override
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);
    g2d.fillOval(500,55, 70, 70);
    g2d.fillOval(450, 40, 70, 70);
    g2d.fillOval(460,100, 40, 40);
    g2d.fillOval(495, 110, 40, 40);

    g2d.setColor(Color.black);
    g2d.setStroke(new BasicStroke(13));
    g2d.drawLine(520, 50, 490, 150);
}

}

简要说明:创建一个 JFrame(这是窗口本身)覆盖 JPanel 的疼痛功能在疼痛函数中输入蝴蝶的代码创建一个"蝴蝶"(扩展JPanel)将蝴蝶添加到 JFrame 中:)运行程序

相关内容

  • 没有找到相关文章

最新更新