欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java加密解密示例分享

 更新時(shí)間:2014年01月29日 11:13:09   作者:  
想要?jiǎng)?chuàng)造一個(gè)只有自己能看懂的文件嗎?那就是對(duì)數(shù)據(jù)加密吧,下面分享一個(gè)java的數(shù)據(jù)加密與解密示例

(1)先定義要實(shí)現(xiàn)的類,我先定義了一個(gè)抽象類

復(fù)制代碼 代碼如下:

//圖形類 
abstract class  Shape{ 
     int x,y; 
     int x1,y1; 
    Color color ; 
    Graphics g ; 
    byte type ; 
    public abstract void draw(Graphics g) ; 


//直線類 
 class LineShape extends Shape{ 

    public LineShape(int x, int y ,int x1,int y1,Color color){ 
        this.x = x ; 
        this.y = y ; 
        this.x1 = x1 ; 
        this.y1 = y1 ; 
        this.color = color ; 
        this.type = 0; 
    } 

    @Override 
    public void draw(Graphics g) { 
        g.setColor(color) ; 
        g.drawLine(x, y, x1, y1) ; 

    } 

 } 
//圓類 
class OvalShape extends Shape{ 

  public OvalShape(int x,int y, int x1,int y1,Color color){ 
        this.x = x ; 
        this.y = y ; 
        this.x1 = x1 ; 
        this.y1 = y1 ; 
        this.color = color ; 
        this.type = 1; 
     } 
    @Override 
    public void draw(Graphics g) { 

        g.setColor(color) ; 
        g.drawOval((x+x1)/2,(y+y1)/2,(x1-x)/2 ,(y1-y)/2 ); 
    } 

 } 
//圖片類 
 class picture extends Shape{ 
     int a ; 
     int b ; 
     int cl[][] ; 
     public picture(int a,int b,int cl[][]){ 
         this.type =2 ; 
         this.a =a ; 
         this.b =b ; 
         this.cl =cl ; 

     } 
     public void draw(Graphics g){ 
         for(int k=0;k<a;k++){ 
              for(int j=0;j<b;j++){ 

                Color c   =  new Color(cl[k][j]) ; 
                g.setColor(c); 
                g.drawLine(k+100,j+100,k+100,j+100); 
              } 
          }  
     } 
 } 
 

(2)總方法類

復(fù)制代碼 代碼如下:

public class BmpSaveStart { 

    Graphics g ; 
    public static void main(String[] args) { 
         new BmpSaveStart() .UI(); 

    } 
    public void UI(){ 
        JFrame jf = new JFrame(); 
        jf.setSize(600,600); 

        jf.setTitle("圖片的存儲(chǔ)"); 
        jf.setBackground(Color.WHITE ); 
        JMenuBar bar = new JMenuBar() ; 
        JMenu menu1 = new JMenu("選項(xiàng)") ; 
        JMenuItem m1 = new JMenuItem("畫圓"); 
        JMenuItem m2 = new JMenuItem("畫線"); 
        JMenuItem m3 = new JMenuItem("存儲(chǔ)"); 
        JMenuItem m4 = new JMenuItem("打開(kāi)"); 
        JMenuItem m5 = new JMenuItem("圖片"); 
        menu1.add(m1) ; 
        menu1.add(m2) ; 
        menu1.add(m3) ; 
        menu1.add(m4) ; 
        menu1.add(m5) ; 
        bar.add(menu1); 
        jf.setJMenuBar(bar); 

        jf.setDefaultCloseOperation(3); 
        jf.setVisible(true) ; 
        PaintDemo p =   new PaintDemo(g,jf) ; 

        m1.setActionCommand("畫圓"); 
        m2.setActionCommand("畫線"); 
        m3.setActionCommand("存儲(chǔ)"); 
        m4.setActionCommand("打開(kāi)"); 
        m5.setActionCommand("圖片") ; 

        m1.addActionListener(p); 
        m2.addActionListener(p); 
        m3.addActionListener(p); 
        m4.addActionListener(p); 
        m5.addActionListener(p); 
    } 

 (3)監(jiān)聽(tīng)類

復(fù)制代碼 代碼如下:

class PaintDemo implements MouseListener,ActionListener{ 
ImageIcon img = new ImageIcon("Images/100.gif"); 
BufferedImage bufImage = new BufferedImage(img.getIconWidth(),img.getIconHeight(),BufferedImage.TYPE_INT_RGB); 

 int x,y; 
 int x1,y1; 
 JFrame jf ; 
 Graphics g ; 
 String str ; 
 Color c ; 
 int cr ; 
 int cl[][] = new int[1000][1000] ; 

 
ArrayList<Shape> shapes = new ArrayList<Shape>() ; 
 Random random =new Random() ; 
 int R ; 
 int G ; 
 int B ; 
 public PaintDemo( Graphics g ,JFrame jf){ 
     this.jf = jf; 
     this.g = jf.getGraphics(); 
 } 
    @Override 
    public void mouseClicked(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
           x=e.getX(); 
           y=e.getY(); 

         
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
          x1 = e.getX(); 
          y1 = e.getY(); 
          setColor() ; 
          if(str.equals("畫圓")){ 
             // paintOval(); 

                Shape shape = new OvalShape(x, y, x1, y1,c) ; 
                shape.draw(g); 
                shapes.add(shape) ; 
            } 
            if(str.equals("畫線")){ 
                //paintLine(); 

                Shape shape = new LineShape(x, y, x1, y1,c) ; 
                shape.draw(g); 
                shapes.add(shape) ; 
                /*File  f =new File("D:\\test.txt") ;
                if(f==null){
                    try {
                        f.createNewFile();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }*/ 
            } 

                 

            } 
public void saveFile(String path,ArrayList<Shape> shapes){ 
    try{ 
        //文件輸出流 
        FileOutputStream fos = new FileOutputStream(path) ; 
        //將文件輸出流包裝成可寫基本類型的流 
        DataOutputStream dos = new DataOutputStream(fos) ; 
        dos.writeInt(shapes.size());//寫入隊(duì)列中圖形的個(gè)數(shù) 
        //讀取隊(duì)列 
        for(int i=0;i<shapes.size();i++){ 
            //取出一種形狀 
            Shape shape = shapes.get(i); 
            byte type = shape.type ; 

             
            if(type==0){//根據(jù)type判斷類型,如果是直線 
                System.out.println("開(kāi)始存儲(chǔ)直線") ; 
                dos.writeByte(type); 
                LineShape line = (LineShape) shape ;//強(qiáng)制轉(zhuǎn)換為直線類 
                //寫直線形狀的數(shù)據(jù); 
                int x = line.x ; 
                int y = line.y ; 
                int x1 = line.x1 ; 
                int y1 = line.y1 ; 
                Color color = line.color ; 
                cr = color.getRGB(); 
                dos.writeInt(x); 
                dos.writeInt(y); 
                dos.writeInt(x1); 
                dos.writeInt(y1); 
                dos.writeInt(cr); 
            }else if(type == 1){ 
                dos.writeByte(type); 
                System.out.println("開(kāi)始存儲(chǔ)圓") ; 
                OvalShape oval = (OvalShape) shape ;//強(qiáng)制轉(zhuǎn)換為圓類 
                //寫直線形狀的數(shù)據(jù); 
                int x = oval.x ; 
                int y = oval.y ; 
                int x1 = oval.x1 ; 
                int y1 = oval.y1 ; 
                Color color = oval.color ; 
                cr = color.getRGB() ; 
                dos.writeInt(x); 
                dos.writeInt(y); 
                dos.writeInt(x1); 
                dos.writeInt(y1); 
                dos.writeInt(cr); 
            }else if(type ==2){ 

                dos.writeByte(type) ; 
                picture pl = (picture) shape ; 
                System.out.println("開(kāi)始存儲(chǔ)圖片") ; 
                int width = pl.a ; 
                int height = pl.b ; 
                dos.writeInt(width) ; 
                dos.writeInt(height) ; 
                for(int k=0;k<width;k++){ 
                    for(int j=0;j<height;j++){ 
                        int t = pl.cl[k][j]; 
                        dos.writeInt(t) ; 
                    } 
                } 

            } 
            } 
        dos.flush() ; 
        fos.close(); 
    }catch(Exception e){ 
        e.printStackTrace(); 
    } 

public ArrayList<Shape> readFile(String path){ 

    try{ 
        //創(chuàng)建文件對(duì)象的輸入流 
        FileInputStream fis = new FileInputStream(path); 
        //將文件輸入流包裝成可讀基本類型的流 
        DataInputStream dis = new DataInputStream(fis); 
        //先讀取文件長(zhǎng)度,即總共的形狀個(gè)數(shù) 
        int len = dis.readInt() ; 
        for(int i=0 ;i<len;i++){ 
            byte  type = dis.readByte(); 
            if(type==0){ 
            int a=  dis.readInt(); 
            int b=  dis.readInt(); 
            int c=  dis.readInt(); 
            int d=  dis.readInt(); 
            Color f= new Color(dis.readInt()); 
            LineShape line = new LineShape(a,b,c,d,f); 
            //讀取直線的設(shè)置 

            //將直線存入隊(duì)列 
            shapes.add(line) ; 
            }else if(type == 1){ 

                int a=  dis.readInt(); 
                int b=  dis.readInt(); 
                int c=  dis.readInt(); 
                int d=  dis.readInt(); 
                Color f= new Color(dis.readInt()); 
                System.out.println("開(kāi)始讀取圓") ; 
                OvalShape oval = new OvalShape(a,b,c,d,f); 
                shapes.add(oval) ; 
              }else if(type == 2){ 

                int   a=    dis.readInt(); 
                int    b=   dis.readInt(); 

                  for(int k=0;k<a;k++){ 
                      for(int j=0;j<b;j++){ 

                     cl[k] [j] = dis.readInt(); 

                      } 
                  } 
                  picture   pic = new picture(a,b,cl) ; 
                  shapes.add(pic) ; 
              } 

               
    }}catch(Exception e){ 
        e.printStackTrace(); 
    } 
    return shapes ; 

    @Override 
    public void mouseEntered(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 
    public void setColor(){ 
          R =random.nextInt(255); 
          G =random.nextInt(255); 
          B =random.nextInt(255); 
          c=new Color(R,G,B) ; 
    } 
   /* public void paintLine(){
        setColor();
          g.drawLine(x, y, x1, y1) ;
    }
    public void paintOval(){
        setColor();
        g.drawOval((x+x1)/2,(y+y1)/2,(x1-x)/2 ,(y1-y)/2 );
    }
    */ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
        str = e.getActionCommand() ; 
        jf.addMouseListener(this); 
        if(str.equals("存儲(chǔ)")){ 

            saveFile("D:\\test.txt",shapes) ; 

        } 
        if(str.equals("打開(kāi)")){ 

            readFile("D:\\test.txt") ; 
            for(int i=0;i<shapes.size();i++){ 

                Shape shape = shapes.get(i); 
                if(shape.type ==0){ 
                    System.out.println("確定是圓類型"); 
                    LineShape line = (LineShape)shape ; 
                    line.draw(g); 
                }else if(shape.type==1){ 
                    System.out.println("確定是圓類型"); 
                    OvalShape  oval = (OvalShape)shape ; 
                    oval.draw(g); 
                }else if(shape.type ==2){ 
                    System.out.println("確定是圖片類型"); 
                    picture pl = (picture)shape ; 
                    pl.draw(g) ; 

                } 

                 
            } 
        } 
        if(str.equals("圖片")){ 

        Thread th = new Thread(new Runnable(){ 

            public void run(){ 
                while(true){ 
                    try{ 
                        Thread.sleep(30) ; 

               Graphics g1 = bufImage.getGraphics(); 

               g1.drawImage(img.getImage(), 0,0,null); 
             jf.getGraphics().drawImage(bufImage,100,100,null) ; 
              int width = bufImage.getWidth(); 
               int height = bufImage.getHeight() ; 
           for(int k=0;k<width;k++){ 
                for(int j=0;j<height;j++){ 
                    int p =bufImage.getRGB(k,j) ; 
                    cl[k][j] = p ; 
                         } 
                          } 
              picture pe =new picture(width,height,cl); 
              shapes.add(pe); 

                 
                       }catch(Exception e){ 
                        e.printStackTrace(); 
                         } 
            } 
        }}); 
        th.start(); 
        }         
    } 

相關(guān)文章

  • Java獲取當(dāng)前時(shí)間戳案例詳解

    Java獲取當(dāng)前時(shí)間戳案例詳解

    這篇文章主要介紹了Java獲取當(dāng)前時(shí)間戳案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • MyBatis自動(dòng)生成Where語(yǔ)句

    MyBatis自動(dòng)生成Where語(yǔ)句

    這篇文章主要介紹了MyBatis自動(dòng)生成Where語(yǔ)句的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • spring初始化方法的執(zhí)行順序及其原理分析

    spring初始化方法的執(zhí)行順序及其原理分析

    這篇文章主要介紹了spring初始化方法的執(zhí)行順序及其原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Swing常用組件之文本框和文本區(qū)

    Swing常用組件之文本框和文本區(qū)

    這篇文章主要為大家詳細(xì)介紹了Swing常用組件之文本框(JTestField)和文本區(qū)(JTextArea),Swing是一個(gè)用于開(kāi)發(fā)Java應(yīng)用程序用戶界面的開(kāi)發(fā)工具包,本文開(kāi)始帶大家學(xué)習(xí)Swing
    2016-05-05
  • Mac下設(shè)置Java默認(rèn)版本的方法

    Mac下設(shè)置Java默認(rèn)版本的方法

    今天工作的時(shí)候發(fā)現(xiàn)了一個(gè)錯(cuò)誤,提示java版本太低,無(wú)法啟動(dòng)!想起自己裝過(guò)高版本的Java,但是卻沒(méi)有默認(rèn)啟動(dòng),從網(wǎng)上找了一些資料,整理下現(xiàn)在分享給大家,有需要的可以參考借鑒。
    2016-10-10
  • 網(wǎng)易Java程序員兩輪面試 請(qǐng)問(wèn)你能答對(duì)幾個(gè)?

    網(wǎng)易Java程序員兩輪面試 請(qǐng)問(wèn)你能答對(duì)幾個(gè)?

    為大家分享網(wǎng)易Java程序員兩輪面試題,考考大家,這些問(wèn)題你能答對(duì)幾個(gè)?
    2017-11-11
  • Java設(shè)計(jì)模式編程之解釋器模式的簡(jiǎn)單講解

    Java設(shè)計(jì)模式編程之解釋器模式的簡(jiǎn)單講解

    這篇文章主要介紹了Java設(shè)計(jì)模式編程之解釋器模式的講解,解釋器設(shè)計(jì)模式要注意其引發(fā)的性能問(wèn)題,需要的朋友可以參考下
    2016-04-04
  • jvm運(yùn)行原理以及類加載器實(shí)例詳解

    jvm運(yùn)行原理以及類加載器實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于jvm運(yùn)行原理以及類加載器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • elasticsearch bucket 之rare terms聚合使用詳解

    elasticsearch bucket 之rare terms聚合使用詳解

    這篇文章主要為大家介紹了elasticsearch bucket 之rare terms聚合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)

    java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)

    這篇文章主要為大家介紹了java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)的詳細(xì)使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03

最新評(píng)論