Package net.codebuilders.desktop.imagetools

Source Code of net.codebuilders.desktop.imagetools.ImageToolsModel

/*
* Copyright (C) 2009 Carl B. Marcum - CodeBuilders.net
* Phone (937) 242-6519 - <http://www.codebuilders.net>
*
* This file is part of "Image Tools by CodeBuilders.net"
* hereafter referered to as "Image Tools".
*
* Image Tools 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 3 of the License, or
* (at your option) any later version.
*
* Image Tools 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 Image Tools.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
* ImageToolsModel.java
*/
package net.codebuilders.desktop.imagetools;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.awt.Color;
import java.awt.Font;

/**
*
* @author Carl Marcum
*/
public class ImageToolsModel {

    private String dirPath = "";
    private String fileName = "";
    private String fileExt = "";

    private String text = "XXX";
    private String fontName = Font.SANS_SERIF; // "SansSerif"
    private int fontSize = 14;
    private float textBoxWidth = (float)200.0;
    private Color color = Color.RED;
    Robot robot = null;

    // current image between undo and redo queue
    private BufferedImage image = null;
    public LinkedList<BufferedImage> undoQueue = null;
    public LinkedList<BufferedImage> redoQueue = null;

    // class for firing property change events
    public PropertyChangeSupport pcs = null;

    /** Creates new form ImageToolsModel */
    public ImageToolsModel() {

        undoQueue = new LinkedList<BufferedImage>();

        redoQueue = new LinkedList<BufferedImage>();

        pcs = new PropertyChangeSupport(this);

    }

    String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileExt() {
        return fileExt;
    }

    public void setFileExt(String fileExt) {
        this.fileExt = fileExt;
    }

    public void capture() {
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.sync();
        Rectangle ecran = new Rectangle(tk.getScreenSize());
        try {
            robot = new Robot();
        } catch (java.awt.AWTException awte) {
            awte.printStackTrace();
        }

        robot.setAutoDelay(0);
        robot.setAutoWaitForIdle(false);
        // wait 4 seconds
        robot.delay(4000);
        setImage(robot.createScreenCapture(ecran));
    //You get a BufferedImage in TYPE_INT_BGR.

    // File file = new File("CapImageIO.png");
//        File file = new File(dirPath, (fileName + ".png"));
//        try {
//            javax.imageio.ImageIO.write(getImage(), "PNG", file);
//        } catch (java.io.IOException ioe) {
//            ioe.printStackTrace();
//        }

    }

    /**
     * @return the image
     */
    public BufferedImage getImage() {
        BufferedImage image = this.image;

        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(BufferedImage image) {

        // DEBUG
        System.out.println("entering Model setImage");

        // event test 2009-03-20
        BufferedImage old = this.image;


        // Test for property change - 2009-03-21
        // objects have to be different to fire a change
        this.image = new BufferedImage(image.getWidth(), image.getHeight(),
                BufferedImage.TYPE_INT_RGB);

        // set the new image
        this.image = image;

        // if there is an image copy it to undo queue
        if (old != null) {
            // copy the current image into undoQueue
            this.undoQueue.push(old);
            // DEBUG
            System.out.println("image added to undoQueue");
            System.out.println("undoQueue has " + this.undoQueue.size());
        }

        // Empty the redoQueue if it has items
        if (!this.redoQueue.isEmpty()) {
            this.redoQueue.clear();
            System.out.println("Redo Queue cleared");
        }


        // DEBUG
        System.out.println("Model firePropertyChange");
        if (old != null) { // in case the is no previous image
            System.out.println(old.toString());
        }
        System.out.println(this.image.toString());
        pcs.firePropertyChange("image", old, this.image);
        if (pcs.hasListeners("image")) {
            System.out.println("image has listeners");
        } else {
            System.out.println("image has no listeners");
        }



    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * @param text the text to set
     */
    public void setText(String text) {
        String oldText = getText();
        this.text = text;
        pcs.firePropertyChange("text", oldText, this.text);
    }

    /**
     * @return the fontSize
     */
    public int getFontSize() {
        return fontSize;
    }

    /**
     * @param fontSize the fontSize to set
     */
    public void setFontSize(int fontSize) {
        int oldSize = this.getFontSize();
        this.fontSize = fontSize;
        pcs.firePropertyChange("fontSize", oldSize, fontSize);
    }

    public void undo() {
        // action should be disabled if undoQueue is empty
        if (this.undoQueue.isEmpty()) {
            return;
        } else {
            // copy the current image into redoQueue
            this.redoQueue.push(this.getImage());
            // move the first undoQueue element to current
            this.image = this.undoQueue.pop();
            // DEBUG
            System.out.println("undoQueue has " + this.undoQueue.size());
            System.out.println("redoQueue has " + this.redoQueue.size());
        }

    }

    public void redo() {
        // action should be disabled if redoQueue is empty
        if (this.redoQueue.isEmpty()) {
            return;
        } else {
            // copy the current image into undoQueue
            this.undoQueue.push(this.getImage());
            // move the first redoQueue element to current
            this.image = this.redoQueue.pop();
            // DEBUG
            System.out.println("undoQueue has " + this.undoQueue.size());
            System.out.println("redoQueue has " + this.redoQueue.size());
        }
    }

    /**
     * @return the textBoxWidth
     */
    public float getTextBoxWidth() {
        return textBoxWidth;
    }

    /**
     * @param textBoxWidth the textBoxWidth to set
     */
    public void setTextBoxWidth(float textBoxWidth) {
        this.textBoxWidth = textBoxWidth;
    }

    /**
     * @return the color
     */
    public Color getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(Color color) {
        this.color = color;
       
    }

    /**
     * @return the fontName
     */
    public String getFontName() {
        return fontName;
    }

    /**
     * @param fontName the fontName to set
     */
    public void setFontName(String fontName) {
        String oldFontName = this.getFontName();
        this.fontName = fontName;
        pcs.firePropertyChange("fontName", oldFontName, this.fontName);
    }
}
TOP

Related Classes of net.codebuilders.desktop.imagetools.ImageToolsModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.