Search Results for '2008/05/17'

2 POSTS

  1. 2008/05/17 [Java How To Programming] DrawLineArt , DrawArcArt
역시 전에 짰던 소스를 리뷰해보며 다시 짜봤는데, 이소스는 참 맘에 든다. 간결하면서도 잘짠 것 같다. 자바 첨 배울때 짠 소스인데 어떻게짰지??ㅋ 신기하다.;
달팽이관처럼 빙글빙글 도는 사각형 미로와 둥근 미로를 그리는 소스이다.

사용자 삽입 이미지


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);
        }
 }