All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bibliothek.paint.model.Picture Maven / Gradle / Ivy

/*
 * Bibliothek - DockingFrames
 * Library built on Java/Swing, allows the user to "drag and drop"
 * panels containing any Swing-Component the developer likes to add.
 * 
 * Copyright (C) 2007 Benjamin Sigg
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Benjamin Sigg
 * [email protected]
 * CH - Switzerland
 */
package bibliothek.paint.model;

import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import bibliothek.util.xml.XElement;

/**
 * A set of {@link Shape}s forming some picture.
 * @author Benjamin Sigg
 *
 */
public class Picture {
    /** the shapes of this picture */
    private List shapes = new ArrayList();
    
    /** the observers of this picture */
    private List listeners = new ArrayList();
   
    /** the name of this picture */
    private String name;
    
    /**
     * Creates a new picture.
     * @param name the name of this picture
     */
    public Picture( String name ){
        this.name = name;
    }
    
    @Override
    public String toString() {
        return name;
    }
    
    /**
     * Writes the contents of this picture.
     * @param out the stream to write into
     * @throws IOException if an I/O error occurs
     */
    public void write( DataOutputStream out ) throws IOException{
        out.writeUTF( name );
        out.writeInt( shapes.size() );
        for( Shape shape : shapes ){
            ShapeUtils.write( shape, out );
        }
    }
    
    /**
     * Writes the contents of this picture.
     * @param element the element to write into
     */
    public void writeXML( XElement element ){
        element.addElement( "name" ).setString( name );
        XElement xshapes = element.addElement( "shapes" );
        for( Shape shape : shapes ){
            ShapeUtils.writeXML( shape, xshapes.addElement( "shape" ) );
        }
    }
    
    /**
     * Reads the contents of this picture from in.
     * @param in the stream to read from
     * @throws IOException if an I/O error occurs
     */
    public void read( DataInputStream in ) throws IOException{
        name = in.readUTF();
        shapes.clear();
        for( int i = 0, n = in.readInt(); itrue if there are no shapes in this picture
     */
    public boolean isEmpty(){
        return shapes.isEmpty();
    }
    
    /**
     * Deletes the newest {@link Shape} of this picture.
     */
    public void removeLast(){
        shapes.remove( shapes.size()-1 );
        for( PictureListener listener : listeners.toArray( new PictureListener[ listeners.size() ] ))
            listener.pictureChanged();
    }
    
    /**
     * Adds an observer to this picture.
     * @param listener the new observer
     */
    public void addListener( PictureListener listener ){
        listeners.add( listener );
    }
    
    /**
     * Removes an observer from this picture.
     * @param listener the listener to remove
     */
    public void removeListener( PictureListener listener ){
        listeners.remove( listener );
    }
    
    /**
     * Gets the name of this picture.
     * @return the name
     */
    public String getName() {
        return name;
    }
    
    /**
     * Paints all shapes of this picture.
     * @param g the graphics context
     * @param stretch a factor each coordinate will be multiplied with
     */
    public void paint( Graphics g, double stretch ){
        for( Shape shape : shapes )
            shape.paint( g, stretch );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy