You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Ashish Kulkarni <ku...@yahoo.com> on 2003/06/19 01:13:30 UTC

create a SVG file from JPanel

Hi,

I am pasting a code below , i want to create a SVG
file of MyTotalPanel so i can create a PDF file from
this SVG file, 
but some how it is not working, can any one please
point out why,
I dont care how big the file is, or if it fits in one
page or not, i want a SVG file so can be viewd on
screen by scrolling

Ashish
/** code here
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import java.awt.print.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;

import org.w3c.dom.*;
import org.apache.xerces.dom.*;
//import org.apache.batik.svggen.*;
import java.io.FileOutputStream;
import java.io.Writer;
import java.io.OutputStreamWriter;
import org.apache.batik.svggen.*;
import org.apache.batik.svggen.*;
import java.awt.image.BufferedImage;
import org.apache.batik.dom.GenericDOMImplementation;

public class TestPDFCreation extends JFrame implements
ActionListener
{
	
	private MyTotalPanel totalPanel;
	public TestPDFCreation()
	{
	super("Print Frame");

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("create svg");
    JMenuItem svg = new JMenuItem("Create SVG");
	svg.addActionListener(this);
	menu.add(svg);
	menuBar.add(menu);
	totalPanel = new MyTotalPanel();
	this.setJMenuBar(menuBar);
	JScrollPane pane = new JScrollPane(totalPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    this.getContentPane().add(pane);
   	this.setSize(1000,700);
   	show();
   	}
   		public void actionPerformed(ActionEvent evt)
	{
	//do printing here
		 try
	    {
    	DOMImplementation domImpl =
    	GenericDOMImplementation.getDOMImplementation();
    	Document document = domImpl.createDocument(null,
"svg", null);
    	SVGGraphics2D svgGenerator = new
SVGGraphics2D(document);
    	
        totalPanel.paint(svgGenerator);
       //
svgGenerator.setSVGCanvasSize(totalPanel.getPreferredSize());
        
        String useCssStr =
System.getProperty("useCss", "true");
        boolean useCss =
useCssStr.equalsIgnoreCase("true");

   		FileOutputStream fos = new
FileOutputStream("C:\\Fop-0.20.1\\docs\\examples\\svg\\paneltest.svg");
    	Writer outWriter = new OutputStreamWriter(fos,
"UTF-8");
    	svgGenerator.stream(outWriter, useCss);
    	
    	}
    	
    	catch (Exception exc)
    	{
     
      		exc.printStackTrace();
    	}
	}
	
	public class MyTotalPanel extends JPanel
	{
		public MyTotalPanel()
		{
			this.setBackground(Color.white);
			this.setOpaque(true);
			setPreferredSize(new Dimension(1600, 1600) );
		}	
		public void paintComponent(Graphics g)
      {
    	  super.paintComponent(g);
    	  g.setColor(Color.blue);
    	  int x = 0;
    	  
    	  	for(int i = 0; i < 100 ; i++)
       		{	
      		x +=60;
      		g.drawLine(x, 0, x, 1600);
      		g.drawString(String.valueOf(i), x, 10);
     	  	}
		
		
		}
	}
	
	public static void main(String args[])
    {
    	new TestPDFCreation();
    
    }
	
	
	
}

 end of code **/

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: create a SVG file from JPanel

Posted by Thomas DeWeese <Th...@Kodak.com>.
Ashish Kulkarni wrote:

>Hi,
>
>I am pasting a code below , i want to create a SVG
>file of MyTotalPanel so i can create a PDF file from
>this SVG file, 
>but some how it is not working, can any one please
>point out why,
>I dont care how big the file is, or if it fits in one
>page or not, i want a SVG file so can be viewd on
>screen by scrolling
>  
>
    The basic code looks good here.   You don't say how it's not working.
One thing I did notice was that you set the Canvas size after you draw your
stuff - which probably isn't a great idea.  Also generally you want to 
turn off
Swing double buffering - otherwise Swing is likely to render all your 
'vector'
data into an offscreen buffer and only that bufferedImage will be drawn 
to the
SVGGraphics2D (which is what you wanted to do in your second message :).

>/** code here
>import java.awt.*;
>import java.awt.event.*;
>import java.awt.image.*;
>import java.util.*;
>import java.io.*;
>import java.awt.print.*;
>
>import javax.swing.*;
>import javax.swing.border.*;
>import javax.swing.event.*;
>import javax.swing.filechooser.*;
>
>import org.w3c.dom.*;
>import org.apache.xerces.dom.*;
>//import org.apache.batik.svggen.*;
>import java.io.FileOutputStream;
>import java.io.Writer;
>import java.io.OutputStreamWriter;
>import org.apache.batik.svggen.*;
>import org.apache.batik.svggen.*;
>import java.awt.image.BufferedImage;
>import org.apache.batik.dom.GenericDOMImplementation;
>
>public class TestPDFCreation extends JFrame implements
>ActionListener
>{
>	
>	private MyTotalPanel totalPanel;
>	public TestPDFCreation()
>	{
>	super("Print Frame");
>
>this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
>   
>    JMenuBar menuBar = new JMenuBar();
>    JMenu menu = new JMenu("create svg");
>    JMenuItem svg = new JMenuItem("Create SVG");
>	svg.addActionListener(this);
>	menu.add(svg);
>	menuBar.add(menu);
>	totalPanel = new MyTotalPanel();
>	this.setJMenuBar(menuBar);
>	JScrollPane pane = new JScrollPane(totalPanel,
>JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
>JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
>    this.getContentPane().add(pane);
>   	this.setSize(1000,700);
>   	show();
>   	}
>   		public void actionPerformed(ActionEvent evt)
>	{
>	//do printing here
>		 try
>	    {
>    	DOMImplementation domImpl =
>    	GenericDOMImplementation.getDOMImplementation();
>    	Document document = domImpl.createDocument(null,
>"svg", null);
>    	SVGGraphics2D svgGenerator = new
>SVGGraphics2D(document);
>    	
>        totalPanel.paint(svgGenerator);
>       //
>svgGenerator.setSVGCanvasSize(totalPanel.getPreferredSize());
>        
>        String useCssStr =
>System.getProperty("useCss", "true");
>        boolean useCss =
>useCssStr.equalsIgnoreCase("true");
>
>   		FileOutputStream fos = new
>FileOutputStream("C:\\Fop-0.20.1\\docs\\examples\\svg\\paneltest.svg");
>    	Writer outWriter = new OutputStreamWriter(fos,
>"UTF-8");
>    	svgGenerator.stream(outWriter, useCss);
>    	
>    	}
>    	
>    	catch (Exception exc)
>    	{
>     
>      		exc.printStackTrace();
>    	}
>	}
>	
>	public class MyTotalPanel extends JPanel
>	{
>		public MyTotalPanel()
>		{
>			this.setBackground(Color.white);
>			this.setOpaque(true);
>			setPreferredSize(new Dimension(1600, 1600) );
>		}	
>		public void paintComponent(Graphics g)
>      {
>    	  super.paintComponent(g);
>    	  g.setColor(Color.blue);
>    	  int x = 0;
>    	  
>    	  	for(int i = 0; i < 100 ; i++)
>       		{	
>      		x +=60;
>      		g.drawLine(x, 0, x, 1600);
>      		g.drawString(String.valueOf(i), x, 10);
>     	  	}
>		
>		
>		}
>	}
>	
>	public static void main(String args[])
>    {
>    	new TestPDFCreation();
>    
>    }
>	
>	
>	
>}
>
> end of code **/
>
>__________________________________
>Do you Yahoo!?
>SBC Yahoo! DSL - Now only $29.95 per month!
>http://sbc.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
>For additional commands, e-mail: batik-users-help@xml.apache.org
>
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org