Serialises a Stroke object : Stroke « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » 2D Graphics GUI » StrokeScreenshots 
Serialises a Stroke object
  


import java.awt.BasicStroke;
import java.awt.Stroke;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/* 
 * JCommon : a free general purpose class library for the Java(tm) platform
 
 *
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 
 * Project Info:  http://www.jfree.org/jcommon/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 
 * ------------
 * IOUtils.java
 * ------------
 * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: IOUtils.java,v 1.8 2009/01/22 08:34:58 taqua Exp $
 *
 * Changes
 * -------
 * 26-Jan-2003 : Initial version
 * 23-Feb-2003 : Documentation
 * 25-Feb-2003 : Fixed Checkstyle issues (DG);
 * 29-Apr-2003 : Moved to jcommon
 * 04-Jan-2004 : Fixed JDK 1.2.2 issues with createRelativeURL;
 *               added support for query strings within these urls (TM);
 */


public class Main {
  /**
   * Returns <code>true</code> if a class implements <code>Serializable</code>
   * and <code>false</code> otherwise.
   *
   @param c  the class.
   *
   @return A boolean.
   */
  public static boolean isSerializable(final Class c) {
      /**
      final Class[] interfaces = c.getInterfaces();
      for (int i = 0; i < interfaces.length; i++) {
          if (interfaces[i].equals(Serializable.class)) {
              return true;
          }
      }
      Class cc = c.getSuperclass();
      if (cc != null) {
          return isSerializable(cc);
      }
       */
      return (Serializable.class.isAssignableFrom(c));
  }

  /**
   * Reads a <code>Stroke</code> object that has been serialised by the
   {@link SerialUtilities#writeStroke(Stroke, ObjectOutputStream)} method.
   *
   @param stream  the input stream (<code>null</code> not permitted).
   *
   @return The stroke object (possibly <code>null</code>).
   *
   @throws IOException  if there is an I/O problem.
   @throws ClassNotFoundException  if there is a problem loading a class.
   */
  public static Stroke readStroke(final ObjectInputStream stream)
      throws IOException, ClassNotFoundException {

      if (stream == null) {
          throw new IllegalArgumentException("Null 'stream' argument.");
      }
      Stroke result = null;
      final boolean isNull = stream.readBoolean();
      if (!isNull) {
          final Class c = (Classstream.readObject();
          if (c.equals(BasicStroke.class)) {
              final float width = stream.readFloat();
              final int cap = stream.readInt();
              final int join = stream.readInt();
              final float miterLimit = stream.readFloat();
              final float[] dash = (float[]) stream.readObject();
              final float dashPhase = stream.readFloat();
              result = new BasicStroke(
                  width, cap, join, miterLimit, dash, dashPhase
              );
          }
          else {
              result = (Strokestream.readObject();
          }
      }
      return result;

  }

  /**
   * Serialises a <code>Stroke</code> object.  This code handles the
   * <code>BasicStroke</code> class which is the only <code>Stroke</code>
   * implementation provided by the JDK (and isn't directly
   * <code>Serializable</code>).
   *
   @param stroke  the stroke object (<code>null</code> permitted).
   @param stream  the output stream (<code>null</code> not permitted).
   *
   @throws IOException if there is an I/O error.
   */
  public static void writeStroke(final Stroke stroke,
                                 final ObjectOutputStream stream)
      throws IOException {

      if (stream == null) {
          throw new IllegalArgumentException("Null 'stream' argument.");
      }
      if (stroke != null) {
          stream.writeBoolean(false);
          if (stroke instanceof BasicStroke) {
              final BasicStroke s = (BasicStrokestroke;
              stream.writeObject(BasicStroke.class);
              stream.writeFloat(s.getLineWidth());
              stream.writeInt(s.getEndCap());
              stream.writeInt(s.getLineJoin());
              stream.writeFloat(s.getMiterLimit());
              stream.writeObject(s.getDashArray());
              stream.writeFloat(s.getDashPhase());
          }
          else {
              stream.writeObject(stroke.getClass());
              stream.writeObject(stroke);
          }
      }
      else {
          stream.writeBoolean(true);
      }
  }
}

   
    
  
Related examples in the same category
1.Dashed rectangleDashed rectangle
2.A dashed stroke
3.Stroking or Filling with a Texture
4.Basic strokeBasic stroke
5.Thick stroke demoThick stroke demo
6.Dashed strokeDashed stroke
7.Stroke with iron effectStroke with iron effect
8.Smokey effectSmokey effect
9.Custom StrokesCustom Strokes
10.Changing the Thickness of the Stroking Pen
11.Tries to deduct the stroke-type from the given stroke object.
12.Tries to extract the stroke-width from the given stroke object.
13.A component for choosing a stroke from a list of available strokes.
14.Cancel the effects of the zoom on a particular Stroke
15.This program demonstrates different stroke types.This program demonstrates different stroke types.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.