java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Several overlapping filled polygons to demonstrate the effect order has on alpha blending results E-mail
User Rating: / 4
PoorBest 

This program draws several overlapping filled polygons to demonstrate the effect order has on alpha blending results. Use the 't' key to toggle the order of drawing polygons. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook10;

import javax.swing.*;
import java.awt.event.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*; 

/**
 * This program draws several overlapping filled polygons to demonstrate the
 * effect order has on alpha blending results. Use the 't' key to toggle the
 * order of drawing polygons.
 
 @author: Kiet Le (Java conversion)
 */
public class alpha
  extends JFrame
    implements GLEventListener, KeyListener
{
  private GLCanvas canvas;

  public alpha()
  {
    super("alpha");

    canvas = new GLCanvas();
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
   
    getContentPane().add(canvas);
  }

  public void run()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500500);
    setLocationRelativeTo(null);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  /*
   * Main Loop Open window with initial window size, title bar, RGBA display
   * mode, and handle input events.
   */
  public static void main(String[] args)
  {
    new alpha().run();
  }

  /*
   * Initialize alpha blending function.
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    //
    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glShadeModel(GL.GL_FLAT);
    gl.glClearColor(0000);
  }

  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    //
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glColor4f(1.0f1.0f0.0f0.75f);
    gl.glRectf(0.0f0.0f0.5f1.0f);

    gl.glColor4f(0.0f1.0f1.0f0.75f);
    gl.glRectf(0.0f0.0f1.f0.5f);
    /* draw colored polygons in reverse order in upper right */
    gl.glColor4f(0.f1.0f1.0f0.75f);
    gl.glRectf(0.5f0.5f1.0f1.0f);

    gl.glColor4f(1.0f1.0f0.0f0.75f);
    gl.glRectf(0.5f0.5f1.0f1.0f);

    gl.glEnd();
  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
    GLU glu = new GLU();
 
    gl.glViewport(00, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    if (w <= hglu.gluOrtho2D(0.01.00.01.0 * h / w);
    else glu.gluOrtho2D(0.01.0 * w / h, 0.01.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
      boolean deviceChanged)
  {
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
     switch (key.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;

      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key)
  {
  }

}

Source: Kiet Le's The Red Book Examples using JOGL


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.