오래전에 연습용으로 만들었던 거라 허접합니다.. ㅎㅎ
참고하실 분들만 참고하세요..
import java.awt.*;
import java.awt.event.*;
/**
작성일: 2007. 11. 6
작성자: JK
기 능: 색깔 세개짜리 어린이 그림판..
*/
public class Painter extends Frame
{
Panel pn, pc;
Button bto, btr, btb, btp, btm, btw, btc;
MyCanvas can;
Color gc=Color.black; //ActionEvent에 사용할 멤버 변수들
int x, y, h=2, v=2;
public Painter()
{
super("Painter");
addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); //종료 시키는 WindowAdapter 생성
setBackground(new Color(50, 50, 50));
pn=new Panel();
pc=new Panel();
add(pn, "North");
add(pc, "Center");
pn.setLayout(new FlowLayout());
pn.setBackground(Color.black);
pc.setLayout(new FlowLayout());
pn.add(bto=new Button("Orange"));
bto.setForeground(Color.white);
bto.setBackground(Color.orange);
pn.add(btr=new Button("Red"));
btr.setForeground(Color.white);
btr.setBackground(Color.red);
pn.add(btb=new Button("Black"));
btb.setForeground(Color.white);
btb.setBackground(Color.black);
pn.add(btp=new Button("크게"));
btp.setForeground(Color.white);
btp.setBackground(new Color(50, 50, 50));
pn.add(btm=new Button("작게"));
btm.setForeground(Color.white);
btm.setBackground(new Color(50, 50, 50));
pn.add(btw=new Button("지우개"));
btw.setForeground(Color.white);
btw.setBackground(Color.darkGray);
pn.add(btc=new Button("리셋"));
btc.setForeground(Color.white);
btc.setBackground(new Color(50, 50, 50));
can=new MyCanvas();
can.setSize(370, 300);
pc.add(can);
can.setBackground(Color.white);
MyHandler mh=new MyHandler();
can.addMouseMotionListener(mh);
bto.addActionListener(mh);
btr.addActionListener(mh);
btb.addActionListener(mh);
btp.addActionListener(mh);
btm.addActionListener(mh);
btw.addActionListener(mh);
btc.addActionListener(mh);
}
class MyHandler extends MouseMotionAdapter implements ActionListener
{
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
can.repaint();
}
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
if (obj.equals(bto))
{
//h=2; v=2;
gc=Color.orange;
}
else if (obj.equals(btr))
{
//h=2; v=2;
gc=Color.red;
}
else if (obj.equals(btb))
{
//h=2; v=2;
gc=Color.black;
}
else if (obj.equals(btp))
{
h+=3;
v+=3;
if (h>=32) {h=32; v=32;} //브러쉬 크기를 잡아준다
}
else if (obj.equals(btm))
{
h-=3;
v-=3;
if (h<=2) {h=2; v=2;}
}
else if (obj.equals(btw))
{
//h=32; v=32;
gc=Color.white;
}
else if (obj.equals(btc))
{
//pc.add(can);
Graphics gg=can.getGraphics(); //현종이 덕에 알게된 Canvas 초기화법..
gg.setColor(Color.white);
gg.fillRect(0, 0, 370, 300);
}
setTitle("Painter [Brush Size "+h+"]"); //브러쉬 크기는 귀찮아서 그냥 타이틀에 출력
}
}
class MyCanvas extends Canvas
{
public void update(Graphics g)
{
g.setColor(gc);
g.fillOval(x, y, h, v);
}
}
public static void main(String[] args)
{
Painter p=new Painter();
p.setSize(400, 380);
p.setVisible(true);
}
}