How to make star shape in Java?

I'm trying to make some shapes with java. I created two rectangles with two different colors but I want to create a star shape and I can't find useful source to help me doing this.

Here is my code:

import java.awt.*;
import javax.swing.*;

public class shapes extends JPanel{
public void paintComponent(Graphics GPHCS){
super.paintComponent(GPHCS);
this.setBackground(Color.WHITE);

GPHCS.setColor(Color.BLUE);
GPHCS.fillRect(25,25,100,30);

GPHCS.setColor(Color.GRAY);
GPHCS.fillRect(25,65,100,30);

GPHCS.setColor(new Color(190,81,215));
GPHCS.drawString("This is my text", 25, 120);
}
}

Please help me to make a star shape.

Thank you..

Answers


You could try using a polygon and some basic math:

    int midX = 500;
    int midY = 340;
    int radius[] = {118,40,90,40};
    int nPoints = 16;
    int[] X = new int[nPoints];
    int[] Y = new int[nPoints];

    for (double current=0.0; current<nPoints; current++)
    {
        int i = (int) current;
        double x = Math.cos(current*((2*Math.PI)/max))*radius[i % 4];
        double y = Math.sin(current*((2*Math.PI)/max))*radius[i % 4];

        X[i] = (int) x+midX;
        Y[i] = (int) y+midY;
    }

    g.setColor(Color.WHITE);
    g.fillPolygon(X, Y, nPoints);

You can also use existing classes e.g. http://java-sl.com/shapes.html for regular polygons and stars.


Need Your Help

How to pass values from one activity to another using tap on List Item

android listview android-listview adapter baseadapter

I am trying to pass ciicked listitem data to another activity, but whenever i do tap on any of the list item neither able to get Toast nor able to pass selected list item value to another activity....

Wordpress - Use featured image as div background within shortcode in PHP

php css wordpress shortcode

I'm trying to get the featured image of a post in wordpress and output it as the background image of a div and align it to the bottom right of the div.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.