Skip to content

Commit 05bd93a

Browse files
committed
Merge pull request ericaro#22 from oyarzun/export
Make JSurface export method public
2 parents d530f8c + 0aead8b commit 05bd93a

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

src/main/java/net/ericaro/surfaceplotter/surface/JSurface.java

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -428,61 +428,6 @@ public SurfaceVertex[][] getValuesArray() {
428428
private boolean dragged; // dragged flag
429429
private int click_x, click_y; // previous mouse cursor position
430430

431-
/**
432-
* <code>mouseDown</code> event handler. Sets internal tracking variables
433-
* for dragging operations.
434-
*
435-
* @param e
436-
* the event
437-
* @param x
438-
* the x coordinate of cursor
439-
* @param y
440-
* the y coordinate of cursor
441-
*/
442-
443-
public void doExportPNG(File file) throws IOException {
444-
if (file == null)
445-
return;
446-
int h, w;
447-
w = 50;
448-
h = 30;
449-
java.awt.image.BufferedImage bf = new java.awt.image.BufferedImage(getWidth(), getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
450-
Graphics2D g2d = bf.createGraphics();
451-
452-
// g2d.setColor(java.awt.Color.white);
453-
// g2d.fillRect(0,0,getWidth() ,getHeight());
454-
// g2d.setColor(java.awt.Color.black);
455-
export(g2d);
456-
// java.awt.image.BufferedImage bf2=bf.getSubimage(0,0,w,h);
457-
boolean b = javax.imageio.ImageIO.write(bf, "PNG", file);
458-
}
459-
460-
/**
461-
* needs batik, will reintroduce it later
462-
* @throws ParserConfigurationException
463-
*/
464-
public void doExportSVG(File file) throws IOException, ParserConfigurationException{
465-
if (file == null)
466-
return;
467-
468-
// Create an instance of org.w3c.dom.Document
469-
org.w3c.dom.Document document = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
470-
471-
// Create an instance of the SVG Generator
472-
473-
org.apache.batik.svggen.SVGGraphics2D svgGenerator = new org.apache.batik.svggen.SVGGraphics2D(document);
474-
475-
// Ask the test to render into the SVG Graphics2D implementation
476-
export(svgGenerator);
477-
478-
// Finally, stream out SVG to the standard output using UTF-8 //
479-
// character to byte encoding
480-
boolean useCSS = true; // we want to use CSS // style attribute
481-
java.io.Writer out = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file), "UTF-8");
482-
svgGenerator.stream(out, useCSS);
483-
out.close();
484-
}
485-
486431
/**
487432
* Paints surface. Creates surface plot, contour plot, or density plot based
488433
* on current vertices array, contour plot flag, and density plot flag. If
@@ -591,7 +536,13 @@ public void update(Graphics g) {
591536
paintComponent(g); // do not erase, just paint
592537
}
593538

594-
private void export(Graphics g) {
539+
/**
540+
* Exports <code>JSurface</code> to the specified graphics object.
541+
*
542+
* @param g the graphics object
543+
*
544+
*/
545+
public void export(Graphics g) {
595546
if (data_available && !interrupted) {
596547
boolean old = printing;
597548
printing = true;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.ericaro.surfaceplotter.surface;
2+
3+
import java.awt.Graphics2D;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.xml.parsers.ParserConfigurationException;
8+
9+
public class SurfaceUtils {
10+
11+
public static void doExportPNG(JSurface surface, File file) throws IOException {
12+
if (file == null)
13+
return;
14+
int h, w;
15+
w = 50;
16+
h = 30;
17+
java.awt.image.BufferedImage bf = new java.awt.image.BufferedImage(surface.getWidth(), surface.getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
18+
Graphics2D g2d = bf.createGraphics();
19+
20+
g2d.setColor(java.awt.Color.white);
21+
g2d.fillRect(0,0,surface.getWidth() ,surface.getHeight());
22+
g2d.setColor(java.awt.Color.black);
23+
surface.export(g2d);
24+
// java.awt.image.BufferedImage bf2=bf.getSubimage(0,0,w,h);
25+
boolean b = javax.imageio.ImageIO.write(bf, "PNG", file);
26+
}
27+
28+
/**
29+
* needs batik, will reintroduce it later
30+
* @throws ParserConfigurationException
31+
*/
32+
public static void doExportSVG(JSurface surface, File file) throws IOException, ParserConfigurationException{
33+
if (file == null)
34+
return;
35+
36+
// Create an instance of org.w3c.dom.Document
37+
org.w3c.dom.Document document = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
38+
39+
// Create an instance of the SVG Generator
40+
41+
org.apache.batik.svggen.SVGGraphics2D svgGenerator = new org.apache.batik.svggen.SVGGraphics2D(document);
42+
43+
// Ask the test to render into the SVG Graphics2D implementation
44+
surface.export(svgGenerator);
45+
46+
// Finally, stream out SVG to the standard output using UTF-8 //
47+
// character to byte encoding
48+
boolean useCSS = true; // we want to use CSS // style attribute
49+
java.io.Writer out = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file), "UTF-8");
50+
svgGenerator.stream(out, useCSS);
51+
out.close();
52+
}
53+
}

src/test/java/Sample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.ericaro.surfaceplotter.JSurfacePanel;
1717
import net.ericaro.surfaceplotter.Mapper;
1818
import net.ericaro.surfaceplotter.ProgressiveSurfaceModel;
19+
import net.ericaro.surfaceplotter.surface.SurfaceUtils;
1920

2021

2122

@@ -53,7 +54,7 @@ private void button1ActionPerformed() {
5354
JFileChooser jfc = new JFileChooser();
5455
try {
5556
if (jfc.showSaveDialog(surfacePanel1) == JFileChooser.APPROVE_OPTION )
56-
surfacePanel1.getSurface().doExportSVG( jfc.getSelectedFile());
57+
SurfaceUtils.doExportSVG(surfacePanel1.getSurface(), jfc.getSelectedFile());
5758
} catch (Exception e) {
5859
e.printStackTrace();
5960
}

0 commit comments

Comments
 (0)