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

首先上几张效果图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
第一先写一个圆的实体类,这个实体类必须要序列号才能使用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();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值