当前位置: 移动技术网 > IT编程>开发语言>Java > java加密解密示例分享

java加密解密示例分享

2019年07月22日  | 移动技术网IT编程  | 我要评论
(1)先定义要实现的类,我先定义了一个抽象类复制代码 代码如下://图形类  abstract class  shape{   &n

(1)先定义要实现的类,我先定义了一个抽象类

复制代码 代码如下:

//图形类 
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)总方法类

复制代码 代码如下:

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("图片的存储"); 
        jf.setbackground(color.white ); 
        jmenubar bar = new jmenubar() ; 
        jmenu menu1 = new jmenu("选项") ; 
        jmenuitem m1 = new jmenuitem("画圆"); 
        jmenuitem m2 = new jmenuitem("画线"); 
        jmenuitem m3 = new jmenuitem("存储"); 
        jmenuitem m4 = new jmenuitem("打开"); 
        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("存储"); 
        m4.setactioncommand("打开"); 
        m5.setactioncommand("图片") ; 

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

 (3)监听类

复制代码 代码如下:

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());//写入队列中图形的个数 
        //读取队列 
        for(int i=0;i<shapes.size();i++){ 
            //取出一种形状 
            shape shape = shapes.get(i); 
            byte type = shape.type ; 

             
            if(type==0){//根据type判断类型,如果是直线 
                system.out.println("开始存储直线") ; 
                dos.writebyte(type); 
                lineshape line = (lineshape) shape ;//强制转换为直线类 
                //写直线形状的数据; 
                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("开始存储圆") ; 
                ovalshape oval = (ovalshape) shape ;//强制转换为圆类 
                //写直线形状的数据; 
                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("开始存储图片") ; 
                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{ 
        //创建文件对象的输入流 
        fileinputstream fis = new fileinputstream(path); 
        //将文件输入流包装成可读基本类型的流 
        datainputstream dis = new datainputstream(fis); 
        //先读取文件长度,即总共的形状个数 
        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); 
            //读取直线的设置 

            //将直线存入队列 
            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("开始读取圆") ; 
                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("存储")){ 

            savefile("d:\\test.txt",shapes) ; 

        } 
        if(str.equals("打开")){ 

            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(); 
        }         
    } 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网