读书人

java简略画板

发布时间: 2012-11-01 11:11:32 作者: rapoo

java简单画板

java简略画板

?

?

实现步骤:

??????1.? 主界面

?????????? 主要方法:

?????????? 继承JFrame类,并获得其内容面板

?????????? getContentPane();

??????????

?????????? 获得内容面板的画布对象:

?????????? Graphic g=dPane.getGrahpics();

?

??????????调用重绘方法

??????????repaint();

????????? 自动调用内部类paint()方法。实现重绘。

??????

????? 2.? 添加按钮监听器

???????????继承鼠标监听器和事件监听器。

?

?????????? 重写actionPerformed()等方法

?????????

????? 3. 自定义保存与打开按钮时响应的方法

?????????

?

?

?

?

?

主界面:MainFrame类

?

public class SaveOpen {/** * 保存到文件 * @throws IOException  */public void save(String path,List<Graph> list) throws IOException{//输出流FileOutputStream fos=new FileOutputStream(path);//将文件输出流包装成可写基本类型的流DataOutputStream dos=new DataOutputStream(fos);System.out.println("--------------------->"+list.size());dos.writeInt(list.size());for(int i=0;i<list.size();i++){Graph graph=list.get(i);//获取选择图像String item=graph.getItem();//定义一个图形变量int type=0;//得到坐标值int x1=graph.getX1();int y1=graph.getY1();int x2=graph.getX2();int y2=graph.getY2();//判断为圆if(item.equals("圆")){type=2;}//判断为矩形else if(item.equals("矩形")){type=1;}//判断为曲线或者直线else{type=0;}dos.writeInt(type);dos.writeInt(x1);dos.writeInt(y1);dos.writeInt(x2);dos.writeInt(y2);//写入颜色int rgb=graph.getColor().getRGB();dos.writeInt(rgb);}dos.flush();fos.close();}/** * 读取文件 * @throws IOException  */public List<Graph> open(String path) throws IOException{List<Graph> list=new ArrayList<Graph>();//输入流InputStream fis=new FileInputStream(path);DataInputStream dis=new DataInputStream(fis);//长度int len=dis.readInt();System.out.println("===>"+len);//循环输出for(int i=0;i<len;i++){int type=dis.readInt();int x1=dis.readInt();int y1=dis.readInt();int x2=dis.readInt();int y2=dis.readInt();int rgb=dis.readInt();Color color=new Color(rgb);if(type==0){String item="直线";//实例化Graph对象Graph graph=new Graph(color,item,x1,y1,x2,y2);list.add(graph);}else if(type==1){String item="矩形";//实例化Graph对象Graph graph=new Graph(color,item,x1,y1,x2,y2);list.add(graph);}else if(type==2){String item="圆";//实例化Graph对象Graph graph=new Graph(color,item,x1,y1,x2,y2);list.add(graph);}}dis.close();return list;}}

?

?

?????

?????????

?

?

读书人网 >编程

热点推荐