draw Rounded Rect opengl - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

draw Rounded Rect opengl

Demo Code


import javax.media.opengl.GL2;

public class Main{
    public static void drawRoundedRect(GL2 gl2, double x, double y,
            double z, double width, double height, double offset,
            int arcSeg, float c1[], float c2[]) {
        double wsize = width;
        double hsize = height;

        // draw center
        drawQuadFan(gl2, x, y, z, wsize - offset, hsize - offset, c1, c2);
        /*/*from ww w .  j a  va 2  s.com*/
        gl2.glBegin(GL2.GL_QUADS);
           gl2.glVertex3d(x-wsize+offset, y-hsize+offset, z);
           gl2.glVertex3d(x+wsize-offset, y-hsize+offset, z);
           gl2.glVertex3d(x+wsize-offset, y+hsize-offset, z);
           gl2.glVertex3d(x-wsize+offset, y+hsize-offset, z);
        gl2.glEnd();
         */

        // draw upper
        if (c2 != null)
            gl2.glColor4fv(c2, 0);
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3d(x - wsize + offset, y + hsize - offset, z);
        gl2.glVertex3d(x + wsize - offset, y + hsize - offset, z);
        gl2.glVertex3d(x + wsize - offset, y + hsize, z);
        gl2.glVertex3d(x - wsize + offset, y + hsize, z);
        gl2.glEnd();

        // draw lower
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3d(x - wsize + offset, y - hsize + offset, z);
        gl2.glVertex3d(x + wsize - offset, y - hsize + offset, z);
        gl2.glVertex3d(x + wsize - offset, y - hsize, z);
        gl2.glVertex3d(x - wsize + offset, y - hsize, z);
        gl2.glEnd();

        // draw west
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3d(x - wsize, y - hsize + offset, z);
        gl2.glVertex3d(x - wsize + offset, y - hsize + offset, z);
        gl2.glVertex3d(x - wsize + offset, y + hsize - offset, z);
        gl2.glVertex3d(x - wsize, y + hsize - offset, z);
        gl2.glEnd();

        // draw east
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3d(x + wsize - offset, y - hsize + offset, z);
        gl2.glVertex3d(x + wsize, y - hsize + offset, z);
        gl2.glVertex3d(x + wsize, y + hsize - offset, z);
        gl2.glVertex3d(x + wsize - offset, y + hsize - offset, z);
        gl2.glEnd();

        drawPie(gl2, x + wsize - offset, y + hsize - offset, z, offset,
                0.0, 90.0, arcSeg); // upper-right
        drawPie(gl2, x - wsize + offset, y + hsize - offset, z, offset,
                90.0, 180.0, arcSeg); // upper-left
        drawPie(gl2, x - wsize + offset, y - hsize + offset, z, offset,
                180.0, 270.0, arcSeg); // lower-right
        drawPie(gl2, x + wsize - offset, y - hsize + offset, z, offset,
                270, 360.0, arcSeg); // lower-left
    }
    public static void drawQuadFan(GL2 gl2, double x, double y, double z,
            double w, double h, float c1[], float c2[]) {
        gl2.glBegin(GL2.GL_TRIANGLE_FAN);
        if (c1 != null)
            gl2.glColor4fv(c1, 0);
        gl2.glVertex3d(x, y, z); // center

        if (c2 != null)
            gl2.glColor4fv(c2, 0);
        gl2.glVertex3d(x - w, y - h, z); // lower left
        gl2.glVertex3d(x + w, y - h, z); // lower right
        gl2.glVertex3d(x + w, y + h, z); // upper right
        gl2.glVertex3d(x - w, y + h, z); // upper left

        gl2.glVertex3d(x - w, y - h, z); // bring it back
        gl2.glEnd();
    }
    public static void drawQuadFan(GL2 gl2, double x, double y, double z,
            double w, double h) {
        drawQuadFan(gl2, x, y, z, w, h, null, null);
    }
    public static void drawPie(GL2 gl2, double x, double y, double z,
            double r, double start, double end, int seg) {
        double ang = (end - start) / (double) seg;
        gl2.glBegin(GL2.GL_TRIANGLE_FAN);
        gl2.glVertex3d(x, y, z);

        for (int i = 0; i <= seg; i++) {
            double rx = r * Math.cos((start + ang * i) * 3.1416 / 180.0);
            double ry = r * Math.sin((start + ang * i) * 3.1416 / 180.0);
            gl2.glVertex3d(x + rx, y + ry, z);
        }
        gl2.glEnd();
    }
}

Related Tutorials