[] STUDY ³»¿ë

ÀÛ¼ºÀÏ 2004-04-16
ÀÛ¼ºÀÚ park
Á¶È¸¼ö 1087
Á¦ ¸ñ µµÇü ±×¸®±â¿Í ¸Þ´º

¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[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);
                        }                                
                }
        };

}

µµÇü ±×¸®±â¿Í ¸Þ´º °ú(¿Í) °ü·ÃµÈ Ãß°¡ Á¤º¸

  • ¸¶¿ì½º¸¦ »ç¿ëÇÏ¿© ¼±±×¸®±â [2004-04-14] [park´Ô Àç°ø] [Á¶È¸:2056]
  • ¸¶¿ì½º¸¦ »ç¿ëÇÏ¿© »ç°¢Çü ±×¸®±â [2004-04-14] [park´Ô Àç°ø] [Á¶È¸:1014]
  • ¸¶¿ì½º·Î »ç°¢Çü ±×¸®°í reDrawÇϱâ [2004-04-14] [park´Ô Àç°ø] [Á¶È¸:1592]
  • µµÇü ±×¸®±â¿Í ¸Þ´º [2004-04-16] [park´Ô Àç°ø] [Á¶È¸:1087]
  • ±×·¡ÇÈ ¸â¼Òµå [2003-10-21] [park´Ô Àç°ø] [Á¶È¸:981]

    ¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â