java-창 프로그램 -circle

11778 단어 gui
import com.sun.awt.AWTUtilities;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Circle {

    static int DIV_NUM = 0;

    public static void main(String[] args) {
        int diam = 800;
        if (args != null && args.length >= 1) {
            try {
                diam = Integer.parseInt(args[0]);
            } catch (Exception e) {

            }
        }
        int divNum = 16;
        if (args != null && args.length >= 2) {
            try {
                divNum = Integer.parseInt(args[1]);
            } catch (Exception e) {
            }
        }
        if (divNum % 2 != 0) {
            return;
        }
        DIV_NUM = divNum;
        JFrame jf = new JFrame();
        RoundRectPanel jp = new RoundRectPanel(jf);
        jp.setLayout(null);
        jp.setOpaque(false);//Panel     
        jp.setBorder(new RoundRectBorder(Color.WHITE));

        jf.add(jp);
        jf.setUndecorated(true);//     
        jf.setBounds(300, 200, diam, diam);
        AWTUtilities.setWindowOpaque(jf, false);//JFrame     
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        ReSizeEvent dg = new ReSizeEvent(jf);
        /**       **/
        jf.addMouseListener(dg);
        jf.addMouseMotionListener(dg);
        jf.setAlwaysOnTop(true);
    }
}

class RoundRectPanel extends JPanel {

    private JFrame jf;

    public RoundRectPanel(JFrame jf) {
        super();
        this.jf = jf;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);
        BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] {10, 10}, 0);
        g2d.setStroke(stroke);
        int diam = jf.getWidth();
        int div = Circle.DIV_NUM / 2;
        int length = diam / Circle.DIV_NUM;
        int radius = diam / 2;

        //  
        for (int i = 1; i < div; i++) {
            int divLength = cal(radius, (div - i) * length);

            int[] topLeft = new int[] {radius - divLength, length * i};
            int[] topRight = new int[] {radius + divLength, length * i};
            int[] bottomLeft = new int[] {radius - divLength, length * (div * 2 - i)};
            int[] bottomRight = new int[] {radius + divLength, length * (div * 2 - i)};

            g2d.drawLine(topLeft[0], topLeft[1], topRight[0], topRight[1]);
            g2d.drawLine(topLeft[0], topLeft[1], bottomLeft[0], bottomLeft[1]);

            g2d.drawLine(bottomLeft[0], bottomLeft[1], bottomRight[0], bottomRight[1]);
            g2d.drawLine(bottomRight[0], bottomRight[1], topRight[0], topRight[1]);
        }
        g2d.drawLine(0, length * div, radius * 2, length * div);

        //  
        for (int i = 1; i < div; i++) {
            int divLength = cal(radius, (div - i) * length);

            int[] leftTop = new int[] {length * i, radius - divLength};
            int[] leftBottom = new int[] {length * i, radius + divLength};
            int[] rightTop = new int[] {length * (div * 2 - i), radius - divLength};
            int[] rightBottom = new int[] {length * (div * 2 - i), radius + divLength};

            g2d.drawLine(leftTop[0], leftTop[1], leftBottom[0], leftBottom[1]);
            g2d.drawLine(leftTop[0], leftTop[1], rightTop[0], rightTop[1]);

            g2d.drawLine(rightTop[0], rightTop[1], rightBottom[0], rightBottom[1]);
            g2d.drawLine(rightBottom[0], rightBottom[1], leftBottom[0], leftBottom[1]);
        }
        g2d.drawLine(length * div, 0, length * div, radius * 2);

        //  
        double magic = 1.4142;
        int xieXian = new Double(magic * diam).intValue();
        int divLength = new Double(length / magic).intValue();
        for (int i = 1; i < div; i++) {
            int a = radius + i * divLength;
            int b = radius - i * divLength;
            int c = new Double(cal(radius, i * length) / magic).intValue();

            g2d.drawLine(a - c, b - c, a + c, b + c);

            a = radius - i * divLength;
            b = radius + i * divLength;
            g2d.drawLine(a - c, b - c, a + c, b + c);

            a = radius - i * divLength;
            b = radius - i * divLength;
            g2d.drawLine(a + c, b - c, a - c, b + c);

            a = radius + i * divLength;
            b = radius + i * divLength;
            g2d.drawLine(a + c, b - c, a - c, b + c);
        }

        int ss = new Double(((xieXian - diam) / 2) / magic).intValue();
        int ss2 = new Double(((xieXian - diam) / 2 + diam) / magic).intValue();
        g2d.drawLine(ss, ss, ss2, ss2);
        g2d.drawLine(ss, ss2, ss2, ss);
    }

    private static int cal(int radius, int length) {
        return new Double(Math.sqrt((Double.parseDouble(String.valueOf(radius * radius - length * length))))).intValue();
    }
}

class RoundRectBorder implements Border {
    Color color;

    public RoundRectBorder() {
        super();
        this.color = Color.RED;
    }

    public RoundRectBorder(Color color) {
        this.color = color;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(color);
        ((Graphics2D) g).setStroke(new BasicStroke(1.0f));
        g.drawRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, c.getWidth(), c.getHeight());
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(0, 0, 0, 0);
    }

    @Override
    public boolean isBorderOpaque() {
        return false;
    }
}

class ReSizeEvent extends MouseAdapter {
    private JFrame jf;
    private Point  prePos, curPos, jfPos;
    private static final double BREADTH          = 15.0;//      
    private              int    dragType;
    private static final int    DRAG_MOVE        = 1;
    private static final int    DRAG_UP          = 2;
    private static final int    DRAG_UPLEFT      = 3;
    private static final int    DRAG_UPRIGHT     = 4;
    private static final int    DRAG_LEFT        = 5;
    private static final int    DRAG_RIGHT       = 6;
    private static final int    DRAG_BOTTOM      = 7;
    private static final int    DRAG_BOTTOMLEFT  = 8;
    private static final int    DRAG_BOTTOMRIGHT = 9;

    public ReSizeEvent(JFrame jf) {
        this.jf = jf;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        prePos = e.getLocationOnScreen();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        areaCheck(e.getPoint());
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        curPos = e.getLocationOnScreen();
        jfPos = jf.getLocation();
        dragAction();
        prePos = curPos;
    }

    private void dragAction() {
        switch (dragType) {
            case DRAG_MOVE:
                jf.setLocation(jfPos.x + curPos.x - prePos.x,
                        jfPos.y + curPos.y - prePos.y);
                break;
            case DRAG_UP://x    ,y    ,  Height  
                jf.setLocation(jfPos.x,
                        jfPos.y + curPos.y - prePos.y);
                int diam = jf.getHeight() - (curPos.y - prePos.y);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_LEFT://y    ,x    ,width  
                jf.setLocation(jfPos.x + curPos.x - prePos.x,
                        jfPos.y);
                diam = jf.getWidth() - (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_RIGHT://x,y    ,width  
                jf.setLocation(jfPos.x,
                        jfPos.y);
                diam = jf.getWidth() + (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_BOTTOM://x,y    ,Height  
                jf.setLocation(jfPos.x,
                        jfPos.y);
                diam = jf.getHeight() + (curPos.y - prePos.y);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_UPLEFT://x,y     ,h,w   
                jf.setLocation(jfPos.x + curPos.x - prePos.x,
                        jfPos.y + curPos.y - prePos.y);
                diam = jf.getWidth() - (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_BOTTOMRIGHT://x,y     ,h,w  
                jf.setLocation(jfPos.x,
                        jfPos.y);
                diam = jf.getWidth() + (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_UPRIGHT://x    ,y,w,h  
                jf.setLocation(jfPos.x,
                        jfPos.y + curPos.y - prePos.y);
                diam = jf.getWidth() + (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            case DRAG_BOTTOMLEFT://y  ,xwh  
                jf.setLocation(jfPos.x + curPos.x - prePos.x,
                        jfPos.y);
                diam = jf.getWidth() - (curPos.x - prePos.x);
                jf.setSize(diam, diam);
                jf.repaint();
                break;
            default:
                break;
        }
    }

    private boolean areaCheck(Point p) {
        if (p.getX() <= BREADTH && p.getY() <= BREADTH) {
            dragType = DRAG_UPLEFT;
            jf.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
        } else if (p.getX() > BREADTH
                && p.getX() < (jf.getWidth() - BREADTH)
                && p.getY() <= BREADTH) {
            dragType = DRAG_UP;
            jf.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
        } else if (p.getX() >= (jf.getWidth() - BREADTH) && p.getY() <= BREADTH) {
            dragType = DRAG_UPRIGHT;
            jf.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
        } else if (p.getX() <= BREADTH
                && p.getY() < (jf.getHeight() - BREADTH)
                && p.getY() > BREADTH) {
            dragType = DRAG_LEFT;
            jf.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
        } else if (p.getX() >= (jf.getWidth() - BREADTH)
                && p.getY() < (jf.getHeight() - BREADTH)
                && p.getY() > BREADTH) {
            dragType = DRAG_RIGHT;
            jf.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
        } else if (p.getX() <= BREADTH
                && p.getY() >= (jf.getHeight() - BREADTH)) {
            dragType = DRAG_BOTTOMLEFT;
            jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
        } else if (p.getX() > BREADTH
                && p.getX() < (jf.getWidth() - BREADTH)
                && p.getY() >= (jf.getHeight() - BREADTH)) {
            dragType = DRAG_BOTTOM;
            jf.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
        } else if (p.getX() >= (jf.getWidth() - BREADTH)
                && p.getY() >= (jf.getHeight() - BREADTH)) {
            dragType = DRAG_BOTTOMRIGHT;
            jf.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
        } else {
            dragType = DRAG_MOVE;
            jf.setCursor(new Cursor(Cursor.MOVE_CURSOR));
            return false;
        }
        return true;
    }
}

좋은 웹페이지 즐겨찾기