001/*
002 * This file is part of OCaml-Java runtime.
003 * Copyright (C) 2007-2013 Xavier Clerc.
004 *
005 * OCaml-Java runtime is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * OCaml-Java runtime is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.ocamljava.runtime.wrappers;
020
021import java.awt.Canvas;
022import java.awt.Frame;
023import java.awt.image.BufferedImage;
024
025import org.ocamljava.runtime.context.Context;
026import org.ocamljava.runtime.context.CurrentContext;
027import org.ocamljava.runtime.primitives.otherlibs.graph.GraphSlot;
028import org.ocamljava.runtime.values.Value;
029
030/**
031 * The {@code OCamlGraphics} provides utility methods related to the
032 * {@code Graphics} module.
033 *
034 * @author <a href="mailto:xclerc@ocamljava.org">Xavier Clerc</a>
035 * @version 2.0
036 * @since 2.0
037 */
038public final class OCamlGraphics {
039
040    /**
041     * No instance of this class.
042     */
043    private OCamlGraphics() {
044    } // end empty constructor
045
046    /**
047     * Returns the {@code Graphics} frame of a context.
048     * @param ctxt context to get frame from - should not be {@code null}
049     * @return the {@code Graphics} frame of the passed context,
050     *         {@code null} if the {@code Graphics} library has not been
051     *         opened or is canvas-only
052     */
053    public static Frame getFrame(final Context ctxt) {
054        assert ctxt != null : "null ctxt";
055        final GraphSlot slot = (GraphSlot) ctxt.getCodeState().getSlot(GraphSlot.SLOT);
056        return slot != null
057            ? slot.getFrame()
058            : null;
059    } // end method 'getFrame(Context)'
060
061    /**
062     * Returns the {@code Graphics} frame of the current context.
063     * @return the {@code Graphics} frame of the current context,
064     *         {@code null} if the {@code Graphics} library has not been
065     *         opened or is canvas-only
066     */
067    public static Frame getFrame() {
068        final GraphSlot slot = (GraphSlot) CurrentContext.getCodeState().getSlot(GraphSlot.SLOT);
069        return slot != null
070            ? slot.getFrame()
071            : null;
072    } // end method 'getFrame()'
073
074    /**
075     * Returns the {@code Graphics} canvas of a context.
076     * @param ctxt context to get canvas from - should not be {@code null}
077     * @return the {@code Graphics} canvas of the passed context,
078     *         {@code null} if the {@code Graphics} library has not been
079     *         opened
080     */
081    public static Canvas getCanvas(final Context ctxt) {
082        assert ctxt != null : "null ctxt";
083        final GraphSlot slot = (GraphSlot) ctxt.getCodeState().getSlot(GraphSlot.SLOT);
084        return slot != null
085            ? slot.getCanvas()
086            : null;
087    } // end method 'getCanvas(Context)'
088
089    /**
090     * Returns the {@code Graphics} canvas of the current context.
091     * @return the {@code Graphics} canvas of the current context,
092     *         {@code null} if the {@code Graphics} library has not been
093     *         opened
094     */
095    public static Canvas getCanvas() {
096        final GraphSlot slot = (GraphSlot) CurrentContext.getCodeState().getSlot(GraphSlot.SLOT);
097        return slot != null
098            ? slot.getCanvas()
099            : null;
100    } // end method 'getCanvas()'
101
102    /**
103     * Returns the {@code Graphics} buffer of the current context. <br/>
104     * One should notice that a new buffer is created each time the frame is
105     * resized.
106     * @return the {@code Graphics} buffer of the current context,
107     *         {@code null} if the {@code Graphics} library has not been
108     *         opened
109     */
110    public static BufferedImage getBuffer() {
111        final GraphSlot slot = (GraphSlot) CurrentContext.getCodeState().getSlot(GraphSlot.SLOT);
112        return slot != null
113            ? slot.getBuffer()
114            : null;
115    } // end method 'getBuffer()'
116
117} // end class 'OCamlGraphics'