⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 framefactory.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
Copyright (C) 2001, 2006, 2007 United States Government
as represented by the Administrator of the
National Aeronautics and Space Administration.
All Rights Reserved.
*/

package gov.nasa.worldwind.render;

import com.sun.opengl.util.BufferUtil;
import gov.nasa.worldwind.util.Logging;

import javax.media.opengl.GL;
import java.awt.*;
import java.nio.DoubleBuffer;

/**
 * Static class for drawing 2D frames.
 * <p>
 * All shapes are drawn inside a bounding rectangle whose lower left corner
 * is at the origin. Shapes with a leader use an offset point that indicate where the
 * leader triangle should point at - it usually has a negative y since the leader connects
 * at the bottom of the frame (at y = 0).
 * </p>
 * @author Patrick Murris
 * @version $Id: FrameFactory.java 10542 2009-04-27 17:28:30Z dcollins $
 * @see AbstractAnnotation
 */
public class FrameFactory
{
    public static final String SHAPE_RECTANGLE = "Render.FrameFactory.ShapeRectangle";
    public static final String SHAPE_ELLIPSE = "Render.FrameFactory.ShapeEllipse";
    public static final String SHAPE_NONE = "Render.FrameFactory.ShapeNone";

    public static final String LEADER_TRIANGLE = "Render.FrameFactory.LeaderTriangle";
    public static final String LEADER_NONE = "Render.FrameFactory.LeaderNone";

    private static int cornerSteps = 16;
    private static int leaderGapWidth = 40;
    private static int circleSteps = 64;

    /**
     * Draw a shape with the specified width and height, gl mode and corner radius. GL mode came be one of
     * <code>GL.GL_TRIANGLE_FAN</code> and <code>GL.LINE_STRIP</code>. Corner radius only apply
     * to <code>SHAPE_RECTANGLE</code> - set to zero for square corners.
     * @param dc the current <code>DrawContext</code>.
     * @param shape the shape - can be one of <code>SHAPE_RECTANGLE</code> or <code>SHAPE_ELLIPSE</code>.
     * @param width the width of the overall shape.
     * @param height the height of the shape.
     * @param glMode the GL mode - can be one of <code>GL.GL_TRIANGLE_FAN</code> and <code>GL.LINE_STRIP</code>.
     * @param cornerRadius the rounded corners radius. Set to zero for square corners.
     */
    public static void drawShape(DrawContext dc, String shape, double width, double height, int glMode, int cornerRadius)
    {
        if (!shape.equals(SHAPE_NONE))
            drawBuffer(dc, glMode, createShapeBuffer(shape, width, height, cornerRadius, null));
    }

    /**
     * Draw a shape with the specified width and height, gl mode and corner radius. The shape includes a
     * leader triangle pointing to a specified point. GL mode came be one of <code>GL.GL_TRIANGLE_FAN</code>
     * and <code>GL.LINE_STRIP</code>. Corner radius only apply to <code>SHAPE_RECTANGLE</code> - set to zero for square corners.
     * @param dc the current <code>DrawContext</code>.
     * @param shape the shape - can be one of <code>SHAPE_RECTANGLE</code> or <code>SHAPE_ELLIPSE</code>.
     * @param width the width of the overall shape.
     * @param height the height of the shape excluding the leader.
     * @param leaderOffset the coordinates of the point to which the leader leads.
     * @param glMode the GL mode - can be one of <code>GL.GL_TRIANGLE_FAN</code> and <code>GL.LINE_STRIP</code>.
     * @param cornerRadius the rounded corners radius. Set to zero for square corners.
     */
    public static void drawShapeWithLeader(DrawContext dc, String shape, double width, double height, Point leaderOffset, int glMode, int cornerRadius)
    {
        if (!shape.equals(SHAPE_NONE))
            drawBuffer(dc, glMode, createShapeWithLeaderBuffer(shape, width, height, leaderOffset, cornerRadius, null));
    }

    /**
     * Create a vertex buffer for a shape with the specified width, height and corner radius. Corner radius only apply
     * to <code>SHAPE_RECTANGLE</code> - set to zero for square corners.
     * @param shape the shape - can be one of <code>SHAPE_RECTANGLE</code> or <code>SHAPE_ELLIPSE</code>.
     * @param width the width of the overall shape.
     * @param height the height of the shape.
     * @param cornerRadius the rounded corners radius. Set to zero for square corners.
     * @return the vertex buffer.
     */
    public static DoubleBuffer createShapeBuffer(String shape, double width, double height, int cornerRadius, DoubleBuffer buffer)
    {
        if (shape.equals(SHAPE_RECTANGLE))
            return createRoundedRectangleBuffer(width, height, cornerRadius, buffer);
        else if (shape.equals(SHAPE_ELLIPSE))
            return createEllipseBuffer(width, height, circleSteps, buffer);
        else if (shape.equals(SHAPE_NONE))
            return null;
        else
            // default to rectangle if shape unknown
            return createRoundedRectangleBuffer(width, height, cornerRadius, buffer);

    }

    /**
     * Create a vertex buffer for a shape with the specified width, height and corner radius. The shape includes a
     * leader triangle pointing to a specified point. Corner radius only apply to <code>SHAPE_RECTANGLE</code>
     * - set to zero for square corners.
     * @param shape the shape - can be one of <code>SHAPE_RECTANGLE</code> or <code>SHAPE_ELLIPSE</code>.
     * @param width the width of the overall shape.
     * @param height the height of the shape excluding the leader.
     * @param leaderOffset the coordinates of the point to which the leader leads.
     * @param cornerRadius the rounded corners radius. Set to zero for square corners.
     * @return the vertex buffer.
     */
    public static DoubleBuffer createShapeWithLeaderBuffer(String shape, double width, double height, Point leaderOffset, int cornerRadius, DoubleBuffer buffer)
    {
        if (shape.equals(SHAPE_RECTANGLE))
            return createRoundedRectangleWithLeaderBuffer(width, height, leaderOffset, cornerRadius, buffer);
        else if (shape.equals(SHAPE_ELLIPSE))
            return createEllipseWithLeaderBuffer(width, height, leaderOffset, circleSteps, buffer);
        else if (shape.equals(SHAPE_NONE))
            return null;
        else
            // default to rectangle if shape unknown
            return createRoundedRectangleWithLeaderBuffer(width, height, leaderOffset, cornerRadius, buffer);
    }

    /**
     * Draw a vertex buffer in a given gl mode. Vertex buffers coming from the createShapeBuffer() methods support
     * both <code>GL.GL_TRIANGLE_FAN</code> and <code>GL.LINE_STRIP</code>.
     * @param dc the current DrawContext.
     * @param mode the desired drawing GL mode.
     * @param count the number of vertices to draw.
     * @param verts the vertex buffer to draw.
     */
    public static void drawBuffer(DrawContext dc, int mode, int count, DoubleBuffer verts)
    {
        if (dc == null)
        {
            String message = Logging.getMessage("nullValue.DrawContextIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }
        if (verts == null)
        {
            String message = Logging.getMessage("nullValue.BufferIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        GL gl = dc.getGL();
        // Set up
        gl.glPushClientAttrib(GL.GL_CLIENT_VERTEX_ARRAY_BIT);
        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL.GL_DOUBLE, 0, verts);
        // Draw
        gl.glDrawArrays(mode, 0, count);
        // Restore
        gl.glPopClientAttrib();
    }

    /**
     * Draw a vertex buffer with texture coordinates in a given gl mode. Vertex buffers coming from the
     * createShapeBuffer() methods support both <code>GL.GL_TRIANGLE_FAN</code> and <code>GL.LINE_STRIP</code>.
     * @param dc the current DrawContext.
     * @param mode the desired drawing GL mode.
     * @param count the number of vertices to draw.
     * @param verts the vertex buffer to draw.
     */
    public static void drawBuffer(DrawContext dc, int mode, int count, DoubleBuffer verts, DoubleBuffer coords)
    {
        if (dc == null)
        {
            String message = Logging.getMessage("nullValue.DrawContextIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }
        if (verts == null || coords == null)
        {
            String message = Logging.getMessage("nullValue.BufferIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }
        
        GL gl = dc.getGL();
        // Set up
        gl.glPushClientAttrib(GL.GL_CLIENT_VERTEX_ARRAY_BIT);
        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(2, GL.GL_DOUBLE, 0, verts);
        gl.glTexCoordPointer(2, GL.GL_DOUBLE, 0, coords);
        // Draw
        gl.glDrawArrays(mode, 0, count);
        // Restore
        gl.glPopClientAttrib();
    }

    public static void drawBuffer(DrawContext dc, int mode, DoubleBuffer verts)
    {
        if (dc == null)
        {
            String message = Logging.getMessage("nullValue.DrawContextIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -