On Thursday 09 August 2001 18:03, Phil Cope wrote:
> Hello Batik developers,
>
> This is a kind of request for help/bug report.
>
> What I would like to do is overlay 2 SVG documents, or overlay
> an SVG document on a surface that has a previously rendered
> png/jpeg image.
>
> I had the idea to use Swing's JLayeredPane to layer a JSVGCanvas
> over another one, however this doesn't work - the lower document
> is always obliterated, even if we call JSVGCanvas.setOpaque(false);
>
> It is possible that this would be fixed by the reworking of the
> following code in JGVTComponent
>
> public void paintComponent(Graphics g) {
> super.paintComponent(g);
>
> Graphics2D g2d = (Graphics2D)g;
>
> Dimension d = getSize();
> g2d.setComposite(AlphaComposite.SrcOver);
> g2d.setPaint(getBackground());
> g2d.fillRect(0, 0, d.width, d.height);
> ...
>
> An alternative solution would be to have a renderer that isn't
> a JComponent, but that HAS a JComponent as its rendering surface
> so that I could write my own paintComponent() method on
> my own subclass that did something like
>
> public void paintComponent(Graphics g) {
> //
> clear(bgColor);
> backgroundSVGDocument.render(this); // or g ?
> foregroundSVGDocument.render(this); // or g ?
> }
> Where back/foregroundSVGDocument are instances of this
> hypothetical SVGrenderer.
> Is there any way of achieving this with the current batik toolkit ?
>
A solution is to set the background color of the foreground component to a
fully transparent color, by calling:
svgCanvas.setBackground(new Color(0, 0, 0, 0));
The attached file is a modified version of the Batik website's JSVGCanvas
example which is demonstrating SVG documents layering.
--
Stephane.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
public class SVGApplication {
public static void main(String[] args) {
JFrame f = new JFrame("Batik");
SVGApplication app = new SVGApplication(f);
f.getContentPane().add(app.createComponents());
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setVisible(true);
}
JFrame frame;
JButton button = new JButton("Load...");
JLabel label = new JLabel();
JSVGCanvas svgCanvas = new JSVGCanvas();
JSVGCanvas svgCanvas2 = new JSVGCanvas();
public SVGApplication(JFrame f) {
frame = f;
}
public JComponent createComponents() {
final JPanel panel = new JPanel(new BorderLayout());
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(button);
p.add(label);
panel.add("North", p);
JPanel lp = new JPanel(new StackLayout());
lp.add(svgCanvas);
lp.add(svgCanvas2);
svgCanvas.setBackground(new Color(0, 0, 0, 0));
svgCanvas2.setURI("file:samples/anne.svg");
panel.add("Center", lp);
// Set the button action.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if (choice == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
try {
svgCanvas.setURI(f.toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
// Set the JSVGCanvas listeners.
svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
label.setText("Document Loading...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
label.setText("Document Loaded.");
}
});
svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {
label.setText("Build Started...");
}
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
label.setText("Build Done.");
//frame.pack();
}
});
svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
label.setText("Rendering Started...");
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
label.setText("");
}
});
return panel;
}
public static class StackLayout implements LayoutManager {
public void addLayoutComponent(String name, Component comp) {
// nothing to do
}
public void layoutContainer(Container parent) {
Dimension dim = parent.getSize();
for (int i=0; i < parent.getComponentCount(); ++i) {
parent.getComponent(i).setBounds(0, 0, dim.width, dim.height);
}
}
public Dimension minimumLayoutSize(Container parent) {
Dimension minSize = new Dimension(0, 0);
for (int i=0; i < parent.getComponentCount(); ++i) {
Dimension dim = parent.getComponent(i).getMinimumSize();
minSize.width = Math.max(minSize.width, dim.width);
minSize.height = Math.max(minSize.height, dim.height);
}
return minSize;
}
public Dimension preferredLayoutSize(Container parent) {
Dimension size = new Dimension(0, 0);
for (int i=0; i < parent.getComponentCount(); ++i) {
Dimension dim = parent.getComponent(i).getPreferredSize();
size.width = Math.max(size.width, dim.width);
size.height = Math.max(size.height, dim.height);
}
return size;
}
public void removeLayoutComponent(Component comp) {
// nothing to do
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]