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 Todd Wilson <to...@byu.edu> on 2002/03/12 22:58:49 UTC
Using transparency over an underlying component
Greetings,
We're building a client-side Java app in which we'd like to use SVG
images, but seem to be having difficulty with the transparency feature.
Just to emphasize up front--what we're building is pure client-side
Java; that is, no web browser, no Adobe SVG Viewer, etc.
Transparency within an SVG seems to work just fine, meaning shapes
overlaying each other where "fill:none" can be designated in order to
allow some shapes visible through other shapes. However, we're hoping
to accomplish basically what a transparent GIF allows--portions of an
image are transparent and allow whatever the SVG is rendred on top of to
show through the image. So, for example, I could render an SVG on top
of a JFrame or JPanel that has a blue background, and portions of that
blue background would be visible through the SVG because portions of the
SVG are transparent.
Here's our shot at this--
===============================================
test.svg
===============================================
<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="300" height="200">
<rect x="50" y="50" width="100" height="50" style="fill:none;
stroke:red;"/>
</svg>
===============================================
Notice the style attribute contains "fill:none" which, as we understand
it, will make its fill transparent.
===============================================
TransTest.java
===============================================
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import org.apache.batik.swing.*;
import org.apache.batik.swing.gvt.*;
public class TransTest extends JFrame
{
public TransTest()
{
// Kill the app when the window closes.
this.addWindowListener( new WindowAdapter() { public void
windowClosing( WindowEvent e ) { System.exit( 0 ); } } );
// Create an SVG canvas.
JSVGCanvas svgCanvas = new JSVGCanvas();
// Make sure that the canvas isn't opaque.
svgCanvas.setOpaque( false );
System.out.println( "isOpaque: " + svgCanvas.isOpaque() );
// Tell the canvas where to find the SVG.
svgCanvas.setURI( "file:/path/to/svg/test.svg" );
// Turn the background of the frame a bright blue so that if the
// SVG is transparent the blue will shine through.
this.getContentPane().setBackground( Color.blue );
// Add the SVG to the frame.
this.getContentPane().add( svgCanvas );
// Make the frame visible and pack it so that it can be
// displayed.
this.setVisible( true );
this.pack();
}
public static void main( String[] args )
{
TransTest transTest = new TransTest();
}
}
===============================================
We've tried to make the class as simple as possible, but notice that it
does set the background of the JFrame to blue, so the blue should be
visible through the SVG, if the transparency is working as we'd like it to.
When the class is compiled and run the SVG is renderd on the JFrame just
fine, but the background is white instead of blue.
We read in an earlier post to this list that the type of transparency
we're trying for works in IE with the Adobe SVG Viewer, and that the
transparency is accomplished using "fill:none" in the style attribute.
Is there another way we should be rendering the SVG images aside from
using JSVGCanvas? Is there something else we're missing? Is the type
of transparency we're hoping to use even possible with SVG in the
current version of Batik?
Many thanks in advance to anyone willing to lend a hand.
Regards,
Todd Wilson
P.S. We've been watching the list for a while and have searched the
archive for a solution to our problem, but haven't had any luck. I
apologize in advance if this problem has been previously posted and
resolved.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Using transparency over an underlying component
Posted by Todd Wilson <to...@byu.edu>.
Doh! You're absolutely right, Thomas. I sincerely apologize. Your
suggestion worked like a charm. Many thanks.
Todd
Thomas E Deweese wrote:
>>>>>>"TW" == Todd Wilson <to...@byu.edu> writes:
>>>>>>
>
> TW> Unfortunately that just changed the background from white to
> TW> black, instead of allowing the blue background of the underlying
> TW> JFrame to show through. Here's my revised code, just let me know
> TW> if I'm misunderstanding anything:
>
>
> TW> svgCanvas.setBackground( new Color( 0, 0, 0 ) );
> ^^^^^^^^^
> Three zeros is opaque black, four zeros (what I had in my message)
> is transparent black.
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Using transparency over an underlying component
Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "TW" == Todd Wilson <to...@byu.edu> writes:
TW> Unfortunately that just changed the background from white to
TW> black, instead of allowing the blue background of the underlying
TW> JFrame to show through. Here's my revised code, just let me know
TW> if I'm misunderstanding anything:
TW> svgCanvas.setBackground( new Color( 0, 0, 0 ) );
^^^^^^^^^
Three zeros is opaque black, four zeros (what I had in my message)
is transparent black.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Using transparency over an underlying component
Posted by Todd Wilson <to...@byu.edu>.
Thanks for the reply, Thomas. Unfortunately that just changed the
background from white to black, instead of allowing the blue background
of the underlying JFrame to show through. Here's my revised code, just
let me know if I'm misunderstanding anything:
=============================================================
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import org.apache.batik.swing.*;
import org.apache.batik.swing.gvt.*;
public class TransTest extends JFrame
{
public TransTest()
{
// Kill the app when the window closes.
this.addWindowListener( new WindowAdapter() { public void
windowClosing( WindowEvent e ) { System.exit( 0 ); } } );
// Create an SVG canvas.
JSVGCanvas svgCanvas = new JSVGCanvas();
// Make sure that the canvas isn't opaque.
svgCanvas.setBackground( new Color( 0, 0, 0 ) );
svgCanvas.setOpaque( false );
System.out.println( "isOpaque: " + svgCanvas.isOpaque() );
// Tell the canvas where to find the SVG.
svgCanvas.setURI( "file:/path/to/svg/test.svg" );
// Turn the background of the frame a bright blue so that if the
// SVG is transparent the blue will shine through.
this.getContentPane().setBackground( Color.blue );
// Add the SVG to the frame.
this.getContentPane().add( svgCanvas );
// Make the frame visible and pack it so that it can be
// displayed.
this.setVisible( true );
this.pack();
}
public static void main( String[] args )
{
TransTest transTest = new TransTest();
}
}
=============================================================
Todd
Thomas E Deweese wrote:
>>>>>>"TW" == Todd Wilson <to...@byu.edu> writes:
>>>>>>
>
> TW> Transparency within an SVG seems to work just fine, meaning shapes
> TW> overlaying each other where "fill:none" can be designated in order
> TW> to allow some shapes visible through other shapes. However, we're
> TW> hoping to accomplish basically what a transparent GIF
> TW> allows--portions of an image are transparent and allow whatever
> TW> the SVG is rendred on top of to show through the image. So, for
> TW> example, I could render an SVG on top of a JFrame or JPanel that
> TW> has a blue background, and portions of that blue background would
> TW> be visible through the SVG because portions of the SVG are
> TW> transparent.
>
> You need to set the background of the JSVGCanvas to something
> other than white. I suggest transparent black:
>
> // Make sure that the canvas isn't opaque.
> + svgCanvas.setBackground(new Color(0,0,0,0));
> svgCanvas.setOpaque( false );
> System.out.println( "isOpaque: " + svgCanvas.isOpaque() );
>
>
> ---------------------------------------------------------------------
> 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
Using transparency over an underlying component
Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "TW" == Todd Wilson <to...@byu.edu> writes:
TW> Transparency within an SVG seems to work just fine, meaning shapes
TW> overlaying each other where "fill:none" can be designated in order
TW> to allow some shapes visible through other shapes. However, we're
TW> hoping to accomplish basically what a transparent GIF
TW> allows--portions of an image are transparent and allow whatever
TW> the SVG is rendred on top of to show through the image. So, for
TW> example, I could render an SVG on top of a JFrame or JPanel that
TW> has a blue background, and portions of that blue background would
TW> be visible through the SVG because portions of the SVG are
TW> transparent.
You need to set the background of the JSVGCanvas to something
other than white. I suggest transparent black:
// Make sure that the canvas isn't opaque.
+ svgCanvas.setBackground(new Color(0,0,0,0));
svgCanvas.setOpaque( false );
System.out.println( "isOpaque: " + svgCanvas.isOpaque() );
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org