java IO流的简单使用——教你开发一个属于自己的图片格式

java IO流的简单使用——教你写一个属于自己的图片格式

说在前面:本次学习需要提前学会:java窗口的编写、画图板的制作;

1、 简单的I/O知识点:
(1)、保存一个数字或字符到指定文件的简单步骤:

第一步,建立一个OutputStream类以及DataOutputStream类:

OutputStream OutputStream类名= new FileOutputStream(文件路径以及文件名,没有则会自动创建);
DataOutputStream DataOutputStream类名 = new DataOutputStream(OutputStream对象名);

第二步,使用writeChar方法写入字符,使用writeInt方法写入整形数字:

   DataOutputStream对象名.writeChar('|');
   DataOutputStream对象名.writeInt(1);

第三步,刷新,讲流里面缓存的数据写入文件:

        DataOutputStream对象名.flush();

第四步,关闭文件:

   DataOutputStream对象名.close();

(2)、打开一个数字或字符到指定文件的简单步骤:

第一步,建立一个InputStream类以及DataInputStream类:

OutputStream InputStream类名= new FileInputStream(文件路径以及文件名,没有则会自动创建);

DataInputStream DataOutputStream类名 = new DataInputStream(OutputStream对象名);
第二步,使用writeChar方法写入字符,使用writeInt方法写入整形数字:

   DataInputStream对象名.readChar();
   DataInputStream对象名.readInt();

第三步,关闭文件:

   DataInStream对象名.close();

2、 写一个属于自己的图片格式
(1)、写一个窗口,添加两个按钮和两个文本框,两个按钮的文字分别为保存、打开,如下:

—DraFrame.java—


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class DrawFrame {
    public static void main(String [] args){
        showUI();
    }   
    public static void showUI(){
        JFrame jsf = new JFrame();
        jsf.setSize(1000,900);
        jsf.setTitle("画板");
        jsf.setDefaultCloseOperation(3);   //设置退出程序方法
        jsf.setLocationRelativeTo(null);   //设置居中显示
     //   jsf.setResizable(false);
        java.awt.FlowLayout jaf = new java.awt.FlowLayout();
        jsf.setLayout(jaf);
        JTextField jst_Name = new JTextField("请输入文件名");
        jsf.add(jst_ Name);
        JTextField jst_Path = new JTextField("请输入文件路径及文件名");
        jsf.add(jst_Path);
        JButton jsb_Sa = new JButton("保存");
        jsf.add(jsb_Sa);
        JButton jsb_Op = new JButton("打开");
        jsf.add(jsb_Op);
        jsf.setVisible(true);
    }
}

(2)、为窗口添加鼠标监听器,实现画直线功能:
—DraFrame.java—

import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class DrawFrame {

    public static void main(String [] args){
        showUI();
    }   
    public static void showUI(){
        JFrame jsf = new JFrame();
        jsf.setSize(1000,900);
        jsf.setTitle("画板");
        jsf.setDefaultCloseOperation(3);   //设置退出程序方法
        jsf.setLocationRelativeTo(null);   //设置居中显示
     //   jsf.setResizable(false);
        java.awt.FlowLayout jaf = new java.awt.FlowLayout();
        jsf.setLayout(jaf);
        JTextField jst_Name = new JTextField("请输入文件名");
        jsf.add(jst_IfsX);
        JTextField jst_Path = new JTextField("请输入路径及文件名");
        jsf.add(jst_Path);
        JButton jsb_Sa = new JButton("保存");
        jsf.add(jsb_Sa);
        JButton jsb_Op = new JButton("打开");
        jsf.add(jsb_Op);
        jsf.setVisible(true);
        Graphics g = jsf.getGraphics();
        DrawListener d = new DrawListener(g);
        jsf.addMouseListener(d);
    }
}

— DrawListener.java—

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Graphics;
public class DrawListener implements MouseListener {
    private int x1,x2,y1,y2;
    Graphics g;
    public DrawListener(Graphics g) {
        this.g = g;
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        g.drawLine(x1, y1, x2, y2);
    }

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

    }

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

(3)、写一个类,在画线时将点的位置数据保存,同时在里面写一个画线的方法:
—PointInfo.java—


import java.awt.Graphics;

public class PointInfo {
    Graphics g;
    private int x1,y1,x2,y2;
    PointInfo(int x1, int y1, int x2, int y2){
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
    }
    public void draw(){
        g.drawLine(x1, y1, x2, y2);
    }
}

(4)、在监听器类里写一个PointInfo:的对象数组,在画线时将各点数据保存:

—DrawListener.java—


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Graphics;
public class DrawListener implements MouseListener,ActionListener {
    private int count = 0;
    PointInfo[] point = new PointInfo[100];
    private int x1,x2,y1,y2;
    Graphics g;

    public DrawListener(Graphics g) {
        this.g = g;
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        g.drawLine(x1, y1, x2, y2);
        PointInfo p = new PointInfo(x1, y1, x2, y2);
        point[count++] = p;
    }

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

    }

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

    }
}

(4)、为保存按钮添加事件监听器,实现保存功能,为打开按钮添加事件监听器,实现打开功能:


---DrawFrame.java---

import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class DrawFrame {

    public static void main(String [] args){
        showUI();
    }   
    public static void showUI(){
        JFrame jsf = new JFrame();
        jsf.setSize(1000,900);
        jsf.setTitle("画板");
        jsf.setDefaultCloseOperation(3);   //设置退出程序方法
        jsf.setLocationRelativeTo(null);   //设置居中显示
     //   jsf.setResizable(false);
        java.awt.FlowLayout jaf = new java.awt.FlowLayout();
        jsf.setLayout(jaf);
        JTextField jst_Name = new JTextField("请输入文件名");
        jsf.add(jst_Name);
        JTextField jst_Path = new JTextField("请输入路径及文件名");
        jsf.add(jst_Path);
        JButton jsb_Sa = new JButton("保存");
        jsf.add(jsb_Sa);
        JButton jsb_Op = new JButton("打开");
        jsf.add(jsb_Op);
        jsf.setVisible(true);
        Graphics g = jsf.getGraphics();
        DrawListener d = new DrawListener(g,jst_Name,jst_Path);
        jsb_Sa.addActionListener(d);
        jsb_Op.addActionListener(d);
        jsf.addMouseListener(d);
    }
}

—DrawListener.java—


import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JTextField;

public class DrawListener implements MouseListener,ActionListener {
    private int count = 0;
    PointInfo[] point = new PointInfo[100];
    private int x1,x2,y1,y2;
    Graphics g;
    String Bt;
    private JTextField TxN;
    private JTextField TxP;

    public DrawListener(Graphics g,JTextField t1,JTextField t2) {
        this.g = g;
        TxN = t1;
        TxP = t2;
    }

    public void WriteFile() throws IOException {
        if (Bt.equals("保存")) {
            String fileName = TxN.getText();
            File f = new File("D://" + fileName + ".ant");//后面的点后的即为文件格式,可以随意设置
            OutputStream out = new FileOutputStream(f);
            DataOutputStream dOut = new DataOutputStream(out);
            for (int i = 0; i < point.length; i++) {
                //将个坐标的位置存入,用'|'隔开
                dOut.writeChar('|');  //写一个斜杠
                dOut.writeInt(point[i].x1); //写x1的坐标
                dOut.writeChar('|'); 
                dOut.writeInt(point[i].y1);
                dOut.writeChar('|'); 
                dOut.writeInt(point[i].x2);
                dOut.writeChar('|'); 
                dOut.writeInt(point[i].y2);
                out.flush();
                dOut.flush();
            }
            out.close();
            dOut.close();
        }
    }
    public void ReadFile() throws IOException {
        if (Bt.equals("打开")) {
            count = 0 ;
            String filePath = TxP.getText();
            InputStream in = new FileInputStream(filePath);
            DataInputStream dIn = new DataInputStream(in);
            while (true) {
            //  dIn.readChar();
                if (dIn.readChar() == -1) {
                    System.out.println("读取完毕");
                    break;
                }
                int x1 = dIn.readInt();
                dIn.readChar();
                int y1 = dIn.readInt();
                dIn.readChar();
                int x2 = dIn.readInt();
                dIn.readChar();
                int y2 = dIn.readInt();
                PointInfo p =new PointInfo(x1, y1, x2, y2);
                p.draw(g);
            }
            in.close();
            dIn.close();

            // System.out.println(a);
            // dIn.
        }
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Bt = e.getActionCommand();
        try {
            ReadFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            WriteFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        g.drawLine(x1, y1, x2, y2);
        PointInfo p = new PointInfo(x1, y1, x2, y2);
        point[count++] = p;
    }

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

    }

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

    }
}

—PointInfo.java—


import java.awt.Graphics;

public class PointInfo {
    int x1,y1,x2,y2;
    PointInfo(int x1, int y1, int x2, int y2){
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
    }
    public void draw(Graphics g){
        g.drawLine(x1, y1, x2, y2);
    }
}

这样即可实现:在第一个文本框里输入文件名点保存,指定路径便会出点指定文件:

在第二个文本框点输入路径及文件名,点打开即可看到响应图形:

更多图像保存的方法和上面类似,按实际情况稍作更改和设计读写规则即可。

猜你喜欢

转载自blog.csdn.net/weixin_41475710/article/details/82256535