Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 7 additions & 56 deletions src/main/java/net/ericaro/surfaceplotter/surface/JSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,61 +428,6 @@ public SurfaceVertex[][] getValuesArray() {
private boolean dragged; // dragged flag
private int click_x, click_y; // previous mouse cursor position

/**
* <code>mouseDown</code> event handler. Sets internal tracking variables
* for dragging operations.
*
* @param e
* the event
* @param x
* the x coordinate of cursor
* @param y
* the y coordinate of cursor
*/

public void doExportPNG(File file) throws IOException {
if (file == null)
return;
int h, w;
w = 50;
h = 30;
java.awt.image.BufferedImage bf = new java.awt.image.BufferedImage(getWidth(), getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bf.createGraphics();

// g2d.setColor(java.awt.Color.white);
// g2d.fillRect(0,0,getWidth() ,getHeight());
// g2d.setColor(java.awt.Color.black);
export(g2d);
// java.awt.image.BufferedImage bf2=bf.getSubimage(0,0,w,h);
boolean b = javax.imageio.ImageIO.write(bf, "PNG", file);
}

/**
* needs batik, will reintroduce it later
* @throws ParserConfigurationException
*/
public void doExportSVG(File file) throws IOException, ParserConfigurationException{
if (file == null)
return;

// Create an instance of org.w3c.dom.Document
org.w3c.dom.Document document = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

// Create an instance of the SVG Generator

org.apache.batik.svggen.SVGGraphics2D svgGenerator = new org.apache.batik.svggen.SVGGraphics2D(document);

// Ask the test to render into the SVG Graphics2D implementation
export(svgGenerator);

// Finally, stream out SVG to the standard output using UTF-8 //
// character to byte encoding
boolean useCSS = true; // we want to use CSS // style attribute
java.io.Writer out = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file), "UTF-8");
svgGenerator.stream(out, useCSS);
out.close();
}

/**
* Paints surface. Creates surface plot, contour plot, or density plot based
* on current vertices array, contour plot flag, and density plot flag. If
Expand Down Expand Up @@ -591,7 +536,13 @@ public void update(Graphics g) {
paintComponent(g); // do not erase, just paint
}

private void export(Graphics g) {
/**
* Exports <code>JSurface</code> to the specified graphics object.
*
* @param g the graphics object
*
*/
public void export(Graphics g) {
if (data_available && !interrupted) {
boolean old = printing;
printing = true;
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/net/ericaro/surfaceplotter/surface/SurfaceUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.ericaro.surfaceplotter.surface;

import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

public class SurfaceUtils {

public static void doExportPNG(JSurface surface, File file) throws IOException {
if (file == null)
return;
int h, w;
w = 50;
h = 30;
java.awt.image.BufferedImage bf = new java.awt.image.BufferedImage(surface.getWidth(), surface.getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bf.createGraphics();

g2d.setColor(java.awt.Color.white);
g2d.fillRect(0,0,surface.getWidth() ,surface.getHeight());
g2d.setColor(java.awt.Color.black);
surface.export(g2d);
// java.awt.image.BufferedImage bf2=bf.getSubimage(0,0,w,h);
boolean b = javax.imageio.ImageIO.write(bf, "PNG", file);
}

/**
* needs batik, will reintroduce it later
* @throws ParserConfigurationException
*/
public static void doExportSVG(JSurface surface, File file) throws IOException, ParserConfigurationException{
if (file == null)
return;

// Create an instance of org.w3c.dom.Document
org.w3c.dom.Document document = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

// Create an instance of the SVG Generator

org.apache.batik.svggen.SVGGraphics2D svgGenerator = new org.apache.batik.svggen.SVGGraphics2D(document);

// Ask the test to render into the SVG Graphics2D implementation
surface.export(svgGenerator);

// Finally, stream out SVG to the standard output using UTF-8 //
// character to byte encoding
boolean useCSS = true; // we want to use CSS // style attribute
java.io.Writer out = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file), "UTF-8");
svgGenerator.stream(out, useCSS);
out.close();
}
}
3 changes: 2 additions & 1 deletion src/test/java/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.ericaro.surfaceplotter.JSurfacePanel;
import net.ericaro.surfaceplotter.Mapper;
import net.ericaro.surfaceplotter.ProgressiveSurfaceModel;
import net.ericaro.surfaceplotter.surface.SurfaceUtils;



Expand Down Expand Up @@ -53,7 +54,7 @@ private void button1ActionPerformed() {
JFileChooser jfc = new JFileChooser();
try {
if (jfc.showSaveDialog(surfacePanel1) == JFileChooser.APPROVE_OPTION )
surfacePanel1.getSurface().doExportSVG( jfc.getSelectedFile());
SurfaceUtils.doExportSVG(surfacePanel1.getSurface(), jfc.getSelectedFile());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down