xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ilian Alekov <iale...@tlogica.com>
Subject java.lang.ClassCastException
Date Thu, 16 Aug 2001 08:56:52 GMT
I am trying to load a SVG file and then to paint it to a Graphics2D, but
I'm having the following problem:

java.lang.ClassCastException
	org.apache.batik.gvt.GraphicsNode
org.apache.batik.bridge.GVTBuilder.build(org.apache.batik.bridge.BridgeContext,
org.w3c.dom.Document)
	void package3.MyFrame.renderSVGDocument(org.w3c.dom.Document, int, int)
	void
package3.MyFrame.jMenuItem1_actionPerformed(java.awt.event.ActionEvent)
	void package3.MyFrame$1.actionPerformed(java.awt.event.ActionEvent)
	void
javax.swing.AbstractButton.fireActionPerformed(java.awt.event.ActionEvent)
	void
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(java.awt.event.ActionEvent)
	void
javax.swing.DefaultButtonModel.fireActionPerformed(java.awt.event.Actio...

I don't know where is the problem?!?
here is my source file:

package package3;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//import org.xml.sax.Parser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import java.io.*;
import java.net.URL;

import org.w3c.dom.svg.*;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import java.awt.geom.Dimension2D;
import java.awt.geom.AffineTransform;

import org.apache.batik.gvt.event.EventDispatcher;

//import org.apache.batik.bridge.*;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.BridgeExtension;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.UserAgent;


import org.apache.batik.gvt.GraphicsNodeRenderContext;
import org.apache.batik.gvt.renderer.StaticRendererFactory;
//import org.apache.batik.gvt.renderer.StaticRendererFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.renderer.*;


//import oracle.xml.parser.v2.*;
//import org.apache.crimson.parser.*;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.GenericDOMImplementation;




//import org.w3c.dom.Document;
//import org.w3c.dom.Element;


/**
 * A Swing-based top level window class.
 * <P>
 * @author Ilian Alekov
 */
public class MyFrame extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
//  JPanel jPanel1 = new JPanel();
  MyCanvas myCanvas = new MyCanvas();
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenu1 = new JMenu();
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenuItem jMenuItem2 = new JMenuItem();
  URL myURL;
  private Graphics2D graphics = null;
  /**
   * Constructs a new instance.
   */
  public MyFrame() {
    super();
    try  {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    jMenu1.setText("File");
    jMenuItem1.setText("Open");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jMenuItem1_actionPerformed(e);
      }
    });
    jMenuItem2.setText("Exit");
    this.getContentPane().add(myCanvas, BorderLayout.CENTER);
    jMenuBar1.add(jMenu1);
    jMenu1.add(jMenuItem1);
    jMenu1.add(jMenuItem2);
    this.setJMenuBar(jMenuBar1);
  }

protected class MUserAgent implements UserAgent {
        AffineTransform currentTransform = null;
        /**
         * Creates a new SVGUserAgent.
         */
        protected MUserAgent(AffineTransform at) {
            currentTransform = at;
        }

        /**
         * Displays an error message.
         */
        public void displayError(String message) {
            System.err.println(message);
        }

        /**
         * Displays an error resulting from the specified Exception.
         */
        public void displayError(Exception ex) {
            ex.printStackTrace(System.err);
        }

        /**
         * Displays a message in the User Agent interface.
         * The given message is typically displayed in a status bar.
         */
        public void displayMessage(String message) {
            System.out.println(message);
        }

        /**
         * Returns a customized the pixel to mm factor.
         */
        public float getPixelToMM() {
            return 0.264583333333333333333f; // 72 dpi
        }

        /**
         * Returns the language settings.
         */
        public String getLanguages() {
            return "en";//userLanguages;
        }

        /**
         * Returns the user stylesheet uri.
         * @return null if no user style sheet was specified.
         */
        public String getUserStyleSheetURI() {
            return null;//userStyleSheetURI;
        }

        /**
         * Returns the class name of the XML parser.
         */
        public String getXMLParserClassName() {
            String parserClassName =
System.getProperty("org.xml.sax.parser");
            if (parserClassName == null) {
                parserClassName = "org.apache.xerces.parsers.SAXParser";
            }
            return
parserClassName;//application.getXMLParserClassName();
        }

        /**
         * Opens a link in a new component.
         * @param doc The current document.
         * @param uri The document URI.
         */
        public void openLink(SVGAElement elt) {
            //application.openLink(uri);
        }

        public Point getClientAreaLocationOnScreen() {
            return new Point(0, 0);
        }

        public void setSVGCursor(java.awt.Cursor cursor) {
        }

        public AffineTransform getTransform() {
            return currentTransform;
        }

        public Dimension2D getViewportSize() {
            return new Dimension(100, 100);
        }

        public EventDispatcher getEventDispatcher() {
            return null;
        }

        public boolean supportExtension(String str) {
            return false;
        }

        public boolean hasFeature(String str) {
            return false;
        }

        public void registerExtension(BridgeExtension be) {
        }
    }

    protected void renderSVGDocument(Document doc, int x, int y) {

        UserAgent userAgent = new MUserAgent(new AffineTransform());

        GVTBuilder builder = new GVTBuilder();
        StaticRendererFactory srf = new StaticRendererFactory();
        GraphicsNodeRenderContext rc = srf.getRenderContext();
        BridgeContext ctx = new BridgeContext(userAgent, rc);
        GraphicsNode root;

        graphics.translate(200, 100);
        graphics.setRenderingHints(rc.getRenderingHints());
        root = builder.build(ctx, doc);
        root.paint(graphics, rc);
        graphics.translate(-200, -100);
    }

  void jMenuItem1_actionPerformed(ActionEvent e) {
  BufferedImage myBImage = new
BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
   JFileChooser fc = new JFileChooser("c:\\.");

     // Step 1: create a DocumentBuilderFactory
     // Step 2: create a DocumentBuilder

   int choice = fc.showOpenDialog(myCanvas);
   if (choice == JFileChooser.APPROVE_OPTION)
   {
      File f = fc.getSelectedFile();
      try {
            myURL = f.toURL();
          }catch (Exception ex){ System.out.print(ex);}

      System.out.print("I'll try to parse it " + myURL.toString());
      try {

//        parser.setValidationMode(false);

        // Step 1: create a DocumentBuilderFactory
        DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
        // Step 2: create a DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // Step 3: parse the input file to get a Document object

        Document doc = db.parse(f);

        System.out.print("First XML Element: " +
doc.getDocumentElement().getTagName().toString());

        graphics = (Graphics2D)myBImage.getGraphics();
        renderSVGDocument(doc,50,50);
        myCanvas.setImage(myBImage);
       }
       catch(Exception ex) {
//       System.out.print(ex);
        ex.printStackTrace(System.out);
        }
      System.out.print("end of parse process "+myURL.toString());
    }

  }
}
Mime
View raw message