Zielony Smok - logo witryny

Gwiazda – Star (Java)

Klasy

Star.java
package star;

import java.awt.*;
import java.awt.geom.*;

/**
 * Tworzy obiekty typu Star (gwiazdka). Obiekt tej
 * klasy może być rysowany podobnie jak inne kształty Javy, a więc tylko kontur,
 * tylko wypełnienie albo kontur z wypełnieniem. Gwizdka może mieć dowolny
 * kolor. Kontur może mieć dowolny kolor i być dowolnego typu.
 */
public class Star implements Shape {
    private final GeneralPath path;

	/**
     * Tworzy obiekt typu Star
     *
     * @param x             float - współrzędna x gwiazdki
     * @param y             float - współrzędna y gwiazdki
     * @param outerDiameter float - średnica zewnętrzna gwiazdki
     * @param innerDiameter float - średnica zewnętrzna gwiazdki
     * @param apices        int - liczba promieni
     */
    public Star(float x, float y, float outerDiameter, float innerDiameter,
                int apices) {
        path = new GeneralPath();
		float[] outX = new float[apices];
		float[] outY = new float[apices];
		float[] inX = new float[apices];
		float[] inY = new float[apices];
		float x0 = x + outerDiameter / 2f;
		float y0 = y + outerDiameter / 2f;
		float alfa = 360f / apices;
		float r = 0.5f * outerDiameter;
        for (int i = 0; i < apices; i++) {
            outX[i] = x0 + r * (float) (Math.cos(Math.toRadians(alfa * i)));
            outY[i] = y0 + r * (float) (Math.sin(Math.toRadians(alfa * i)));
            inX[i] = x0
                    + innerDiameter
                    / 2.0f
                    * (float) (Math
                    .cos(Math.toRadians((alfa * i) + 0.5f * alfa)));
            inY[i] = y0
                    + innerDiameter
                    / 2.0f
                    * (float) (Math
                    .sin(Math.toRadians((alfa * i) + 0.5f * alfa)));
        }
        path.moveTo(inX[apices - 1], inY[apices - 1]);
        for (int i = apices - 1; i > 0; i--) {
            path.lineTo(outX[i], outY[i]);
            path.lineTo(inX[i - 1], inY[i - 1]);
        }
        path.lineTo(outX[0], outY[0]);
        path.closePath();
    }

    @Override
    public boolean contains(Point2D p) {
        return path.contains(p);
    }

    @Override
    public boolean contains(Rectangle2D r) {
        return path.contains(r);
    }

    @Override
    public boolean contains(double x, double y) {
        return path.contains(x, y);
    }

    @Override
    public boolean contains(double x, double y, double w, double h) {
        return path.contains(x, y, w, h);
    }

    @Override
    public Rectangle getBounds() {
        return path.getBounds();
    }

    @Override
    public Rectangle2D getBounds2D() {
        return path.getBounds2D();
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at) {
        return path.getPathIterator(at);
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return path.getPathIterator(at, flatness);
    }

    @Override
    public boolean intersects(Rectangle2D r) {
        return path.intersects(r);
    }

    @Override
    public boolean intersects(double x, double y, double w, double h) {
        return path.intersects(x, y, w, h);
    }
}
StarPanel.java
package star;

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

/**
 * zastosowanie klasy Star;
 */
public class StarPanel extends JPanel {
    private static final long serialVersionUID = 8983225865613648722L;

    public StarPanel() {
        setPreferredSize(new Dimension(700, 500));
        setBounds(0, 0, 700, 500);
        //setBackground(Color.BLUE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        Star s = new Star(100f, 100f, 100f, 20f, 5);
        g2.setColor(Color.RED);
        g2.draw(s);
        Star s1 = new Star(300, 100, 100, 30, 6);
        g2.setColor(Color.RED);
        g2.fill(s1);
        Star s2 = new Star(500, 100, 100, 10, 5);
        g2.setStroke(new BasicStroke(5));
        g2.setColor(Color.BLUE);
        g2.draw(s2);
        g2.setColor(Color.RED);
        g2.fill(s2);
        Star s3 = new Star(100, 300, 100, 10, 10);
        g2.setColor(Color.YELLOW);
        g2.fill(s3);
        Star s4 = new Star(300, 300, 80, 5, 30);
        g2.setColor(Color.GREEN);
        g2.fill(s4);
        Star s5 = new Star(500, 300, 60, 50, 40);
        g2.setStroke(new BasicStroke(3));
        g2.setColor(Color.BLUE);
        g2.draw(s5);
        g2.setColor(Color.MAGENTA);
        g2.fill(s5);
    }
}
Main.java
package star;

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

/**
 * ramka + panel
 */
public class Main extends JFrame {
    private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main(new StarPanel()));
    }

    public Main(JComponent comp) {
		JPanel panel = (JPanel) comp;
        panel.setLayout(null);
		int frameHeight = 500;
		int frameWidth = 700;
		panel.setBounds(0, 0, frameWidth, frameHeight);
        //panel.setBounds(0, 0, 256, 256);
        setLayout(null);
        setPreferredSize(new Dimension(700, 500));
        setBounds((Toolkit.getDefaultToolkit().getScreenSize().width / 2)
                        - (frameWidth / 2), (Toolkit.getDefaultToolkit()
                        .getScreenSize().height / 2) - (frameHeight / 2), frameWidth,
				frameHeight);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Ramka Graficzna");
        //  setResizable(false);
        add(panel);
        setVisible(true);
    }
}

Wynik

Po uruchomieniu klasy Main.java otrzymamy obrazek (Rys. 237):

Kształt Gwiazda - Shape Star - Java
Rys. 237. Gwiazdki