用Java製作小畫家~

本程式碼製作的小畫家有以下功能:畫筆(直線、點、方塊)、顏色(預設左鍵黑、預設右鍵白、每一刻都隨機色彩、使用者RGB自訂)、清空畫布

目前瑕疵:方塊功能只可以由左上往又下拉


 

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

public class painter extends Frame implements WindowListener, MouseMotionListener, MouseListener, ActionListener{
    
    int mx=0,my=0,mx1=0,my1=0,mx2=0,my2=0;/*mouse location*/ /*存取滑鼠位置變數*/
    int mode=0;Boolean clr=false, rb = false; /*用於按鈕判斷模式的變數*/
    int mbt=0; /*存取滑鼠左右鍵的變數*/
    Button bt = null, bt2=null, bt3=null, bt4=null; /*按鈕*/
    TextField th_r = null, th_g = null, th_b = null; /*使用者輸入框 用來輸入RGB數值*/
    Color cr = new Color(0,0,0); /*顏色變數 預設 黑*/
    Color wht = new Color(255,255,255); /*顏色變數 白色*/
    

/*建構子*/
    painter(){  

/*視窗設定*/
        this.setSize(900, 800);
        this.setLocation(500,50);
        this.setLayout(null);

/*按鈕設定*/
        bt = new Button("Rect mode");
        bt.setSize(80, 50);
        bt.setLocation(100,50);
        bt.addActionListener(this);
        
        bt2 = new Button("Clean all");
        bt2.setSize(80, 50);
        bt2.setLocation(200,50);
        bt2.addActionListener(this);
        
        bt3 = new Button("Rainbow mode");
        bt3.setSize(100, 50);
        bt3.setLocation(100,300);
        bt3.addActionListener(this);

        bt4 = new Button("Set color");
        bt4.setSize(80, 50);
        bt4.setLocation(100,250);
        bt4.addActionListener(this);

/*RGB輸入框設定*/
        th_r = new TextField(3);
        th_r.setSize(80,50);
        th_r.setLocation(100, 100);
        
        th_g = new TextField(3);
        th_g.setSize(80,50);
        th_g.setLocation(100, 150);
        
        th_b = new TextField(3);
        th_b.setSize(80,50);
        th_b.setLocation(100, 200);
        

/*顯示與新增物件至此建構子*/
        this.addWindowListener(this);
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        this.add(bt);
        this.add(bt2);
        this.add(bt3);
        this.add(bt4);
        this.add(th_r);
        this.add(th_g);
        this.add(th_b);
        this.setVisible(true);
    }

/*main主程式執行*/

    public static void main(String[] args){
        painter pp = new painter();
    }

    /*Override*/
    public void update(Graphics g){
        paint(g);
    }
    public void paint(Graphics g){
        /*cr = new Color(fun.Rand(0,255),fun.Rand(0,255),fun.Rand(0,255));*/
        

/*左右鍵判斷*/
        if(mbt==MouseEvent.BUTTON1){g.setColor(cr);}
        else if(mbt==MouseEvent.BUTTON3){g.setColor(wht);}

/*模式判斷*/
        if(mode==0){g.fillRect(mx1, my1, mx2-mx1, my2-my1);}
        else if(mode==1){g.drawLine(mx1, my1, mx2, my2);mx1=mx2;my1=my2;}
        else if(mode==-1){g.fillOval(mx-3, my-3, 7, 7);}
    
        if(clr==true){
            g.setColor(wht);
            g.fillRect(0,0,99999,99999);
            clr=false;
        }
        
        if(rb==true){
            cr = new Color(fun.Rand(0,255),fun.Rand(0,255),fun.Rand(0,255));
            g.setColor(cr);
        }
    }


    /*-WindowListener interface of event-*/
    @Override
    public void windowOpened(WindowEvent e) {}
    @Override
    public void windowClosing(WindowEvent e) {dispose();}
    @Override
    public void windowClosed(WindowEvent e) {}
    @Override
    public void windowIconified(WindowEvent e) {}
    @Override
    public void windowDeiconified(WindowEvent e) {}
    @Override
    public void windowActivated(WindowEvent e) {}
    @Override
    public void windowDeactivated(WindowEvent e) {}

    /*-mouse motion interface of event-*/
    @Override/*拖曳:mode=-1表示畫點、0與1表示畫線段及方塊*/
    public void mouseDragged(MouseEvent e) {
        if(mode==-1){mx=e.getX();my=e.getY();repaint();}
        else if(mode==0||mode==1){mx2=e.getX();my2=e.getY();repaint();}
        }
    @Override
    public void mouseMoved(MouseEvent e) {}
    
    /*-Mouse Listener interface of event-*/
    @Override
    public void mouseClicked(MouseEvent e) {}
    @Override/*點擊*/
    public void mousePressed(MouseEvent e) {
        mbt = e.getButton();/*check mouse right, middle, left*/
        if(mode==-1){mx=e.getX();my=e.getY();repaint();}
        else if(mode==0||mode==1){mx1=e.getX();my1=e.getY();}        
        }
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}

    /*-Action interface of event-*/
    @Override/*判斷按鈕被點擊*/
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==bt){
            mode++;
            if(mode>1){mode=-1;}
            if(mode==0){bt.setLabel("Rect mode");}
            else if(mode==1){bt.setLabel("Line mode");}
            else if(mode==-1){bt.setLabel("Dot mode");}
        }
        
        if(e.getSource()==bt2){
            if(clr==false){clr = true;repaint();}
        }
        
        if(e.getSource()==bt3){
            if(rb==false){rb = true;}            
        }
        
        if(e.getSource()==bt4){
            int r = Integer.parseInt(th_r.getText());
            int g = Integer.parseInt(th_g.getText());
            int b = Integer.parseInt(th_b.getText());
            
            if(rb==true){rb = false;}
            cr = new Color(r,g,b);
        }
    }
}

arrow
arrow
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()