수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

그림판에 대해 질문이요..!,급해요 ㅠ

날샘

2023.04.01

마우스로 그림 그리는거 연습하고 있는데요...(그림판이랑 비슷하긴 하지만..)
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);
}

}

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

번호 제 목 글쓴이 날짜
2701397 세로 100% 푸터부분이 바닥에 안붙어요(세로 100% 되는 소스를 썼거든요) 꽃겨울 2025-07-13
2701369 [긴급]로드해온 swf가 갑자기 사라지는 현상..(익스10) (2) 곰돌이 2025-07-13
2701340 [c++]학교 과제 질문이요...... (3) 기쁨해 2025-07-13
2701311 구글 뉴스검색최적화 작업은 누구의 영역인가요? 많은 조언 부탁드려요! 리나 2025-07-13
2701285 아이폰이나 안드로이드 폰 인터넷으로 볼때 배꽃 2025-07-12
2701230 테마 설정하면 밑에 뜨는 글 삭제 (1) 창의적 2025-07-12
2701177 css적용이 안되요~ (6) 다니엘 2025-07-11
2701151 사이트작업시 inputbox 가 readonly 형태표시 어떻게 하시나요? (1) 찬내 2025-07-11
2701123 간단한 select 질문입니다 (3) 천사의눈물 2025-07-11
2701061 비베질문.. 똘끼 2025-07-10
2701034 메일폼 내 script 삽입가능한 방법 없을까요.. (2) 마음새 2025-07-10
2701008 분명히 버튼을 만들었는데 액션이 안걸립니다. (3) 재찬 2025-07-10
2700923 전체중앙정렬&독타입&쿼크모드 ㅜㅠ (8) 푸른들 2025-07-09
2700893 질문드리겠습니다. 도도한 2025-07-09
2700793 무비클립에 마우스 오버시 랜덤으로 효과음 나기는 어떻게 ;; (1) 바닐라 2025-07-08
2700741 웹전송? (2) 연와인 2025-07-07
2700686 카테고리 호버시 세부카테고리 보이게하는 것, css로만 가능할까요?? (3) 다힘 2025-07-07
2700658 메타태그 질문드립니다..ㅠㅠ;;; 모해 2025-07-07
2700632 외부에서 이미지 파일을 불러와야 합니다. 도와주세요. (4) 에일린 2025-07-06
2700579 (air + as3) smtp 이용해서 첨부파일 포함해서 메일 보내기 물보라 2025-07-06
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com