如何在Java中画一颗星星?

xxe27gdn  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(124)

我想在一些点上画一个星星。我的问题是它没有显示我的小星星的线条。我错过了什么?我正在画每个点,画线条,设置颜色,它只是没有显示我的星星。它确实显示了框架,但我认为问题不是框架,而是代码的实际主体。你们建议尝试什么?

public class StarClass 
implements Icon {
    static JFrame frame;
    public static void main(String[] args) {
    JFrame frame = new JFrame();

    frame.setSize(400, 400);
    frame.setTitle("My Star");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {

    Graphics2D g2 = (Graphics2D) g;

    //points
    Point2D.Double pt1 
        = new Point2D.Double(100, 10);
    Point2D.Double pt2 
        = new Point2D.Double(125, 75);
    Point2D.Double pt3 
        = new Point2D.Double(200, 85);
    Point2D.Double pt4 
        = new Point2D.Double(150, 125);
    Point2D.Double pt5 
        = new Point2D.Double(160, 190);
    Point2D.Double pt6 
        = new Point2D.Double(100, 150);
    Point2D.Double pt7 
        = new Point2D.Double(40, 190);
    Point2D.Double pt8 
        = new Point2D.Double(50, 125);
    Point2D.Double pt9 
        = new Point2D.Double(0, 85);

    //lines
    Line2D.Double ln1 
        = new Line2D.Double(pt1, pt2);
    Line2D.Double ln2 
        = new Line2D.Double(pt2, pt3);
    Line2D.Double ln3 
        = new Line2D.Double(pt3, pt4);
    Line2D.Double ln4 
        = new Line2D.Double(pt4, pt5);
    Line2D.Double ln5 
        = new Line2D.Double(pt5, pt6);
    Line2D.Double ln6 
        = new Line2D.Double(pt6, pt7);
    Line2D.Double ln7 
        = new Line2D.Double(pt7, pt8);
    Line2D.Double ln8 
        = new Line2D.Double(pt8, pt9);

    //color of lines
    g2.setColor(Color.BLUE);

    //draw the lines
    g2.draw(ln1);
    g2.draw(ln2);
    g2.draw(ln3);
    g2.draw(ln4);
    g2.draw(ln5);
    g2.draw(ln6);
    g2.draw(ln7);
    g2.draw(ln8);

    }

    @Override
    public int getIconWidth() {
        return 200;
    }

    @Override
    public int getIconHeight() {
        return 200;
    }
}
b1zrtrql

b1zrtrql1#

您应该找到一个比paintIcon更好的地方来完成这一切。
在您的情况下根本不调用此方法。
这里是你的代码修复。似乎你只是缺少1-2行
从星星开始(这个问题你应该可以自己调试)。

import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.*;

class DrawPanel extends JPanel {

    private static final long serialVersionUID = 776058311964590886L;

    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;

        // points
        Point2D.Double pt1 = new Point2D.Double(100, 10);
        Point2D.Double pt2 = new Point2D.Double(125, 75);
        Point2D.Double pt3 = new Point2D.Double(200, 85);
        Point2D.Double pt4 = new Point2D.Double(150, 125);
        Point2D.Double pt5 = new Point2D.Double(160, 190);
        Point2D.Double pt6 = new Point2D.Double(100, 150);
        Point2D.Double pt7 = new Point2D.Double(40, 190);
        Point2D.Double pt8 = new Point2D.Double(50, 125);
        Point2D.Double pt9 = new Point2D.Double(0, 85);

        // lines
        Line2D.Double ln1 = new Line2D.Double(pt1, pt2);
        Line2D.Double ln2 = new Line2D.Double(pt2, pt3);
        Line2D.Double ln3 = new Line2D.Double(pt3, pt4);
        Line2D.Double ln4 = new Line2D.Double(pt4, pt5);
        Line2D.Double ln5 = new Line2D.Double(pt5, pt6);
        Line2D.Double ln6 = new Line2D.Double(pt6, pt7);
        Line2D.Double ln7 = new Line2D.Double(pt7, pt8);
        Line2D.Double ln8 = new Line2D.Double(pt8, pt9);

        // color of lines
        g2.setColor(Color.BLUE);

        // draw the lines
        g2.draw(ln1);
        g2.draw(ln2);
        g2.draw(ln3);
        g2.draw(ln4);
        g2.draw(ln5);
        g2.draw(ln6);
        g2.draw(ln7);
        g2.draw(ln8);
    }
}

public class StarClass {

    static JFrame frame;

    public static void main(String[] args) {

        DrawPanel panel = new DrawPanel();

        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);

        frame.setSize(400, 400);
        frame.setTitle("My Star");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
olmpazwi

olmpazwi2#

import java.awt.*;
import javax.swing.*;
public class Try extends JFrame {
    public Try(){
    super("fatimah");
    setSize(700,700);
    setVisible(true);
    }
    @Override
    public void paint(Graphics g){
    super.paint(g);
    int x1[]={50,125,100,75,150};
    int y1[]={70,200,35,200,90};
    g.drawPolygon(x1,y1,5);
    g.setColor(Color.yellow);
    int x2[]={150,240,205,140,260};
    int y2[]={90,200,35,200,90};
    g.fillPolygon(x2,y2,5);
    }
    public static void main(String[] args) {
        Try aTry = new Try();
    }
}

相关问题