728x90
package object.Extends;
import javax.swing.JFrame;
import java.awt.Graphics;
//--------------------------------------------------------------------------
// public class DrawShapeExam
//--------------------------------------------------------------------------
public class DrawShapeExam extends JFrame{
public static void main(String[] args) {
DrawShapeExam win = new DrawShapeExam("도형 그리기");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
Point[] p = {
new Point(100, 100),
new Point(140, 50),
new Point(200, 100)
};
Triangle t = new Triangle(p);
Circle c = new Circle(new Point(150, 150), 50);
// 원을 그린다.
g.drawOval(c.center.x, c.center.y, c.r, c.r);
// 직선 3개로 삼각형을 그린다.
g.drawLine(t.p[0].x, t.p[0].y, t.p[1].x, t.p[1].y);
g.drawLine(t.p[1].x, t.p[1].y, t.p[2].x, t.p[2].y);
g.drawLine(t.p[2].x, t.p[2].y, t.p[0].x, t.p[0].y);
}
DrawShapeExam(String title) {
super(title);
setLocation(300, 200);
setSize(500, 400);
setVisible(true);
}
} // End - public class DrawShapeExam
//--------------------------------------------------------------------------
// 좌표 클래스
//--------------------------------------------------------------------------
class Point {
int x;
int y;
Point(int x, int y) { // 매개변수 있는 생성자
this.x = x;
this.y = y;
}
Point() { // 기본 생성자
this(0, 0);
}
} // End - class Point
//--------------------------------------------------------------------------
// 원 클래스
//--------------------------------------------------------------------------
class Circle {
Point center; // 원의 원점좌표
int r; // 반지름
Circle() {
this(new Point(0, 0), 100);
}
Circle(Point center, int r) {
this.center = center;
this.r = r;
}
} // End - class Circle
//--------------------------------------------------------------------------
// 삼각형 클래스
//--------------------------------------------------------------------------
class Triangle {
Point[] p = new Point[3];
Triangle(Point[] p) {
this.p = p;
}
Triangle(Point p1, Point p2, Point p3) {
p[0] = p1;
p[1] = p2;
p[2] = p3;
}
} // End - class Triangle
'Language > Java' 카테고리의 다른 글
[Java] compareTo 메소드 (0) | 2022.08.09 |
---|---|
[Java] 실습 CardDeckExam (0) | 2022.08.09 |
[Java] Scanner 클래스의 메서드 (0) | 2022.08.09 |
[Java] 실습 CaptionTvExam (0) | 2022.08.08 |
[Java] 실습 PhoneBookVer03 (0) | 2022.08.08 |