java解密_java加密解密示例分享

这篇博客通过示例展示了如何使用Java进行图形绘制,包括直线、圆形和自定义图片类,并实现了图形的存储和读取功能。通过监听事件处理用户交互,使用DataOutputStream和DataInputStream进行文件操作,实现了不同类型图形数据的序列化和反序列化。
摘要由CSDN通过智能技术生成

(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 shapes = new 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, 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 shapes){

try{

//文件输出流

FileOutputStream fos = new FileOutputStream(path) ;

//将文件输出流包装成可写基本类型的流

DataOutputStream dos = new DataOutputStream(fos) ;

dos.writeInt(shapes.size());//写入队列中图形的个数

//读取队列

for(int i=0;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

for(int j=0;j

int t = pl.cl[k][j];

dos.writeInt(t) ;

}

}

}

}

dos.flush() ;

fos.close();

}catch(Exception e){

e.printStackTrace();

}

}

public ArrayList readFile(String path){

try{

//创建文件对象的输入流

FileInputStream fis = new FileInputStream(path);

//将文件输入流包装成可读基本类型的流

DataInputStream dis = new DataInputStream(fis);

//先读取文件长度,即总共的形状个数

int len = dis.readInt() ;

for(int i=0 ;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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值