Logo Search packages:      
Sourcecode: javamorph version File versions  Download package

CFrame.java

package javamorph;

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

/**
 * @version 1.1
 * <br/>
 * @author claus.erhard.wimmer@googlemail.com
 * <br/>
 * Program: JavaMorph V 1.1.
 * <br/>
 * Class: CFrame.
 * <br/>
 * License: GPLv2.
 * <br/>
 * Description: JComponent to show either the left picture of the right one.
 * <br/>
 * Hint: Paint operation also performed by the four decorators (a pattern).
 */
00024 public class CFrame extends JLabel {
    /** Java API. */
00026     private static final long serialVersionUID = 1L;
    /** Preferred paint size of this component. */ 
00028     public static final Dimension PREF_SIZE = new Dimension(400,300);
    /** Background color of this component. */
00030     public static final Color BACKGROUND = Color.black;
    /** Constant for pop up menu edit command: Edit the mesh. */
00032     public static final int EDIT_MESH = 1;
    /** Constant for pop up menu edit command: Edit the polygon. */ 
00034     public static final int EDIT_POLYGON = 2;
    /** Parent frame which lays out this component. */
00036     private CMain parent;
    /** Decorator to draw & edit the morph mesh. */
00038     private CMeshDecorator dmesh;
    /** Decorator to draw & edit the picture as subject of this program. */
00040     private CPictureDecorator dpicture;
    /** Decorator to draw & edit the clip polygon. */
00042     private CPolygonDecorator dpolygon;
    /** Decorator to provide the pop up menu. */
00044     private CPopupMenuDecorator dpopup;
    /** Double buffer image to avoid flickering effects. */
00046     private BufferedImage moffline;  
    /** Picture's mesh. */
00048     private Vector<Point> mesh;
    /** Picture's clip polygon points. */
00050     private Vector<Point> polygon;
    /** Picture itself. */
00052     private BufferedImage image;
    /** Polygon's smoothed clip matrix. */
00054     private double clip[][];
    /** File to store a debug copy of the clip matrix. */
00056     private File f_clip;
    /** 
     * Constructor.
     * 
     * @param parent Main JFrame.
     * @param mesh Picture's mesh.
     * @param polygon Clip polygon points.
     * @param image Picture itself.
     * @param clip Smoothed clip matrix.
     * @param f_mesh File to store to mesh to.
     * @param f_polygon File to store the polygon to.
     * @param f_clip File to debug the smoothed clip matrix to.
     */
00069     public CFrame(CMain parent, 
            Vector<Point> mesh,
            Vector<Point> polygon,
            BufferedImage image,
            double clip[][],
            File f_mesh,
            File f_polygon,
            File f_clip){
        /* Assign the arguments. */
        this.parent = parent;
        this.mesh = mesh;
        this.polygon = polygon;
        this.image = image;
        this.clip = clip;
        this.f_clip = f_clip;
        /* Init decorators. */
        dpolygon = 
            new CPolygonDecorator(this, polygon, image, f_polygon, clip, f_clip);
        dmesh = new CMeshDecorator(this, mesh, image, f_mesh);
        dpopup = new CPopupMenuDecorator(this);
        dpicture = new CPictureDecorator(this, image);
        /* Set color. */
        setBackground(CFrame.BACKGROUND);
        /* Set crosshair cursor. */
        setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    }
    /**
     * Paint this component to the screen. Call the decorators therefore.
     */
00098     public void paint(Graphics g){
        int r = 0, c = 0;
        Dimension size = this.getSize();
        /* Init double buffer. */
        this.moffline = new BufferedImage(
                size.width, 
                size.height, 
                BufferedImage.TYPE_INT_ARGB);
        Graphics o = this.moffline.getGraphics();
        o.setColor(this.getBackground());
        /* Draw checkerboard pattern. */
        for(int i = 0; i < size.width; i += 20, c += 1, r = 0){
            for(int j = 0; j < size.height; j +=  20, r += 1){
                if(0 == (c + r) % 2){
                    o.setColor(Color.lightGray);
                }else{
                    o.setColor(Color.gray);
                }
                o.fillRect(i, j, 20, 20);
            }
        }
        /* Set anit-alias. */
        ((Graphics2D)o).setRenderingHint
        (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        /* Call decorators. */
        dpicture.paint(o);
        dmesh.paint(o);
        dpolygon.paint(o);
        dpopup.paint(o);
        /* Draw onto screen. */
        g.drawImage(moffline, 0, 0, this);
    }
    /**
     * Java API.
     */
00133     public void update(Graphics g){
        this.paint(g);
    }
    /**
     * Java API. Constant used as return.
     */
00139     public Dimension getPreferredSize(){
        return CFrame.PREF_SIZE;
    }
    /**
     * Return the position & the size of the drawn image within the component.   
     * @return Bounds in screen pixel units relating to this component.
     */
00146     public Rectangle getImageBounds(){
        return this.dpicture.getBounds();
    }
    /**
     * Obtain the instance of the application's main class.
     */
00152     public CMain getParent(){
        return this.parent;
    }

    public void genClip(){
        this.dpolygon.genClip();
    }
    /**
     * Scale a screen point to a picture point.
     * @param p Point in screen resolution units.
     */
00163     public void scalePoint(Point p){
        /* Picture area on JComponent. */
        Rectangle bounds = getImageBounds();
        /* Picture's own size. */
        int w = image.getWidth(), h = image.getHeight();
        /* Scale. */
        double x, y;
        x = (p.x - bounds.x) * w / bounds.width;
        y = (p.y - bounds.y) * h / bounds.height;
        x = Math.max(0, Math.min(w, x));
        y = Math.max(0, Math.min(h, y));
        p.x = (int)x;
        p.y = (int)y;
        /* Limit relating the picture's own size. */
        p.x = Math.max(0, Math.min(image.getWidth() - 1, p.x));
        p.y = Math.max(0, Math.min(image.getHeight() - 1, p.y));
    }
    /** 
     * Scale mark & cursor size simultan.
     * (Regression.)
     * 
     * @return Mark size to be draw by scaling decorator.
     */
00186     public int scaleMarkSize(){
        /* Bounds = screen pixel size.*/
        /* Image = origin pixel size.*/
        return 
            CConfig.MARK_SIZE * 
            image.getWidth() /
            getImageBounds().width;
    }
    /** 
     * Delete content of mesh.
     * Initialize matrix controlled by configuration_s number of rows & columns.
     */
00198     public void initMesh(){
        dmesh.init();
    }
    /** 
     * Delete polygon.
     * Initialize controlled by configuration's number of points.
     */
00205     public void initPolygon(){
        dpolygon.init();
    }
}

Generated by  Doxygen 1.6.0   Back to index