draw Poly openGL - Java javax.media.opengl

Java examples for javax.media.opengl:GL

Description

draw Poly openGL

Demo Code


import java.awt.Color;
import javax.media.opengl.GL;

public class Main{
    public static void drawPoly(GL gl, Color color, Color stroke,
            Point one, Point two, Point three) {
        drawPoly(gl, color, color, color, null, stroke, one, two, three,
                null);/*from   w w w .j  a va 2  s .co  m*/
    }
    public static void drawPoly(GL gl, Color color, Color stroke,
            Point one, Point two, Point three, Point four) {
        drawPoly(gl, color, color, color, color, stroke, one, two, three,
                four);
    }
    public static void drawPoly(GL gl, Color colorOne, Color colorTwo,
            Color colorThree, Color stroke, Point one, Point two,
            Point three) {
        drawPoly(gl, colorOne, colorTwo, colorThree, null, stroke, one,
                two, three, null);
    }
    public static void drawPoly(GL gl, Color colorOne, Color colorTwo,
            Color colorThree, Color colorFour, Color stroke, Point one,
            Point two, Point three, Point four) {
        // Draw the shape

        if (four == null) {
            gl.glBegin(GL.GL_TRIANGLES);
        } else {
            gl.glBegin(GL.GL_QUADS);
        }

        setColor(gl, colorOne);
        gl.glVertex2i(one.x, one.y);
        setColor(gl, colorTwo);
        gl.glVertex2i(two.x, two.y);
        setColor(gl, colorThree);
        gl.glVertex2i(three.x, three.y);

        if (four != null) {
            setColor(gl, colorFour);
            gl.glVertex2i(four.x, four.y);
        }

        gl.glEnd();

        // Draw the stroke

        setColor(gl, stroke);
        gl.glBegin(GL.GL_LINE_LOOP);
        gl.glVertex2i(one.x, one.y);
        gl.glVertex2i(two.x, two.y);
        gl.glVertex2i(three.x, three.y);

        if (four != null) {
            gl.glVertex2i(four.x, four.y);
        }

        gl.glEnd();
    }
    public static void setColor(GL gl, Color color) {
        if (color != null) {
            float red = color.getRed() / 255f;
            float green = color.getGreen() / 255f;
            float blue = color.getBlue() / 255f;
            float alpha = color.getAlpha() / 255f;

            gl.glColor4f(red, green, blue, alpha);
        }
    }
}

Related Tutorials