想要创造一个只有自己能看懂的文件吗?那就是对数据加密吧,下面分享一个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 for(int j=0;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
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,
以上就是java加密解密示例分享的详细内容,更多请关注0133技术站其它相关文章!