Layman
Layman

Reputation: 89

How do I draw a star in Java?

I want to draw a star off of some points. My issue is that it is not showing the lines for my little star. What Am I missing here? I am making each point, making the lines, setting the color, and it just does not show my star. It DOES show the frame but i was thinking the issue was not the frame but the actual bulk of the code. What do you guys suggest trying?

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;
    }
}

Upvotes: 1

Views: 4265

Answers (2)

Fatimah
Fatimah

Reputation: 1

    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();
    }
}

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39437

You should find a better place to do all this than in paintIcon.
This method is not called at all in your case.

Here is your code fixed. Seems you're just missing 1-2 lines
from the star (this issue you should be able to debug yourself).

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);
    }

}

Upvotes: 1

Related Questions