No older revisions available
No older revisions available
클래스정의 및 인스턴스(객체) 생성 (예제1 + 예제2) ¶
~cpp
import javax.swing.JOptionPane;
public class HelloWorld {
private String name;
public HelloWorld() {
name = "noname";
}
public void setName(String n)
{
name = n;
}
public void sayHello() {
JOptionPane.showMessageDialog(null, "Hello, World!" + " " + name);
}
public static void main(String[] args) {
HelloWorld hello1 = new HelloWorld();
hello1.setName("세환");
hello1.sayHello();
HelloWorld hello2;
hello2 = hello1;
hello2.sayHello();
}
}
변수 및 메소드의 접근제어 (예제1 + 예제2) ¶
Point.java ¶
~cpp
public class Point {
private int x, y;
public Point() {
}
public void setX(int xValue) { x = xValue; }
public void setY(int yValue) { y = yValue; }
public int getX() { return(x); }
public int getY() { return(y); }
public void move(int xValue, int yValue) {
x += xValue;
y += yValue;
}
}
Circle.java ¶
~cpp
import javax.swing.*;
class Circle {
private Point middlePoint = new Point();
private int width;
private int height;
private String info = "";
public Circle() {
}
public void setData(int xValue, int yValue, int width, int height) {
middlePoint.setX(xValue);
middlePoint.setY(yValue);
this.width = width;
this.height = height;
}
public void setInfo() {
info += "Circle x : " + middlePoint.getX() + "\n" +
"Circle y : " + middlePoint.getY() + "\n" +
"Circle width : " + width + "\n" +
"Circle height : " + height + "\n";
}
public void draw() {
setInfo();
JOptionPane.showMessageDialog(null, info);
}
public void move(int xValue, int yValue) {
middlePoint.move(xValue, yValue);
}
public void change(int xValue, int yValue, int width, int height) {
move(xValue, yValue);
this.width = width;
this.height = height;
}
}
Line.java ¶
~cpp
import javax.swing.JOptionPane;
public class Line {
private Point left_top = new Point();
private Point right_bottom = new Point();
private String info = "";
public Line() {
}
public void setData(int p1_x, int p1_y, int p2_x, int p2_y) {
left_top.setX(p1_x);
left_top.setY(p1_y);
right_bottom.setX(p2_x);
right_bottom.setY(p2_y);
}
public void setInfo() {
info += "직선" + "\n" +
"시작점의 x 좌표 : " + left_top.getX() + "\n" +
"시작점의 y 좌표 : " + left_top.getY() + "\n" +
"끝점의 x 좌표 : " + right_bottom.getX() + "\n" +
"끝점의 y 좌표 : " + right_bottom.getY() + "\n";
}
public void draw() {
setInfo();
JOptionPane.showMessageDialog(null, info);
}
public void move(int xValue, int yValue) {
left_top.move(xValue, yValue);
right_bottom.move(xValue, yValue);
}
}
Rectangle.java ¶
~cpp
import javax.swing.JOptionPane;
public class Rectangle {
private Point left_top = new Point();
private Point right_bottom = new Point();
private double area;
private String info = "";
public Rectangle() {
}
public void setData(int p1_x, int p1_y, int p2_x, int p2_y) {
left_top.setX(p1_x);
left_top.setY(p1_y);
right_bottom.setX(p2_x);
right_bottom.setY(p2_y);
setArea();
}
public void setArea() {
area = Math.abs(right_bottom.getX() - left_top.getX()) * Math.abs(left_top.getY() - right_bottom.getY());
}
public void setInfo() {
info += "사각형" + "\n" +
"left 좌표 : " + left_top.getX() + "\n" +
"top 좌표 : " + left_top.getY() + "\n" +
"right 좌표 : " + right_bottom.getX() + "\n" +
"bottom 좌표 : " + right_bottom.getY() + "\n" +
"area : " + area;
}
public void draw() {
setInfo();
JOptionPane.showMessageDialog(null, info);
}
public void move(int xValue, int yValue) {
left_top.move(xValue, yValue);
right_bottom.move(xValue, yValue);
}
}
MyPictureFrame.java ¶
~cpp
class MyPictureFrame {
private Circle circle = new Circle();
private Line line = new Line();
private Rectangle rect = new Rectangle();
public MyPictureFrame() {
circle.setData(100,100,50,50);
circle.draw();
line.setData(12, 34, 56, 78);
line.move(100, 100);
line.draw();
rect.setData(100, 200, 300, 400);
rect.move(50, 50);
rect.draw();
}
public static void main(String[] args) {
MyPictureFrame frame = new MyPictureFrame();
}
}