프로그래밍

화이트보드(사각형)

Dilrong 2013. 12. 2. 15:27
import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 

public class WhiteBoard extends Applet implements ItemListener {
     Checkbox chk1, chk2, chk3, chk4; 
    Checkbox chk5, chk6, chk7; 
     
    Color curCol = Color.black; 
//여러개로 할 시 배열로 변경
    int x,y,w,h; 
     
    public void init() {
         setBackground(Color.white); 
        CheckboxGroup cbg = new CheckboxGroup();
         CheckboxGroup cbg2 = new CheckboxGroup();
          
        chk5 = new Checkbox("사각형", cbg2, false);
         chk5.setBackground(Color.orange); 
        chk5.addItemListener(this); 
         
        chk6 = new Checkbox("원", cbg2, false);
         chk6.setBackground(Color.orange); 
        chk6.addItemListener(this); 
         
        chk7 = new Checkbox("선", cbg2, true);
         chk7.setBackground(Color.orange); 
        chk7.addItemListener(this); 
         
        chk1 = new Checkbox("검정색", cbg, true);
         chk1.setBackground(Color.black); 
        chk1.setForeground(Color.white); 
        chk1.addItemListener(this); 
         
        chk2 = new Checkbox("빨강색", cbg, false);
         chk2.setBackground(Color.red); 
        chk2.addItemListener(this); 
         
        chk3 = new Checkbox("보라색", cbg, false);
         chk3.setBackground(Color.magenta); 
        chk3.addItemListener(this); 
         
        chk4 = new Checkbox("녹 색", cbg, false);
         chk4.setBackground(Color.green); 
        chk4.addItemListener(this); 
         
        add(chk1); 
        add(chk2); 
        add(chk3); 
        add(chk4); 
        add(chk5); 
        add(chk6); 
        add(chk7); 
        addMouseListener(new MouseEventHdl());
         addMouseMotionListener(new MouseMotionHdl());
     } 
     
    public void itemStateChanged(ItemEvent e) {
         if (e.getItem().equals("검정색")) curCol = Color.black;
         else if (e.getItem().equals("빨강색")) curCol = Color.red;
         else if (e.getItem().equals("보라색")) curCol = Color.magenta;
         else if (e.getItem().equals("녹 색")) curCol = Color.green;
     } 
     
    public void actionPerFormed(ActionEvent e) {
     } 
     
    public void paint(Graphics g) {
         g.setColor(curCol); 
        g.drawRect(x, y, w, h); 
    } 
     
    public class MouseMotionHdl extends MouseMotionAdapter {
         public void mouseDragged(MouseEvent e) {
             w=Math.abs(x-e.getX()); 
            h=Math.abs(y-e.getY()); 
            repaint(); 
        } 
    } 
     
    public class MouseEventHdl extends MouseAdapter {
         public void mousePressed(MouseEvent e) {
             x = e.getX(); 
            y = e.getY(); 
        } 
        public void mouseReleused(MouseEvent e) {
             w = Math.abs(x-e.getX()); 
            h = Math.abs(y-e.getY()); 
        } 
    } 
}
반응형