/* Copyright (C) 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.cmd;

import java.io.IOException;
import org.hecl.*;

/**
 * ImageCmd implements the "image" command, which 
 * draws an image.
 */

public class ImageCmd implements Command {
    API api;
    boolean debug = false;
    
    public ImageCmd (API api) {
        this.api = api;
    }
    
    @Override
    public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
        if (debug) System.out.println(argv[0].toString());
        
        String filename;
        if (argv.length > 1) {
            filename = StringThing.get(argv[1]);
        } else {
            throw new HeclException ("Missing filename");
        }
        
        double dpi = 200;
        double massstab = 0;
        double pxX = 0;
        double pxY = 0;
        try {
            switch (argv.length - 1) {
                case 5:
                    try {
                        massstab = DoubleThing.get(argv[5]);
                    } catch (NumberFormatException e) {
                    }
                case 4:
                    try {
                        pxY = DoubleThing.get(argv[4]);
                        pxX = DoubleThing.get(argv[3]);
                    } catch (NumberFormatException e) {
                    }
                //case 3: does not exist!
                case 2:
                    try {
                        dpi = DoubleThing.get(argv[2]);
                    } catch (NumberFormatException e) {
                    }
                case 1: // filename
                    api.image(filename, dpi, pxX, pxY, massstab);
                    break;
                default:
                    throw new HeclException("Illegal nb of arguments.");
            }
        } catch (IOException | HeclException e) {
            throw new HeclException ("Image command failed: " + e.getLocalizedMessage());
        }
        
	return null;
    }
}

 

Ads help maintain this website.