java画板
创建java画板
?
画板的实现步骤
a.定义一个画板类,让该类继承自JFrame
?
b.定义一个初始化界面的方法,方法中要初始化JFrame的属性
?
c.给窗体设置菜单栏的方法
?
d.创建一个窗体的工具面板类,然后将该类添加到窗体上的左侧
给工具面板上的每一个功能按钮添加动作监听器,并且绑定事件
处理类对象。
?
e.创建一个颜色的面板类,然后将该类添加到窗体上的底部
要给窗体添加颜色的按钮或者标签,并且绑定事件处理对象。
?
f.创建一个画图形的面板类,然后将对象添加到窗体的中间。
1.将事件添加到面板类里边。
2.在画板类中给画图形的面板类添加监听器,并且绑定事件处理
类对象
?
?
?
1.建立程序入口(main函数)
? ?public class Main {
?
? ? 当然,也可以单独设计菜单类
? ??public class MenuPanel extends JPanel{
private JPanel panel=new JPanel(); private JPanel panel1=new JPanel(); public void show_colorPanel(){ panel.setLayout(new FlowLayout(FlowLayout.LEFT)); panel.setPreferredSize(new Dimension(30,30)); panel.setLayout(null); panel.setBackground(Color.DARK_GRAY); JButton button1=new JButton(); button1.setBounds(5, 5, 15, 15); JButton button2=new JButton(); button2.setBounds(5, 5, 15, 15); panel.add(button1); panel.add(button2); panel1.setPreferredSize(new Dimension(210,30)); panel1.setBackground(Color.LIGHT_GRAY); panel1.setLayout(new GridLayout(2,14,2,2)); for(int i=0;i<28;i++){ JButton button3=new JButton(); button3.setSize(new Dimension(15,15)); panel1.add(button3); } this.add(panel); this.add(panel1); }}?
6.保存
? ?首先得到保存路径,
? ?然后截取路径,
? ?再用输入输出流,
? ?把其读入数组中,
? ?再将其写到指定的文件夹中,
? ?实现保存。
?
? ?使用java.io.DataOutputStream可直接输出字符型、整形、布尔型等,但之前要将基本流包装成特殊流。
?
? ?执行完毕时,要记得关闭流。
?
? ? public void saveFile(String path,ArrayList<Shape> shapes){
? ? ? try{
? ? ? //文件输出流
? ? ? FileOutputStream fos=new FileOutputStream(path);
? ? ? //将文件输出流包装成可写基本类型的流
? ? ? DataOutputStream dos=new DataOutputStream(fos);
? ? ? //先写入队列中图形个数
? ? ? dos.writeInt(shapes.size());
? ? ? //读取队列
? ? ? for(int i=0;i<shapes.size();i++){
? ? ? //取出一种形状
? ? ? Shape shape=shapes.get(i);
? ? ? byte type=shape.type;
? ? ? //写形状类型
? ? ? dos.writeByte(type);
? ? ? //根据type判断类型如果是直线
? ? ? if(type==0){
? ? ?//强制类型转换成直线类
? ? ?ImpLine line=(ImpLine) shape;
? ? ?//写直线形状的数据
? ? ?int x1=line.x1;
? ? ?int y1=line.y1;
? ? ?int x2=line.x2;
? ? ?int y2=line.y2;
? ? ?dos.writeInt(x1);
? ? ?dos.writeInt(y1);
? ? ?dos.writeInt(x2);
? ? ?dos.writeInt(y2);
? ? ? }
? ? ?
? ? ? }
? ? ? dos.flush();
? ? ? //关闭输出流
? ? ? fos.close();
? ? ? }catch(Exception ef){
? ? ? ? ?ef.printStackTrace();
? ? ? }
? ? }
?
? ? //读文件,将文件中的内容存入队列shapes,并返回
? ? public ArrayList<Shape> readFile(String path){
? ? ? //创建一个队列用来保存从文件中读取到的数据
? ? ? ArrayList<Shape> shapes=new ArrayList<Shape>();
? ? ? try{
? ? ? //创建文件对象的输入流
? ? ? FileInputStream fis=new FileInputStream(path);
? ? ? //将文件输入流包装成可读基本类型的流
? ? ? DataInputStream dis=new DataInputStream(fis);
? ? ? //先读取长度,即总共的形状个数
? ? ? int len=dis.readInt();
? ? ? //根据读取到的长度,得到循环次数
? ? ? for(int i=0;i<len;i++){
? ? ? //读图形类型
? ? ? byte type=dis.readByte();
? ? ? if(type==0){
? ? ?//如果type是0,表示接下来要读取的是直线的数据,创建直线对象
? ? ?LineShape line=new LineShape();
? ? ?//将读取的数据设置为直线的属性值
? ? ?line.type=type;
? ? ?line.x1=dis.readInt();
? ? ?line.y1=dis.readInt();
? ? ?line.x2=dis.readInt();
? ? ?line.y2=dis.readInt();
? ? ?//将直线存入队列
? ? ?shapes.add(line);
? ? ? }
? ? ? }?
? ? ?}catch(Exception e){
? ? e.printStackTrace();
? ? ? }
? ? ?return shapes;
? ? ??
? ? }
?
?
?
7.BMP保存
? ?每种文件都是按照特定的格式进行存储的,与后缀名无关
? ?BMP文件以存储方式为24位真彩色为例
?
? ?结构:a.BMP文件头(14字节) ?仿BMP文件头(两个int数据:width和height,占8字节)
? ? ? ? ? ? b.位图信息头(40字节)
? ? ? ? ? ? c.颜色表(由颜色索引数决定)
? ? ? ? ? ? d.位图数据(由图像尺寸决定)
? ? ? ? ? ? ? ?仿BMP的位图数据(width*height个int值)
?