그림판에 대해 질문이요..!,급해요 ㅠ
날샘
마우스로 그림 그리는거 연습하고 있는데요...(그림판이랑 비슷하긴 하지만..)
clear 버튼 누르면,, canvas에 지운거 다 지우고 싶은데,, clearRect쓰면될것같은데,, 안되네요..
어떻게 해야할지 ...
그리고,,그림그리고, 색깔 바꾸면,, 전에 그렸던 그림이 다 지워집니다..
어떻게 해야할까요.. ㅠㅠ
두가지좀 가르쳐주세요..
소스 올립니다...
======================================================================================
// 첫 화면
---- SelectPanel .java-----
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SelectPanel extends JFrame implements ActionListener,ItemListener{
private JButton select,clear,result,close;
private JColorChooser jcc = new JColorChooser(); // 색선택
private Color co;
Canvas canvas;
Graphics g;
public int flag,c_flag;
PaintPanel paintpanel=new PaintPanel();
String str=펜;
public SelectPanel(){
JPanel panel1= new JPanel();
panel1.setLayout(new GridLayout(5,1,20,20));
JPanel panel2= new JPanel();
panel2.setLayout(new BorderLayout());paintpanel.setSize(500,500);
Container ct= getContentPane();
ct.setLayout(new FlowLayout());
select= new JButton(Color Select);
clear=new JButton(clear);
result=new JButton(result);
close=new JButton(Close);
JComboBox combobox= new JComboBox();
ct.add(panel1);
ct.add(panel2);
panel1.add(select);
panel1.add(combobox);
combobox.addItem(도형선택);
combobox.addItem(펜);
combobox.addItem(선);
combobox.addItem(사각형);
combobox.addItem(원);
panel1.add(clear);
panel1.add(result);
panel1.add(close);
panel2.add(new JLabel(그림을 그리시오),BorderLayout.NORTH);
panel2.add(paintpanel,BorderLayout.CENTER);
select.addActionListener(this);
clear.addActionListener(this);
result.addActionListener(this);
close.addActionListener(this);
combobox.addItemListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(select))
{
co = jcc .showDialog(this,색선택,Color.black);
paintpanel.select(flag,co);
//paintpanel.update(g);
}
else if(ae.getSource().equals(clear))
{
//paintpanel.setColor(Color.white);
//paintpanel.canvas.clear();
c_flag=5;
paintpanel.select(c_flag,co);
System.out.println(c_flag+c_flag);
}
else if(ae.getSource().equals(result))
{
}
else if(ae.getSource().equals(close))
setVisible(false); //창닫기
}
public void itemStateChanged(ItemEvent ie){
Object s=ie.getItem();
str=s.toString();
if(str.equals(펜))
flag=0;
else if(str.equals(선))
flag=1;
else if(str.equals(원))
flag=2;
else if(str.equals(사각형))
flag=3;
paintpanel.select(flag,co);
}
}// 마우스로 그림 그리는 부분,
----------------------PaintPanel .java ---------------------
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JPanel;
public class PaintPanel extends Canvas implements MouseMotionListener, MouseListener{
Point first_point, last_point, old_point;
MenuItem menu_tool_pen, menu_tool_line, menu_tool_circle, menu_tool_rect;
Canvas canvas;
private Color co;
String str=펜;
public int flag,c_flag;
public PaintPanel(){
setBackground(Color.white);//canvas 색깔
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me){
first_point = me.getPoint();
old_point=me.getPoint();
System.out.println(first_point);
}
public void mouseReleased(MouseEvent me){
last_point = me.getPoint();
System.out.println(last_point);
repaint();
}
public void mouseDragged(MouseEvent me){
;
last_point = me.getPoint();
System.out.println(last_point);
repaint();
}
public void mouseMoved(MouseEvent me){}
public void update(Graphics g){
/* update()메서드 오버라이드~
이 함수가 호출될 때 화면을 다시 지우고 그리지 않고
현재 화면에 계속 출력될 수 있도록 paint()메서드를 호출한다.
*/
paint(g);
}public void select(int flag,Color selectco){
if(flag==0)
{
str=펜;
System.out.println(str);
}
else if(flag==1)
{
str=선;
System.out.println(str);
}
else if(flag==2)
{
str=원;
System.out.println(str);
}
else if(flag==3)
{
str=사각형;
System.out.println(str);
}
else if(flag==5)
{
//canvas.clear();
}
co=selectco;
System.out.println(flag);
}public void paint(Graphics g){
System.out.println(c_flag:+c_flag);
if(c_flag==5){
super.paint(g);
System.out.println(clear);
g.clearRect(0, 0, 500, 500);
g.setColor(Color.blue);
}else{
if(first_point != null && last_point != null) {
if(str.equals(펜)) {
g.setColor(co);
g.drawLine(first_point.x, first_point.y,
last_point.x, last_point.y);
first_point=last_point;
// return;
}
if(str.equals(선)) {
//g.setColor(co);
g.setColor(Color.WHITE);
g.drawLine(first_point.x, first_point.y,
old_point.x, old_point.y);
g.setColor(co);
//g.setColor(Color.BLACK);
g.drawLine(first_point.x, first_point.y,
last_point.x, last_point.y);
}
else if(str.equals(원)) {
//g.setColor(co);
g.setColor(new Color(255,255,255));
g.fillOval(first_point.x, first_point.y,
old_point.x-first_point.x,
old_point.y-first_point.y);
g.setColor(co);
// g.setColor(new Color(0,0,255));
g.fillOval(first_point.x, first_point.y,
last_point.x-first_point.x,
last_point.y-first_point.y);
}
else if(str.equals(사각형)) {
//g.setColor(co);
g.setColor(new Color(255,255,255));
g.fillRect(first_point.x, first_point.y,
old_point.x-first_point.x,
old_point.y-first_point.y);
g.setColor(co);
// g.setColor(new Color(255,0,0));
g.fillRect(first_point.x, first_point.y,
last_point.x-first_point.x,
last_point.y-first_point.y);
}
old_point=last_point;
}
}
}
}// main 부분
----------------------Painter.java ----------------------
import java.awt.*;
import javax.swing.*;
public class Painter {
public static void main(String args[]){
SelectPanel sp=new SelectPanel();
sp.setTitle(연습);
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sp.setSize(800,600);
sp.setVisible(true);
}
}