5.28.2019

[JAVA] Graphic Programming - Panel

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

public class CirclesDemo extends JFrame {
    public CirclesDemo() {
        setTitle("xxx");

        class MyPanel extends JPanel {
            @Override            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Color[] colors = {
                        Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
                        new Color(0x000080), new Color(0x800080)
                };
                for(int i = 0; i < 7; i++) {
                    int d = delta(i);
                    g.setColor(colors[i]);
                    g.drawOval(10 + d, 10 + d, 240 - 2 * d, 240 - 2 * d); //coordinator x, y, 지름이 양쪽에 있으니 -2*d                }
            }

            int delta(int x) { //square 속 circles 간의 간격                double diagonal = Math.sqrt(240 * 240);
                return (int)(diagonal *  x * 15 / 240); //15 is usual            }
        }

        add(new MyPanel()); //new JPanel로 무명 클래스 만들 수도 있음        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);

    }

    public static void main(String[] args) {
        new CirclesDemo();
    }
}