검색결과 리스트
글
자바 21장 과제 2
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.applet.*;
class Draw implements Serializable{
private int x,y,x1,y1;
private int dist;
public int getDist() {
return dist;
}
public void setDist(int dist) {
this.dist = dist;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
}
public class Ex1 extends Applet implements ItemListener ,MouseListener,MouseMotionListener{
private MenuBar mb=new MenuBar();
private Menu draw =new Menu("DRAW");
private CheckboxMenuItem pen=new CheckboxMenuItem("PEN",true);
private CheckboxMenuItem line=new CheckboxMenuItem("LINE");
private CheckboxMenuItem oval=new CheckboxMenuItem("OVAL");
private CheckboxMenuItem rect=new CheckboxMenuItem("RECT");
private int x,y,x1,y1;
private Vector vc =new Vector();
public void init(){
draw.add(pen);
draw.add(line);
draw.add(oval);
draw.add(rect);
mb.add(draw);
// this.setMenuBar(mb);
}
public void start(){
pen.addItemListener(this);
line.addItemListener(this);
oval.addItemListener(this);
rect.addItemListener(this);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
//업데이트 없에면 좀 다름 근대 버퍼링이 생김
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
for(int i=0;i<vc.size();++i){
Draw d=(Draw)vc.elementAt(i);
if(d.getDist()==1){
g.drawLine(d.getX(), d.getY(), d.getX1(), d.getY1());
}
else if(d.getDist()==2){
g.drawOval(d .getX(), d.getY(), d.getX1()-d.getX(), d.getY1()-d.getY());
}
else if(d.getDist()==3){
g.drawRect(d.getX(), d.getY(), d.getX1()-d.getX(), d.getY1()-d.getY());
}
}
if(line.getState()==true){
g.drawLine(x, y, x1, y1);
}
else if(oval.getState()==true){
g.drawOval(x, y, x1-x, y1-y);
}
else if(rect.getState()==true){
g.drawRect(x, y, x1-x, y1-y);
}
}
public void itemStateChanged(ItemEvent e) {
pen.setState(false);
line.setState(false);
oval.setState(false);
rect.setState(false);
CheckboxMenuItem imsi=(CheckboxMenuItem)e.getSource();
imsi.setState(true);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
y1=e.getY();
this.repaint();
if(pen.getState()!=true){
int dist=0;
if(line.getState()==true)dist=1;
else if(oval.getState()==true)dist=2;
else if(rect.getState()==true)dist=3;
Draw d=new Draw();
d.setDist(dist);
d.setX(x);
d.setY(y);
d.setX1(x1);
d.setY1(y1);
vc.add(d);
}
}
@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 mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
y1=e.getY();
this.repaint();
if(pen.getState()){
Draw d=new Draw();
d.setDist(1);
d.setX(x);
d.setY(y);
d.setX1(x1);
d.setY1(y1);
vc.add(d);
x=x1;
y=y1;
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
'JAVA > 소스' 카테고리의 다른 글
열혈강의 자바 22장 과제 2 (3) | 2012.01.11 |
---|---|
열혈강의 자바 22장 과제 1 (1) | 2012.01.11 |
자바 문자열 숫자로 바꾸는 방법 (0) | 2011.12.12 |
열혈강의 자바 14장 과제1 (0) | 2011.10.30 |
열혈강의 자바 13장 과제2 (0) | 2011.10.27 |