/* Copyright (C) 2014-2016 A.Vontobel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d;
import net.sf.drawj2d.cmd.HCLparser;
import net.sf.drawj2d.yacas.PlotDataParser;
import net.sf.drawj2d.bgd.BgdInterpreter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
/**
*
* @author av
*/
public class JREMode implements inConst {
private double res;
private int pxwidth;
private int pxheight;
boolean success = true;
private int type;
private final boolean quiet;
private final boolean verbose;
private final String filename;
private final String suffix;
private final String quelldatei;
private final double width;
private final double height;
private final double originX;
private final double originY;
private final int srcType;
private JFrame frame;
private final boolean debug = false;
/**
* Creates a new instance of JREMode
* @param c Drawj2d console context
*/
public JREMode (Drawj2d c) {
quiet = c.quiet;
verbose = c.verbose;
width = c.width;
height = c.height;
originX = c.originX;
originY = c.originY;
filename = c.zieldatei;
suffix = c.outputtype.toLowerCase();
quelldatei = c.quelldatei;
srcType = c.srcType;
if (!quiet) System.out.println("Mode: JRE");
String IOsuffixes[] = ImageIO.getWriterFileSuffixes();
for (String t: IOsuffixes) { //System.out.println(t); // 1.7: jpg,bmp,jpeg,wbmp,png,gif
if (t.equals(suffix)) {
type = IMG;
res = imgres;
}
}
if (suffix.equalsIgnoreCase("screen")) {
type = SCREEN;
try {
res = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (java.awt.HeadlessException e) {
res = stdres;
}
}
}
/** Overwrite the default resolution.
* @param resolution dpi */
public void setResolution(double resolution) {
res = resolution;
}
/** Create the drawing.
* @return true if successful */
public boolean run() {
pxwidth = (int) Math.rint(width*res/inch);
pxheight = (int) Math.rint(height*res/inch);
switch (type) {
case IMG:
writeOutput();
break;
case SCREEN:
showOutput();
break;
default:
System.err.println("Not supported: " + suffix);
success = false;
}
return success;
}
private void writeOutput() {
assert type == IMG;
// Write the output to a file
FileOutputStream file = null;
try {
file = new FileOutputStream(filename);
int imgtype = BufferedImage.TYPE_INT_ARGB; // including transparency (alpha channel)
if (suffix.equals("jpeg") || suffix.equals("jpg") || suffix.equals("bmp")) {
imgtype = BufferedImage.TYPE_INT_RGB; // not supporting transparency, avoid black image
}
if (suffix.equals("wbmp")) {
imgtype = BufferedImage.TYPE_BYTE_BINARY; // black-white, avoid empty file
}
BufferedImage bi = new BufferedImage(pxwidth, pxheight, imgtype);
Graphics2D ig2 = bi.createGraphics();
draw(ig2);
ImageIO.write(bi, suffix, file);
} catch (FileNotFoundException ex) {
if (debug) Logger.getLogger(JREMode.class.getName()).log(Level.SEVERE, null, ex);
else System.err.println(ex.getLocalizedMessage());
success = false;
} catch (IOException ex) {
if (debug) Logger.getLogger(JREMode.class.getName()).log(Level.SEVERE, null, ex);
else System.err.println(ex.getLocalizedMessage());
success = false;
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException ex) {
Logger.getLogger(JREMode.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (success) {
System.out.println("Output written to: " + filename);
}
else {
System.out.println("Error occurred. Check for error messages.");
}
}
private void showOutput() {
assert type == SCREEN;
// Display the output to the screen
frame = new JFrame();
frame.setTitle("Drawj2d: " + quelldatei);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
class Zeichenpanel extends JPanel{
JREMode mode;
Zeichenpanel (JREMode mode) {
this.mode = mode;
}
@Override
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Color remember = g2.getColor();
g2.setColor(Color.white);
g2.fillRect(0, 0, mode.pxwidth, mode.pxheight);
g2.setColor(remember);
mode.draw(g2);
}
}
final Zeichenpanel p = new Zeichenpanel(this);
p.setPreferredSize(new Dimension(pxwidth, pxheight));
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
if (pxwidth < scr.getWidth() - 100 && pxheight < scr.getHeight() - 100) {
frame.add(p, BorderLayout.CENTER);
frame.setResizable(false);
} else {
JScrollPane sp = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(sp, BorderLayout.CENTER);
}
JLabel info = new JLabel("ESC close, F5 refresh");
frame.add(info, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
// on ESC key close frame
frame.getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
frame.getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "Redraw");
frame.getRootPane().getActionMap().put("Cancel", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
frame.getRootPane().getActionMap().put("Redraw", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
p.repaint();
}
});
if (success) {
System.out.println("Preview: " + quelldatei);
}
else {
System.out.println("Error occurred. Check for error messages.");
}
}
private void draw(Graphics2D g) {
switch (srcType) {
case srcTypeBgd:
g.scale(res / inch, res / inch);
g.translate(originX, originY); // usually 0, autoscale does also autocenter.
BgdInterpreter bgd_interp = new BgdInterpreter(g, width, height);
bgd_interp.setVerbose(verbose);
success = bgd_interp.sourceFile(quelldatei);
boolean autoscale = true;
if (success) bgd_interp.interpret(autoscale);
break;
case srcTypeYacasPlotData:
PlotDataParser ypd_parser = new PlotDataParser(g, pxwidth, pxheight);
ypd_parser.setOrigin((int) Math.rint(originX*res/inch), (int) Math.rint(originY*res/inch));
ypd_parser.setVerbose(verbose);
success = ypd_parser.sourceFile(quelldatei);
break;
case srcTypeHcl:
default:
g.scale(res / inch, res / inch);
g.translate(originX, originY);
HCLparser ig_parser = new HCLparser(g);
ig_parser.setVerbose(verbose);
success = ig_parser.sourceFile(quelldatei);
}
}
}
Ads help maintain this website.