错误:类,接口或枚举错误代码

时间:2015-09-12 23:53:55

标签: java

我想要一个GUI,它会随机生成不同颜色的星星。这是代码。它可能很简单,但我找不到问题大声笑。我将类命名为Shapes2JPanel.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JPanel;

public class Shapes2JPanel extends JPanel {
    // draw general paths
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // call superclass's paintComponent
        Random random = new Random(); // get random number generator

        int[] xPoints = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
        int[] yPoints = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};
        GeneralPath star = new GeneralPath();

        for (int count = 1; count < xPoints.length; count++)
            star.lineTo(xPoints[count], yPoints[count]);
        star.closePath();
        g2d.translate(150, 150);
        for (int count = 1; count <= 20; count++)
            g2d.rotate(math.PI / 10.0);
        g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g2d.fill(star);
    }
}
}

    public static void main(String[] args) {
        JFrame frame = new JFrame("Drawing 2D Shapes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        1Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
        frame.add(shapes2JPanel); // add shapes2JPanel to frame
        frame.setBackground(Color.WHITE); // set frame background color
        frame.setSize(315, 330); // set frame size
        frame.setVisible(true); // display frame
    } // end main
} // end class Shapes2

1 个答案:

答案 0 :(得分:1)

一旦我解决了很多编译问题,也许复制和粘贴真的出错了,唯一的问题就是没有一个moveTo()用于星上的第一个点。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Shapes2JPanel extends JPanel {

    // draw general paths
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // call superclass's paintComponent
        Random random = new Random(); // get random number generator
        Graphics2D g2d = (Graphics2D) g;
        int[] xPoints = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
        int[] yPoints = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};
        GeneralPath star = new GeneralPath();
        star.moveTo(xPoints[0], yPoints[0]);
        for (int count = 1; count < xPoints.length; count++) {
            star.lineTo(xPoints[count], yPoints[count]);
        }
        star.closePath();
        g2d.translate(150, 150);
        for (int count = 1; count <= 20; count++) {
            g2d.rotate(Math.PI / 10.0);
        }
        g2d.setColor(new Color(random.nextInt(256),
                random.nextInt(256), random.nextInt(256)));
        g2d.fill(star);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Drawing 2D Shapes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
        frame.add(shapes2JPanel); // add shapes2JPanel to frame
        frame.setBackground(Color.WHITE); // set frame background color
        frame.setSize(315, 330); // set frame size
        frame.setVisible(true); // display frame
    } // end main
} // end class Shapes2