javaswing画一个圆并且保存到文件

首先上几张效果图

javaswing画一个圆并且保存到文件_第1张图片
javaswing画一个圆并且保存到文件_第2张图片

javaswing画一个圆并且保存到文件_第3张图片
第一先写一个圆的实体类,这个实体类必须要序列号才能使用IO流中的对象存储。

    import java.io.Serializable;
    
    public class Cir implements Serializable {
        int x;
        int y;
        int radio;
        int color;
    
        public Cir(int x, int y, int radio) {
            this.x = x;
            this.y = y;
            this.radio = radio;
            this.color = color;
        }
    
        public int getX() {
    
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public int getRadio() {
            return radio;
        }
    
        public void setRadio(int radio) {
            this.radio = radio;
        }
    
        public int getColor() {
            return color;
        }
    
        public void setColor(int color) {
            this.color = color;
        }
    }

接下来是写一个画版和一些监听事件。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;


public class pane extends JFrame implements ActionListener
{   private  JPanel jp1;
    private  JPanel jp2;
    private  JButton load;
    private  JButton save;
    private  JButton l2;
    private  JButton clear;
    mianban mb;
    public pane()
    {
        //组建定义
        jp1 = new JPanel();
        mb = new mianban();
        load = new JButton("生成");
        save = new JButton("save");
        clear = new JButton("clear");
        l2 = new JButton("load");

        l2.addActionListener(this);
        load.addActionListener(this);
        save.addActionListener(this);
        clear.addActionListener(this);
        load.setBounds(0,0,30,30);
        save.setBounds(0,400,30,30);
        clear.setBounds(400,0,30,30);

        jp1.add(load);
        jp1.add(save);
        jp1.add(clear);
        jp1.add(l2);

        this.setLayout(new BorderLayout());
        this.add(jp1,BorderLayout.NORTH);
        this.add(mb,BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(600, 400);
        this.setResizable(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //生成圆的的监听事件
        if (e.getSource() == load){
            //重画圆
            mb.repaint();
        }else if(e.getSource() == save){
            try {
                //object 保存实体类圆
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Dier\\Desktop\\java网络编程\\io\\src\\Ac4\\d.data"));
                Cir c = new Cir(mb.x1,mb.y1,mb.r1);
                os.writeObject(c);
                os.close();
            } catch (IOException a) {
                a.printStackTrace();
            }
        }else if (e.getSource() == l2){
            try {
                //从文件中取出实体类,再将圆中赋值重画。
                ObjectInputStream os = new ObjectInputStream(new FileInputStream("C:\\Users\\Dier\\Desktop\\java网络编程\\io\\src\\Ac4\\d.data"));
                Cir c = (Cir) os.readObject();
                System.out.println(c.x);
                mb.x = c.x;
                mb.y = c.y;
                mb.r = c.radio;
                mb.repaint();
                os.close();

            } catch (FileNotFoundException a) {
                a.printStackTrace();
            } catch (IOException a) {
                a.printStackTrace();
            } catch (ClassNotFoundException a) {
                a.printStackTrace();
            }
        }else if(e.getSource() == clear){
            mb.removeAll();
            mb.getGraphics().clearRect(0,0,600,400);
        }
    }


    class mianban extends JPanel
    {
        int x,y,r=0;
        Color color;
        int x1,y1,r1;



        public void paint(Graphics g)
        {
            super.paint(g);
            if (r==0) {
                 r1 = (int) (Math.random() * 200 + 1);
                 x1 =(int) (Math.random() * 100 + 30);
                 y1 = (int) (Math.random() * 100 + 30);
                g.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
                g.drawOval(x1,y1 , r1, r1);        //第一层
            }else {
                g.setColor(color);
                g.drawOval(x,y,r,r);
                r=0;
            }
        }
    }
    public static void main(String args[])
    {

    pane p = new pane();

    }
}

你可能感兴趣的:(Java)