Rather than overriding the public method Component.paint(Graphics g), as it is done with the AWT version,
the Swing version of JetChart overrides the protected method JComponent.paintComponent(Graphics g)
to implement the drawing code of all graphs.
As explained in the previous
topic, an instance of com.jinsight.svg.SVGGraphics, a subclass of java.awt.Graphics, is always
passed by the ChartEncoder class to the paint method of subclasses of GenericGraph when the
encoding process is started, whatever the version, Swing or AWT.
ChartEncoder has no access to
the protected method paintComponent() of the Swing version, and a direct call to the paint method of
Swing components brings unexpected results. To overcome this problem, the call to the paint method must be bypassed
and the SVGGraphics object passed directly to the paintComponent method. This can be achieved by
creating a inner class subclassing the chart context used and overriding the paint method to pass the
Graphics instance directly to the paintComponent() method. Subclasses have access to the protected
methods of their superclasses, so this approach solves the problem.
The following example is the same given in the previous topic, adapted to run as a Swing application.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.jinsight.jetchart.*; import com.jinsight.svg.*; import java.io.*; import com.jinsight.svg.*; import java.io.*; public class Main extends JFrame implements ActionListener { CustomGraph graph; // This flag is checked by CustomGraph, a subclass of Graph, // to verify if an SVG encoding process has been started. boolean isGeneratingSVG; public Main() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel topPanel=new JPanel(); JButton b=new JButton("Generate SVG"); b.addActionListener(this); topPanel.add(b); getContentPane().add("North",topPanel); graph=new CustomGraph(new String[]{"l1","l2","l3","l4","l5","l6","l7"}); graph.setTitle(new String[]{"The JetChart Library","SVG Encoding Demo"}); graph.set3DEnabled(true); graph.getGraphSet(0).getGrid().setEnabled(true); graph.setGradientColors(Color.blue,Color.yellow); BarSerie bs=new BarSerie(); bs.setValues(new double[]{100,80,60,40,90,40,140}); bs.setColor(Color.cyan); bs.setTitle("Bar series"); graph.addSerie(bs); getContentPane().add("Center",graph); setSize(500,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { isGeneratingSVG=true; ChartEncoder ce=new ChartEncoder(graph); OutputStream out=null; try { File f=new File("chart.svg"); out=new FileOutputStream(f); // Encodes chart and outputs SVG code to the chart.svg file. ce.svgEncode(out,false,SVGEncoder.HIGH_QUALITY); } catch (IOException e) { e.printStackTrace(); } finally { isGeneratingSVG=false; try { if (out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } private class CustomGraph extends Graph { public void paint(Graphics g) { if (isGeneratingSVG) paintComponent(g); else super.paint(g); } } public static void main(String[] args) { new Main(); } }