java加密解密示例分享

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

(0)

相关推荐

  • java中DES加密解密

    废话不多说,直接奉上代码: 复制代码 代码如下: package com.eabax.plugin.yundada.utils; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.Invalid

  • javascript将url中的参数加密解密代码

    今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码: 复制代码 代码如下: <SCRIPT LANGUAGE="JavaScript">    <!-- Begin    function Encrypt(str, pwd) {        if(str=="")return "";        str = escape(str);        if(!pwd

  • 实例讲解java的纯数字加密解密

    我们都知道,在用户添加信息时,一些比较敏感的信息,如身份证号,手机号,用户的登录密码等信息,是不能直接明文存进数据库的.今天我们就以一个具体的例子来说明一下纯数字的java加密解密技术. 一般我们从页面获取到用户添加的信息之后,进行加密然后存入到数据库.需要比对信息时,加密之后的用户信息我们看不懂,所以对应的我们就要用解密技术.其实软考中对加密解密技术进行了很全面的说明,这里我们就用一个比较简单的实例来说明一下. 我们可能会习惯在service层进行加密,这个没有太强制的要求.下面我们就具体来看

  • PHP、Java des加密解密实例

    des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库 下面是加密解密的实例 复制代码 代码如下: $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);  $key = "This is a very secre

  • java使用des加密解密示例分享

    复制代码 代码如下: import java.security.Key;import java.security.SecureRandom;import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvPar

  • java常用工具类之DES和Base64加密解密类

    一.DES加密和解密 package com.itjh.javaUtil; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecExc

  • java加密解密示例分享

    (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 LineSh

  • 带密匙的php加密解密示例分享

    复制代码 代码如下: <?php $id = "http://www.jb51.net";$token = encrypt($id, 'E', 'jb51');echo '加密:'.encrypt($id, 'E', 'jb51');echo '<br />';echo '解密:'.encrypt($token, 'D', 'jb51'); /***************************************************************

  • Java加密解密和数字签名完整代码示例

    常见的加密算法 基本的单向加密算法: BASE64严格地说,属于编码格式,而非加密算法 MD5(MessageDigestalgorithm5,信息摘要算法) SHA(SecureHashAlgorithm,安全散列算法) HMAC(HashMessageAuthenticationCode,散列消息鉴别码) 复杂的对称加密(DES.PBE).非对称加密算法: DES(DataEncryptionStandard,数据加密算法) PBE(Password-basedencryption,基于密码

  • Java加密解密工具(适用于JavaSE/JavaEE/Android)

    本文实例为大家分享了一个适用于JavaSE/JavaEE/Android的Java加密解密工具,供大家学习,具体内容如下 package longshu.utils.security; import java.lang.reflect.Method; import java.security.InvalidKeyException; import java.security.Key; import java.security.MessageDigest; import java.security

  • 详解Java 加密解密和数字签名问题

    在做项目中,只要涉及敏感信息,或者对安全有一定要求的场景,都需要对数据进行加密.在Java中原生API即可实现对称加密与非对称加密,并支持常用的加密算法. 对称加密 对称加密使用单钥完成加解密,加密和解密采用相同的密钥.对称加密的速度快,常用于大量数据进行加密.主流的算法有:AES,3DES. 生成3DES密钥 <em>/** * 对称加密-3DES算法,取代旧的DES */</em> SecretKey desKey = KeyGenerator.getInstance(&quo

  • php基于openssl的rsa加密解密示例

    本文实例讲述了php基于openssl的rsa加密解密.分享给大家供大家参考,具体如下: <?php $config = array( //"config" =>"D:/phpserver/Lighttpd/openssl.cnf", //'config' =>'D:/phpStudy/Lighttpd/OpenSSL.cnf', 'private_key_bits' => 1024, // Size of Key. 'private_key

  • Java 加密解密基础分类及模式归纳整理

    Java  加密解密基础: 密码学是研究编制密码和破译密码的技术科学.研究密码变化的客观规律,应用于编制密码以保守通信秘密的,称为编码学:应用于破译密码以获取通信情报的,称为破译学,总称密码学. 密码学常用术语 明文: 待加密数据. 密文: 明文经过加密后数据. 加密: 将明文转换为密文的过程. 加密算法: 将明文转换为密文的转换算法. 加密密钥: 通过加密算法进行加密操作的密钥. 解密: 将密文转换为铭文的过程. 解密算法: 将密文转换为明文的转换算法. 解密密钥: 通过解密短发进行解密操作的

  • Java AES256加密解密示例代码

    Java支持许多安全的加密算法,但是其中一些功能较弱,无法在安全性要求很高的应用程序中使用.例如,数据加密标准(DES)加密算法被认为是高度不安全的.今天介绍一下AES 256加密解密. 什么是 AES 256? 高级加密标准 (英语:Advanced Encryption Standard,缩写:AES ),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用. AES是一种对称加密算法.它旨在易于在硬件和软

  • 兼容PHP和Java的des加密解密代码分享

    php代码: <?php class DES { var $key; var $iv; //偏移量 function DES($key, $iv=0) { $this->key = $key; if($iv == 0) { $this->iv = $key; } else { $this->iv = $iv; } } //加密 function encrypt($str) { $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MO

随机推荐