BLOG main image
My dream is to be a computer programmer.
역시 전에 짰던 소스를 리뷰해보며 다시 짜봤는데, 이소스는 참 맘에 든다. 간결하면서도 잘짠 것 같다. 자바 첨 배울때 짠 소스인데 어떻게짰지??ㅋ 신기하다.;
달팽이관처럼 빙글빙글 도는 사각형 미로와 둥근 미로를 그리는 소스이다.

사용자 삽입 이미지


import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawLineArt extends JPanel {
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
               
                int centerX = getWidth() / 2;
                int centerY = getHeight() / 2;
                int height = 20;
                for(int i=1 ; i<15 ; i++) {
                        g.drawLine(centerX, centerY+height*i, centerX, centerY);
                        g.drawLine(centerX, centerY+height*i, centerX-height*i, centerY+height*i);
                       
                        centerX -= height * i;
                        centerY += height * i;
                        height *= -1;
                }
        }
       
        public static void main(String[] args) {
                JFrame frame = new JFrame();
                frame.add(new DrawLineArt());
                frame.setVisible(true);
                frame.setSize(300, 320);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
 }


import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawArcArt extends JPanel {
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
               
                int centerX = getWidth() / 2; // 중앙 꼭지점 X
                int centerY = getHeight() / 2;// 중앙 꼭지점 Y
                int radius = 10;              // 반지름
                int anc = 0;                  // 시작 각도
                int startX = 0;               // 가상의 사각형을 그리는 시작 X
                int startY = 0;               // 가상의 사각형을 그리는 시작 Y
               
                for(int i=1 ; i<20 ; i++) {
                        anc = (i%2 == 0) ? 180 : 0;
                        startX = centerX - radius * i;
                        startY = centerY - radius * i;
                       
                        g.drawArc(startX, startY, radius*2*i, radius*2*i, anc, 180);
                        centerX = (i%2 == 0) ? centerX + radius : centerX - radius;
                }
        }
       
        public static void main(String[] args) {
                JFrame frame = new JFrame();
                frame.add(new DrawArcArt());
                frame.setVisible(true);
                frame.setSize(300, 320);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
 }



크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by cherrykyun
◀ PREV : [1] : [2] : [3] : [4] : [5] : ... [197] : NEXT ▶

카테고리

분류 전체보기 (197)
My Story (30)
Think (21)
Book (4)
C.S.E (125)
문서 / PPT (2)
좋은 정보 (6)
세상 이야기 (9)

최근에 받은 트랙백

달력

«   2008/05   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31