發新話題

[JAVA] 小畫家的畫圖

[JAVA] 小畫家的畫圖

引用:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class b2 extends JFrame //畫圖物件是JFrame的擴充
{
    Container c;
    Color pencolor[]=new Color[4];
    byte clrpt=0;
    JButton rot0,rot1,rot2;
    JRadioButton r_JRadioButton,g_JRadioButton,b_JRadioButton;
    ButtonGroup radiogroup = new ButtonGroup();
    JLabel lab1=new JLabel("功能列表");
    int mode=0;
    imgZ1 AL1=new imgZ1();
    radioact RA1=new radioact();
    byte data[][]=new byte[480][640];
    byte data_new[][]=new byte[480][640];//存放畫圖資料的陣列
    int redraw,now_x,now_y,indrag;
    public b2()   //建構元
    {
        super("簡易畫板");
        int i,j;
        redraw=1;//進去先重畫一次
        indrag=0;
        //設定三種顏色,背景是白色
        pencolor[0]=Color.red;
        pencolor[1]=Color.green;
        pencolor[2]=Color.blue;
        pencolor[3]=Color.white;
        for (i=0;i<640;i=i+1)
                for (j=0;j<480;j=j+1)
                {
                        data[j]=3; //清成白色
                        data_new[j]=3; //清成白色
                }
        c=getContentPane();
        //設定buttom
        rot1=new JButton("存檔");
        rot2=new JButton("取回");
        rot0=new JButton("結束");
        //設定radio
        r_JRadioButton = new JRadioButton("red",true);
        g_JRadioButton = new JRadioButton("green");
        b_JRadioButton = new JRadioButton("blue");
        radiogroup.add(r_JRadioButton);
        radiogroup.add(g_JRadioButton);
        radiogroup.add(b_JRadioButton);
        //設定視窗大小
        setSize(800,600);
        c.setLayout(new FlowLayout(FlowLayout.CENTER));
        //裝設各種Adaptor
        c.add(lab1);
        c.add(rot1);
        c.add(rot2);
        c.add(rot0);
        c.add(r_JRadioButton);
        c.add(g_JRadioButton);
        c.add(b_JRadioButton);
        rot0.addActionListener(AL1);
        rot1.addActionListener(AL1);
        rot2.addActionListener(AL1);
        r_JRadioButton.addItemListener(RA1);
        g_JRadioButton.addItemListener(RA1);
        b_JRadioButton.addItemListener(RA1);
        addMouseMotionListener(new checkmouse());
        addMouseListener(new cmouse());
        show();
    }
    public void paint(Graphics g)  //真正的畫圖設定
    {
        int x,y;
        //super.paint(g);
        //不用super,是希望以前畫的圖不會被清掉
        g.setColor(Color.white);
        lab1.paint(lab1.getGraphics());
        rot1.paint(rot1.getGraphics());
        rot2.paint(rot2.getGraphics());
        rot0.paint(rot0.getGraphics());
        r_JRadioButton.paint(r_JRadioButton.getGraphics());
        g_JRadioButton.paint(g_JRadioButton.getGraphics());
        b_JRadioButton.paint(b_JRadioButton.getGraphics());
        if (redraw==0) //看看需不需要全部重畫
        {
                if (indrag==1)//如果是滑鼠拖曳中就畫這個點
                {
                        g.setColor(pencolor[clrpt]);
                        g.drawLine(now_x,now_y,now_x,now_y);
                }
        }
        else//需要全部重畫
        {
             for (x=0;x<640;x=x+1)
                for (y=0;y<480;y=y+1)
                {
                        g.setColor(pencolor[data[y][x]]);
                        g.drawLine(x,y+80,x,y+80);
                }
             redraw=0;
        }
    }
  class imgZ1 implements ActionListener//按鈕事件
  {
     public void actionPerformed(ActionEvent e)
     { int i,j;
         if (e.getSource()==rot0)//結束
          {
             System.exit(0);
          }

        /***透過檢查那個按鈕呼叫的,就可以判定該做的動作 ***/
        if (e.getSource()==rot1)//儲存
        {
            for (i=0;i<640;i=i+1)
                for (j=0;j<480;j=j+1)
                {
                        data_new[j]=data[j];
                }
        }
        else
        if  (e.getSource()==rot2)//讀取
        {
           for (i=0;i<640;i=i+1)
                for (j=0;j<480;j=j+1)
                {
                        data[j]=data_new[j];
                }
                redraw=1;
        }
        repaint(); //重新顯示一次
     }
  }
      /************選擇顏色*************************/
    class radioact implements ItemListener
    {
      public void itemStateChanged(ItemEvent e)
      {
               if (e.getSource() == r_JRadioButton)
                   clrpt = 0;
               else if (e.getSource() == g_JRadioButton)   
                   clrpt = 1;
               else
                   clrpt = 2;
               repaint();   
      }
    }//radioact

    /*****實作滑鼠移動事件處理物件 *******/
    class checkmouse extends MouseMotionAdapter
    {
   
            public void mouseDragged(MouseEvent e)
        {
                now_x=e.getX();
                now_y=e.getY();
               data[now_y-80][now_x]=clrpt;//記錄位置與顏色
               indrag=1; //設定要畫圖
               repaint(); //重新顯示一次
        }        
    }//checkmouse
    /*****實作滑鼠事件處理物件 *******/
    class cmouse extends MouseAdapter
    {
   
            public void mousePressed(MouseEvent e)
        {
                now_x=e.getX();
                now_y=e.getY();
               data[now_y-80][now_x]=clrpt;//記錄位置與顏色
               indrag=1; //設定要畫圖
               repaint(); //重新顯示一次
        }        
        public void mouseReleased(MouseEvent e)
        {
                indrag=0; //不用畫了
        }
    }//MyMotionListener  
    public static void main(String args[]) //程式起點
    {
        b2 app=new b2(); //畫圖
        //處理視窗關閉要求
        app.addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing(WindowEvent e)
            {  System.exit(0);}
         });
        
    }
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。