게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
자바 스윙 그림판 액션 문제
게시물ID : programmer_10944짧은주소 복사하기
작성자 : 必*痕
추천 : 0
조회수 : 1623회
댓글수 : 1개
등록시간 : 2015/06/06 23:03:03
옵션
  • 창작글
  • 베스트금지
  • 본인삭제금지
소스 올립니다. 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class painting extends JFrame {
public static final int PEN = 0, OVAL = 1, RECT = 2, LINE = 3, FULL = 4;
private int MODE ;
private Color SC;
ButtonGroup topGroup;
JToolBar topToolbar;
JToggleButton tpen, tcir, trec, tli, col, tfull;
ImageIcon pp, pc, pr, pl, pf, subject; // 도구 이미지
PaintCanvas can;
// 파일 바 미구현
MenuBar MEBA = new MenuBar();

Menu file = new Menu("파일");
MenuItem fnew = new MenuItem("새로열기");
MenuItem fopen = new MenuItem("불러오기");
MenuItem fsave = new MenuItem("저장하기");
MenuItem fas = new MenuItem("다른 이름으로 저장");
MenuItem fexit = new MenuItem("종료하기");

Menu option = new Menu("옵션");
Menu oview = new Menu("보기");
CheckboxMenuItem pv = new CheckboxMenuItem("확대");
CheckboxMenuItem mv = new CheckboxMenuItem("축소");

Menu help = new Menu("도움");
MenuItem about = new MenuItem("만든사람들");

public painting(String title) {
super("그림판");
this.init();
this.start();
this.setIconImage(subject.getImage());
this.setSize(1000, 1000);
setVisible(true);

}

public void BI() {// Button Image
subject = new ImageIcon("그림판.png");
pp = new ImageIcon("수정됨_연필.png");
pc = new ImageIcon("수정됨_CriCle.png");
pr = new ImageIcon("수정됨_사각형.png");
pl = new ImageIcon("수정됨_직선.png");
pf = new ImageIcon("수정됨_채우기.png");
}

public void init() {
// 메뉴바 추가 + 미구현
BI();// <- Button Image
this.setMenuBar(MEBA);
MEBA.add(file);
file.add(fnew);
file.addSeparator(); // <- 구분선
file.add(fopen);
file.add(fsave);
file.add(fas);
file.add(fexit);

MEBA.add(option);
option.add(oview);
oview.add(pv);
oview.add(mv);
option.addSeparator();// <- 구분선

MEBA.add(help);
help.add(about);
BT();
CT();

}

public void BT() {
// --------------------------버튼------------------------//
topToolbar = new JToolBar();
topToolbar.setFloatable(false);

tpen = new JToggleButton(pp);
tcir = new JToggleButton(pc);
trec = new JToggleButton(pr);
tli = new JToggleButton(pl);
tfull = new JToggleButton(pf);
col = new JToggleButton("Other Color...");

topGroup = new ButtonGroup(); // 하나만 선택되게
topGroup.add(tpen);
topGroup.add(tcir);
topGroup.add(trec);
topGroup.add(tli);
topGroup.add(tfull);
topGroup.add(col);

topToolbar.add(tpen);
topToolbar.addSeparator();
topToolbar.add(tcir);
topToolbar.addSeparator();
topToolbar.add(trec);
topToolbar.addSeparator();
topToolbar.add(tli);
topToolbar.addSeparator();
topToolbar.add(tfull);
topToolbar.addSeparator();
topToolbar.add(col);
}

public void CT() {
col.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SC = JColorChooser.showDialog(
((Component) e.getSource()).getParent(),
"Color Spectrum", SC);
if (SC == null)
SC = Color.black;
}
});

}

public void start() {
can = new PaintCanvas();

this.add(BorderLayout.CENTER, can);
this.add(BorderLayout.NORTH, topToolbar);

this.setMenuBar(MEBA);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tpen.addActionListener(new ActionHandler());
tcir.addActionListener(new ActionHandler());
trec.addActionListener(new ActionHandler());
tli.addActionListener(new ActionHandler());
tfull.addActionListener(new ActionHandler());
col.addActionListener(new ActionHandler());

}

class PaintCanvas extends Canvas implements MouseListener,
MouseMotionListener {
int sx, sy, ex, ey;
private Color SC;
private int SShape;
private int SThick;

public void setSelectedColor(Color selectedColor) {
this.SC = selectedColor;

}

public void setSelectedShape(int selectedShape) {
this.SShape = selectedShape;

}

public void setSelectedThick(int selectedThick) {
this.SThick = selectedThick;

}

public PaintCanvas() {
this.setBackground(Color.WHITE);
this.addMouseListener(this);
this.addMouseMotionListener(this);

}

public void paint(Graphics g) {
if (MODE == PEN || MODE == LINE) {
g.setColor(Color.black);
g.drawLine(sx, sy, ex, ey);
} else if (MODE == OVAL) {
if (MODE == FULL) {
g.fillOval(sx, sy, ex - sx, ey - sy);
} else {
g.drawOval(sx, sy, ex - sx, ey - sy);
}
} else if (MODE == RECT) {
if (MODE == FULL) {
g.fillRect(sx, sy, ex - sx, ey - sy);
} else {
g.drawRect(sx, sy, ex - sx, ey - sy);
}
}
}

public void update(Graphics g) {
paint(g);

}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}

@Override
public void mousePressed(MouseEvent e) {// 클릭했을때
// TODO Auto-generated method stub
sx = e.getX();
sy = e.getY();

}

@Override
public void mouseReleased(MouseEvent e) {// 땠을때
// TODO Auto-generated method stub
ex = e.getX();
ey = e.getY();
can.repaint();

}
}

class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == tpen) {
tpen.setSelected(true);
can.setSelectedShape(painting.PEN);
} else if (e.getSource() == tcir) {
tcir.setSelected(true);
can.setSelectedShape(painting.OVAL);
} else if (e.getSource() == trec) {
trec.setSelected(true);
can.setSelectedShape(painting.RECT);
} else if (e.getSource() == tli) {
tli.setSelected(true);
can.setSelectedShape(painting.LINE);
}

else if (e.getSource() == tfull) {
if (e.getSource() == tpen) {
tpen.setSelected(true);
can.setSelectedShape(painting.PEN);
} else if (e.getSource() == tcir) {
tcir.setSelected(true);
can.setSelectedShape(painting.OVAL);
} else if (e.getSource() == trec) {
trec.setSelected(true);
can.setSelectedShape(painting.RECT);
} else if (e.getSource() == tli) {
tli.setSelected(true);
can.setSelectedShape(painting.LINE);
}
}
}
}

public static void main(String[] args) {
painting painted = new painting("Painting");
}
}


액션이 먹히질 않네요

소스상에서 모드 선언 부에 0이나 1이나 값을 넣어주면 

그려지기는 하는데

버튼을 클릭하면 그 버튼의 액션이 안먹혀요 ㅜㅜ
모드에도 액션리스너를 넣어주어야 하나요??


출처 도와 주세요 ㅜㅗㅜ
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호