코드를 원하시면 보여 드리겠습니다. ㄷㄷㄷ
package paint;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Paint extends JFrame implements Serializable, MouseListener,
MouseMotionListener, ItemListener, ActionListener {
private static final long serialVersionUID = 1L;
private JButton pen, circle, square, straight, go, full;// 도구 버튼
private ImageIcon im1, im2, im3, im4, im5, im6; // 도구 이미지
private Container con;// 컨테이너
private MenuBar mb = new MenuBar();
private Menu file = new Menu("파일");
private MenuItem fnew = new MenuItem("새로열기");
private MenuItem fopen = new MenuItem("불러오기");
private MenuItem fsave = new MenuItem("저장하기");
private MenuItem fexit = new MenuItem("종료하기");
private MenuItem fas = new MenuItem("다른 이름으로 저장");
private Menu option = new Menu("옵션");
private Menu oview = new Menu("보기");
private CheckboxMenuItem pv = new CheckboxMenuItem("확대");
private CheckboxMenuItem mv = new CheckboxMenuItem("축소");
private Menu help = new Menu("도움");
private MenuItem about = new MenuItem("만든사람들");
private int xs, ys, xe, ye, dist, cs;// 좌표
private Color c = new Color(cs);// 색깔
private Type type; // 도형 타입
private boolean fill;// 채우기
private Vector vc = new Vector();// 도형 변수
private Canvas can = new Canvas();
private Graphics g; // 그리기를 수행하는 변수
JPanel mt = new JPanel();// 도구 패널
JPanel ct = new JPanel();// 색깔 패널
public Paint(String title) {
super("Painting");
this.init();
this.start();
im1 = new ImageIcon("그림판.png");
this.setIconImage(im1.getImage());
this.setSize(1000, 1000);
setVisible(true);
}
public void init() {
im1 = new ImageIcon("그림판.png");
im2 = new ImageIcon("수정됨_연필.png");
im3 = new ImageIcon("수정됨_CriCle.png");
im4 = new ImageIcon("수정됨_사각형.png");
im5 = new ImageIcon("수정됨_직선.png");
im6 = new ImageIcon("수정됨_채우기.png");
// 메뉴바
this.setMenuBar(mb);
mb.add(file);
file.add(fnew);
file.addSeparator();
file.add(fopen);
file.add(fsave);
file.add(fas);
file.add(fexit);
mb.add(option);
option.add(oview);
oview.add(pv);
oview.add(mv);
option.addSeparator();
mb.add(help);
help.add(about);
// 콘테이너
con = this.getContentPane();
con.setBackground(Color.lightGray);
setLayout(null);
mt();
ct();
canvas();
}
public void canvas() {
can.setSize(900, 900);
can.setBackground(Color.WHITE);
can.setLocation(40, 50);
add(can);
can.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
e.getX();
e.getY();
can.repaint();
}
});
can.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
e.getX();
e.getY();
can.repaint();
}
});
}
public void mt() {
pen = new JButton(im2);
circle = new JButton(im3);
square = new JButton(im4);
straight = new JButton(im5);
full = new JButton(im6);
Dimension di = pen.getPreferredSize();
// 도구 상자 크기
pen.setSize(di);
circle.setSize(di);
square.setSize(di);
straight.setSize(di);
full.setSize(di);
// 도구 상자 위치
pen.setLocation(275, 10);
circle.setLocation(375, 10);
square.setLocation(475, 10);
straight.setLocation(575, 10);
full.setLocation(675, 10);
// 도구상자 추가
con.add("North", pen);
con.add("North", circle);
con.add("North", square);
con.add("North", straight);
con.add("North", full);
// 도구 상자 액션이벤트
pen.addActionListener(this);
circle.addActionListener(this);
square.addActionListener(this);
straight.addActionListener(this);
full.addActionListener(this);
}
public Color ct() {
go = new JButton("Other Color...");
Dimension di2 = go.getPreferredSize();
// 색깔 상자 크기
go.setSize(di2);
// 색깔 상자 위치
go.setLocation(850, 10);
// 색깔 상자
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c = JColorChooser.showDialog(
((Component) e.getSource()).getParent(),
"Color Spectrum", c);
if (c == null)
c = Color.black;
}
});
// 색깔 상자 추가
con.add("East", go);
return c;
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// x를 누르면 종료 프로세서
// 상에서 종료
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void drawpint(Graphics g) {
int startX = xs < xe ? xs : xe;
int startY = ys < ye ? ys : ye;
int endX = xs < xe ? xe - xs : xs - xe;
int endY = ys < ye ? ye - ys : ys - ye;
g.setColor(c);
if (full.isSelected()) {
if (pen.isSelected())
g.drawLine(xs, ys, xe, ye);
else if (circle.isSelected())
g.fillOval(startX, startY, endX, endY);
else if (square.isSelected())
g.fillRect(startX, startY, endX, endY);
else if (straight.isSelected())
g.drawLine(xs, ys, xe, ye);
} else {
if (pen.isSelected())
g.drawLine(xs, ys, xe, ye);
else if (circle.isSelected())
g.drawOval(startX, startY, endX, endY);
else if (square.isSelected())
g.drawRect(startX, startY, endX, endY);
else if (straight.isSelected())
g.drawLine(xs, ys, xe, ye);
}
for (int i = 0; i < vc.size(); i++) {
DrawInfo imsi = (DrawInfo) vc.elementAt(i);
g.setColor(imsi.getCOLOR());
if (imsi.getJButton() == pen || imsi.getJButton() == straight) {
g.drawLine(imsi.getXStart(), imsi.getYStart(), imsi.getXEnd(),
imsi.getYEnd());
} else if (imsi.getJButton() == square) {
if (imsi.getFILL()) {
g.fillRect(imsi.getXStart(), imsi.getYStart(),
imsi.getXEnd() - imsi.getXStart(), imsi.getYEnd()
- imsi.getYStart());
} else {
g.drawRect(imsi.getXStart(), imsi.getYStart(),
imsi.getXEnd() - imsi.getXStart(), imsi.getYEnd()
- imsi.getYStart());
}
} else if (imsi.getJButton() == circle) {
if (imsi.getFILL()) {
g.fillOval(imsi.getXStart(), imsi.getYStart(),
imsi.getXEnd() - imsi.getXStart(), imsi.getYEnd()
- imsi.getYStart());
} else {
g.drawOval(imsi.getXStart(), imsi.getYStart(),
imsi.getXEnd() - imsi.getXStart(), imsi.getYEnd()
- imsi.getYStart());
}
}
}
}
@Override
// action override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
vc.clear();
xs = 0;
ys = 0;
xe = 0;
ye = 0;
this.repaint();
if (e.getSource() == fopen) {
FileDialog fdlg = new FileDialog(this, "열기", FileDialog.LOAD);
fdlg.setVisible(true);
String dir = fdlg.getDirectory();
String file = fdlg.getFile();
if (dir == null || file == null)
return;
try {
ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(new File(
dir, file))));
vc = (Vector) ois.readObject();
ois.close();
} catch (IOException ee) {
} catch (ClassNotFoundException ee) {
}
} else if (e.getSource() == fsave) {
// 저장
FileDialog fdlg = new FileDialog(this, "저장", FileDialog.SAVE);
fdlg.setVisible(true);
String dir = fdlg.getDirectory();
String file = fdlg.getFile();
if (dir == null || file == null)
return;
try {
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(
dir, file))));
oos.writeObject(vc);
oos.close();
} catch (IOException ee) {
}
} else if (e.getSource() == fexit) {
System.exit(0);
}
}
@Override
public void itemStateChanged(ItemEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
xe = e.getX();
ye = e.getY();
ct();
DrawInfo di = new DrawInfo(xs, ys, xe, ye, pen, c);
vc.add(di);
xs = xe;
ys = ye;
can.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
xs = e.getX();
ys = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
xe = e.getX();
ye = e.getY();
ct();
DrawInfo di = new DrawInfo(xs, ys, xe, ye, pen, c);
vc.add(di);
this.repaint();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Paint pg = new Paint("Painting");
}
}