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
}
}
为什么呢? (忽略我使用我的一些朋友代码的类名和评论)如果我遗漏了某些东西,那么某人可以真正具体地说明要添加什么,这对我来说非常糟糕,谢谢!
答案 0 :(得分:-1)
您应该阅读一些教程,但这是您尝试做的。
以下是我要做的事情:
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的痛苦功能 将butterfly的代码输入pain函数 创造一个蝴蝶'(扩展JPanel) 将蝴蝶添加到JFrame 运行程序:)