Создать вращающуюся вокруг своей оси семиконечную звезду - Java

Узнай цену своей работы

Формулировка задачи:

?????

Решение задачи: «Создать вращающуюся вокруг своей оси семиконечную звезду»

textual
Листинг программы
package ru.skipy.tests;
 
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
 
/**
 * RotatingStar
 *
 * @author Eugene Matyushkin aka Skipy
 * @since 04.12.13
 */
public class RotatingStar extends JFrame {
 
    DrawPanel dp;
 
    public RotatingStar() {
        super("Rotating star");
        JPanel cp = new JPanel(new BorderLayout());
        cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setContentPane(cp);
        dp = new DrawPanel(200, 100, Math.PI / 180);
        cp.add(dp, BorderLayout.CENTER);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    dp.rotate();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.repaint();
                        }
                    });
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ex) {
                        // nothing to do
                    }
                }
            }
        }).start();
    }
 
    public static void main(String[] args) {
        new RotatingStar().setVisible(true);
    }
 
 
    static class DrawPanel extends JPanel {
 
        private static final int BEAMS = 7;
        private double radiusE;
        private double radiusI;
        private double angle = 0;
        private double da;
 
        public DrawPanel(double radiusE, double radiusI, double da) {
            this.radiusE = radiusE;
            this.radiusI = radiusI;
            this.da = da;
            setBackground(Color.white);
        }
 
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(Color.red);
            g2d.setStroke(new BasicStroke(3));
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            double cx = getWidth() / 2;
            double cy = getHeight() / 2;
            for (int i = 0; i < BEAMS; i++) {
                double ea = angle + 2 * Math.PI / BEAMS * i;
                double ey = cy - radiusE * Math.sin(ea);
                Line2D l = new Line2D.Double(cx, cy, cx, ey);
                g2d.draw(l);
            }
        }
 
        public void rotate() {
            angle += da;
        }
    }
}

Оцени полезность:

7   голосов , оценка 3.714 из 5
Похожие ответы