public class LinePanel extends JPanel{
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.red);
// 가로선 (x 시작점, y 시작점, x끝점, y끝점)
g.drawLine(100, 100, 200, 100);
}
}
class MyFrame extends JFrame {
LinePanel lp;
public MyFrame() {
lp = new LinePanel();
add(lp);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class LineTest {
public static void main(String[] args) {
new MyFrame();
}
}
drawLine를 통해서 좌표를 설정하여 선을 그릴 수 있다
public class LinePanel extends JPanel{
int x1, y1, x2, y2;
public LinePanel() {
this.x1 = 100;
this.y1 = 100;
this.x2 = 300;
this.y2 = 100;
addMouseListener(new MouseListener() {
// 마우스를 뗄떄
@Override
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
repaint(); // 다시 그리기
}
// 마우스 누를떄
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
// 자동으로 동작
// 그려야할 상황이 있을 경우
@Override
protected void paintComponent(Graphics g) {
// 가로선 (x 시작점, y 시작점, x끝점, y끝점)
g.drawLine(x1, y1, x2, y2);
}
}
class MyFrame extends JFrame {
LinePanel lp;
public MyFrame() {
lp = new LinePanel();
add(lp);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class LineTest {
public static void main(String[] args) {
new MyFrame();
}
}
public class RectPanel extends JPanel{
int x1, y1, x2, y2;
public RectPanel() {
addMouseListener( new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2 = e.getX();
y2 = e.getY();
repaint();
//다시 그려주세요!
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1 = e.getX();
y1 = e.getY();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void paintComponent(Graphics g) {
int width = x2 - x1;
int height = y2 - y1;
int x = x1, y = y1;
if (x1 > x2) {
x = x2;
width = x1 - x2;
}
if (y1 > y2) {
y = y2;
height = y1 - y2;
}
g.drawRect(x, y, width, height);
}
}
class MyFrame extends JFrame {
RectPanel lp;
public MyFrame() {
lp = new RectPanel();
add(lp);
setSize(600, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class RectTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
public class OvalPanel extends JPanel{
int x1, y1, x2, y2;
public OvalPanel() {
addMouseListener( new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2 = e.getX();
y2 = e.getY();
repaint();
//다시 그려주세요!
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1 = e.getX();
y1 = e.getY();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void paintComponent(Graphics g) {
int width = x2 - x1, height = y2 - y1;
int x = x1, y = y1;
if (x1 > x2) {
x = x2;
width = x1 - x2;
}
if (y1 > y2) {
y = y2;
height = y1 - y2;
}
// (시작x , 시작y, 가로, 높이)
g.drawOval(x, y, width, height);
}
}
class MyFrame extends JFrame {
OvalPanel lp;
public MyFrame() {
lp = new OvalPanel();
add(lp);
setSize(600, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class OvalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
public class GraphicInfo implements Serializable {
private int x1, y1, x2, y2, drawType;
private Color drawColor;
public GraphicInfo(int x1, int y1, int x2, int y2, int drawType, Color drawColor) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.drawType = drawType;
this.drawColor = drawColor;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getDrawType() {
return drawType;
}
public void setDrawType(int drawType) {
this.drawType = drawType;
}
public Color getDrawColor() {
return drawColor;
}
public void setDrawColor(Color drawColor) {
this.drawColor = drawColor;
}
}
public class GrimpanPanel extends JPanel{
int x1, y1, x2, y2;
int drawType; // 선 : 0, 원 : 1, 사각혐 : 2
Color drawColor = Color.black;
ArrayList<GraphicInfo> list;
boolean isNew = false;
public GrimpanPanel() {
list = new ArrayList<GraphicInfo>();
addMouseListener( new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
list.add(new GraphicInfo(x1, y1, x2, y2, drawType, drawColor));
isNew = true;
repaint();
//다시 그려주세요!
}
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x1, y1, x2, y2, drawType;
for (GraphicInfo info : list) {
x1 = info.getX1();
y1 = info.getY1();
x2 = info.getX2();
y2 = info.getY2();
drawType = info.getDrawType();
int width = x2 - x1;
int height = y2 - y1;
int x = x1;
int y = y1;
if (x1 > x2) {
x = x2;
width = x1 - x2;
}
if (y1 > y2) {
y = y2;
height = y1 - y2;
}
g.setColor(info.getDrawColor());
switch (drawType) {
case 0: g.drawLine(x1, y1, x2, y2); break;
case 1: g.drawOval(x, y, width, height); break;
case 2: g.drawRect(x, y, width, height); break;
}
}
}
}
class MyFrame extends JFrame {
GrimpanPanel lp;
JFileChooser jfc;
// 열어온 파일이나 저장한 파일을 담기 위한 변수
File file;
public void saveFile() {
int re = 0;
// 저장 다이얼로그 불러오기
// 저장 : 0, 취소 : 1 리턴
// 파일이 열려있지 않으면 어떤 파일이름으로 저장할 것인지 다이얼로그 띄움
if (file == null ) {
re = jfc.showSaveDialog(null);
}
if (re == 0) {
// 저장할 선택한 파일 정보 가져오기
file = jfc.getSelectedFile();
// 객체단위로 출력하기 위하여 objectoutputstream를 이용
// java.io이기 떄문에 에외처리 해야한다
try {
ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(lp.list);
setTitle(file.getName());
lp.isNew = false;
// 출력 후 닫아야한다
oos.close();
} catch (Exception error) {
System.out.println("예외발생 : " + error.getMessage());
}
}
}
public MyFrame() {
jfc = new JFileChooser("C:\\data");
// 메뉴바 생성
JMenuBar jmb = new JMenuBar();
// 파일 메뉴 생성
JMenu mn_file = new JMenu("파일");
// 파일 부메뉴 생성
JMenuItem jmt_new = new JMenuItem("새 파일");
JMenuItem jmt_open = new JMenuItem("열기");
JMenuItem jmt_save = new JMenuItem("저장");
mn_file.add(jmt_new);
mn_file.add(jmt_open);
mn_file.add(jmt_save);
// 그리기 도구 메뉴 생성
JMenu mn_draw = new JMenu("그리기 도구");
// 그리기 도구 부메뉴 생성
JMenuItem jmt_line = new JMenuItem("선");
JMenuItem jmt_circle = new JMenuItem("원");
JMenuItem jmt_rect = new JMenuItem("사각형");
mn_draw.add(jmt_line);
mn_draw.add(jmt_circle);
mn_draw.add(jmt_rect);
// 색상 메뉴 생성
JMenu mn_color = new JMenu("그리기 색상");
// 색상 부메뉴 생성
JMenuItem jmt_red = new JMenuItem("빨강");
JMenuItem jmt_blue = new JMenuItem("파랑");
JMenuItem jmt_green = new JMenuItem("초록");
JMenuItem jmt_black = new JMenuItem("검정");
mn_color.add(jmt_red);
mn_color.add(jmt_blue);
mn_color.add(jmt_green);
mn_color.add(jmt_black);
// 메누바에 메뉴 추가
jmb.add(mn_file);
jmb.add(mn_draw);
jmb.add(mn_color);
// 프레임에 메뉴바 넣기
setJMenuBar(jmb);
// 파일 메뉴 이벤트 설정
jmt_new.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (lp.isNew) {
int re = JOptionPane.showConfirmDialog(null, "저장하시겠습니까?");
if (re == 0) {
saveFile();
// 최소를 누르거나 창을 닫거나
} else if (re == 2 || re == -1) return;
}
// 새파일이 되었으므로 초기화
lp.isNew = false;
// 그래픽이 그려진 리스트를 비운다
lp.list.clear();
// 다시 그린다
lp.repaint();
// 열어둔 파일 초기화
file = null;
}
});
// 파일 열기
jmt_open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 변경된 내용이 있으면 저장할 것인지 물어보고 파일 열기를 한다
if (lp.isNew) {
// 예 : 0, 아니오 : 1 , 취소 : 2, 닫기 : -1
// 취소 닫기는 열기 취소
int re = JOptionPane.showConfirmDialog(null, "변경된 내용 저장하시겠습니까?");
if (re == 0) {
saveFile();
} else if (re == 2 || re == -1) {
return;
}
re = jfc.showOpenDialog(null);
// 다이얼로그에 열기 버트 눌면
if (re == 0) {
// 다이얼로그에서 선택한 파일 객체를 갖고돈다
file = jfc.getSelectedFile();
try {
// 객체단위로 파일을 읽어 들이기 위한
//ObjectInputStream객체를 생성한다.
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(file));
// 파일로 부터 객체를 읽어 들여
// 패널의 리스트에 저장한다.
lp.list = (ArrayList<GraphicInfo>)ois.readObject();
// 패널을 다시 그려줄것을 요청한다.
lp.repaint();
lp.isNew = false;
// 사용한 파일을 닫아준다
ois.close();
} catch (Exception error) {
System.out.println("예외발생:"+ error.getMessage());
}
}
}
}
});
// 파일 저장
jmt_save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
// drawType 정하기
// 선 모양
jmt_line.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawType = 0;
}
});
// 원 모양
jmt_circle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawType = 1;
}
});
// 사각형
jmt_rect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawType = 2;
}
});
// colorType 정하기
jmt_red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawColor = Color.red;
}
});
jmt_blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawColor = Color.blue;
}
});
jmt_green.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawColor = Color.green;
}
});
jmt_black.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lp.drawColor = Color.black;
}
});
// 그림판 판넬 생성
lp = new GrimpanPanel();
add(lp);
setSize(600, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class GrimpanTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
객체를 파일로 내보내기 하려면 직렬화 즉 순서를 보장해줘야 한다
우리가 파일로 기록하고자 하는 것은 list이다
list에 담긴 자료형은 GraphicInfo 인데
내가 만든 자료형(클래스)를 파일로 기록하려면 기록하는 순서를 정햐여 한다
이를 직렬화라고 한다
직렬화 하는 방법
class 클래스이름 implemnets Serializable
ObjectInputStream