|
¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â |
// ¸¶¿ì½º¸¦ ÀÌ¿ëÇÏ¿© »ç°¢Çü ±×¸®±â import java.awt.*; import java.awt.event.*; import java.util.*; // µµÇüÀÇ Á¾·ù Ŭ·¡½º class drawData { int fx, fy; int sx, sy; Color lineColor; int type; // 1=¼±, 2=»ç°¡Çü, 3=¿ø , 4=Ææ boolean flag=false; //¿Ï¼ºµÈ µµÇüÀΰ¡ ? } class Pen { int fx, fy; int sx, sy; Color lineColor; } class drawRect extends Frame implements ActionListener { // WindowAdapter myWinAdapter myW = new myWinAdapter(); // MouseAdapter and MouseMotionAdapter myMouseAdapter myM = new myMouseAdapter(); myMouseMotionAdapter myMM = new myMouseMotionAdapter(); drawData data; Pen pen; int fx,fy, sx, sy; int ox, oy; int type; Color lineColor=Color.black; Vector vector = new Vector(10,5); Vector penVector = new Vector(50,30); MenuBar menuBar = new MenuBar(); Menu drawMenu = new Menu("±×¸®±â"); Menu lineColorMenu = new Menu("¼±»ö"); Menu undoMenu = new Menu("ÆíÁý"); MenuItem lineDraw = new MenuItem("LINE"); MenuItem rectDraw = new MenuItem("RECT"); MenuItem ovalDraw = new MenuItem("OVAL"); MenuItem penDraw = new MenuItem("PEN"); MenuItem blackColor = new MenuItem("black"); MenuItem redColor = new MenuItem("red"); MenuItem blueColor = new MenuItem("blue"); MenuItem greenColor = new MenuItem("green"); MenuItem undo = new MenuItem("Undo"); public drawRect() { setTitle("DrawRect"); createMenu(); setMenuBar(menuBar); addWindowListener(myW); addMouseListener(myM); addMouseMotionListener(myMM); setSize(400,400); } public void createMenu() { menuBar.add(drawMenu); menuBar.add(lineColorMenu); menuBar.add(undoMenu); addMenu(drawMenu, lineDraw); addMenu(drawMenu, rectDraw); addMenu(drawMenu, ovalDraw); addMenu(drawMenu, penDraw); addMenu(lineColorMenu, blackColor); addMenu(lineColorMenu, redColor); addMenu(lineColorMenu, blueColor); addMenu(lineColorMenu, greenColor); addMenu(undoMenu, undo); } public void addMenu(Menu menu, MenuItem item) { menu.add(item); item.addActionListener(this); } public void paint(Graphics g) { int i; drawData data; int w, h; for(i=0 ; i<vector.size(); i++) { data = (drawData)vector.elementAt(i); draw(data); } } public void draw(drawData d) { if(d.type==1) drawLine(d); if(d.type==2) drawRect(d); if(d.type==3) drawOval(d); if(d.type==4) drawPen(pen); } // ¼± public void drawLine(drawData d) { Graphics g = getGraphics(); if(d.flag) // ¿Ï¼ºµÈ µµÇü { g.setColor(d.lineColor); g.drawLine(d.fx, d.fy, d.sx, d.sy); } else // ±×¸®°í ÀÖ´Â µµÇü { g.setColor(lineColor); g.setXORMode(Color.white); g.drawLine(d.fx, d.fy, ox, oy); g.drawLine(d.fx, d.fy, d.sx, d.sy); } } // »ç°¢Çü public void drawRect(drawData d) { int oldWidth, oldHeight; int width, height; Graphics g = getGraphics(); width = d.sx - d.fx; height = d.sy - d.fy; if(d.flag) //¿Ï¼ºÀ̰ųª ´Ù½Ã ±×¸±¶§ { g.setColor(d.lineColor); oldWidth = d.sx - d.fx; oldHeight = d.sy - d.fy; g.drawRect(d.fx, d.fy, width, height); } else { g.setColor(lineColor); oldWidth = ox - d.fx; oldHeight = oy - d.fy; g.setXORMode(Color.white); g.drawRect(d.fx, d.fy, oldWidth, oldHeight); g.drawRect(d.fx, d.fy, width, height); } } // ¿ø public void drawOval(drawData d) { int oldWidth, oldHeight; int width, height; Graphics g = getGraphics(); if(d.flag) { g.setColor(d.lineColor); width = d.sx - d.fx; height = d.sy - d.fy; g.drawOval(d.fx, d.fy, width, height); } else { g.setColor(lineColor); width = d.sx - d.fx; height = d.sy - d.fy; oldWidth = ox - d.fx; oldHeight = oy - d.fy; g.setXORMode(Color.white); g.drawOval(d.fx, d.fy, oldWidth, oldHeight); g.drawOval(d.fx, d.fy, width, height); } } // Ææ public void drawPen(Pen p) { Graphics g = getGraphics(); g.setColor(p.lineColor); g.drawLine(p.fx, p.fy, p.sx, p.sy); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("LINE")) { type = 1;} if(e.getActionCommand().equals("RECT")) { type = 2;} if(e.getActionCommand().equals("OVAL")) { type = 3;} if(e.getActionCommand().equals("PEN")) { type = 4;} if(e.getActionCommand()=="black"){lineColor = new Color(0,0,0);} if(e.getActionCommand()=="red"){lineColor = new Color(255,0,0);} if(e.getActionCommand()=="green"){lineColor = new Color(0,255,0);} if(e.getActionCommand()=="blue"){lineColor = new Color(0,0,255);} if(e.getActionCommand().equals("Undo")) {vector.removeElementAt(vector.size()-1);} repaint(); } public static void main(String[] args) { drawRect f = new drawRect(); f.setVisible(true); } // inner classes class myWinAdapter extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }; class myMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { if(type==4) // ÆæÀÎ °æ¿ì { sx=e.getX(); sy=e.getY(); penVector = new Vector(50,10); } else { data = new drawData(); data.fx=e.getX(); data.fy=e.getY(); data.sx=e.getX(); data.sy=e.getY(); data.type=type; data.lineColor=lineColor; ox=e.getX(); oy=e.getY(); } } public void mouseReleased(MouseEvent e) //µµÇüÀÇ ¸¶¹«¸® { if(type == 0 ) return; if(type==4) // ÆæÀÎ °æ¿ì { vector.addElement(penVector); //penVector.removeAllElements(); } else { data.flag=true; vector.addElement(data); } } }; class myMouseMotionAdapter extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { if(type==4) // ÆæÀ̸é { pen = new Pen(); fx = sx; fy = sy; sx = e.getX(); sy = e.getY(); pen.fx=fx; pen.fy=fy; pen.sx=sx; pen.sy=sy; pen.lineColor=lineColor; penVector.addElement(pen); drawPen(pen); } else { ox = data.sx; oy = data.sy; data.sx=e.getX(); data.sy=e.getY(); draw(data); } } }; } |
¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â |