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: 4096
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:
 
 
 
Drawing pixels and showing the effect of glDrawPixels, glCopyPixels, and glPixelZoom E-mail
User Rating: / 12
PoorBest 

This program demonstrates drawing pixels and shows the effect of glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the mouse while pressing the mouse button will copy the image in the lower-left corner of the window to the mouse position, using the current pixel zoom factors. There is no attempt to prevent you from drawing over the original image. If you press the 'r' key, the original image and zoom factors are reset. If you press the 'z' or 'Z' keys, you change the zoom factors.

The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook11;

/**
 * This program demonstrates drawing pixels and shows the effect of
 * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
 * mouse while pressing the mouse button will copy the image in the lower-left
 * corner of the window to the mouse position, using the current pixel zoom
 * factors. There is no attempt to prevent you from drawing over the original
 * image. If you press the 'r' key, the original image and zoom factors are
 * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
 
 @author Kiet Le (java port)
 */

import java.awt.Point;
import java.awt.event.*;
import java.nio.ByteBuffer;

import javax.swing.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

/**
 * This program demonstrates drawing pixels and shows the effect of
 * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
 * mouse while pressing the mouse button will copy the image in the lower-left
 * corner of the window to the mouse position, using the current pixel zoom
 * factors. There is no attempt to prevent you from drawing over the original
 * image. If you press the 'r' key, the original image and zoom factors are
 * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
 
 @author Kiet Le (Java port)
 */
public class image
  extends JFrame
    implements GLEventListener, KeyListener, MouseMotionListener
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private GLU glu;
  private GLUT glut;
  //
  private static final int checkImageWidth = 64;
  private static final int checkImageHeight = 64;
  private static final int rgb = 3;
  private byte checkImage[][][];
  private ByteBuffer checkImageBuf = 
  BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * rgb);

  private static float zoomFactor = 1.0f;
  private static int height;
  private Point mousePoint;// = new Point(); 

  //
  public image()
  {
    super("image");// name of file as title in window bar
    /*
     * display mode (single buffer and RGBA)
     */
    caps = new GLCapabilities();
    caps.setDoubleBuffered(false);
    System.out.println(caps.toString());
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseMotionListener(this);
    //
    getContentPane().add(canvas);
  }

  /*
   * Declare initial window size, position, and set frame's close behavior. Open
   * window with "hello" in its title bar. Call initialization routines.
   * Register callback function to display graphics. Enter main loop and process
   * events.
   */
  public void run()
  {
    setSize(250250);
    setLocationRelativeTo(null)// center
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  public static void main(String[] args)
  {
    new image().run();
  }

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
    //
    gl.glClearColor(0.0f0.0f0.0f0.0f);
    gl.glShadeModel(GL.GL_FLAT);
    makeCheckImage();
    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
  }

  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    if (mousePoint != null)
    {
      int screeny = height - (int)mousePoint.getY();
      gl.glRasterPos2i(mousePoint.x, screeny);
      gl.glPixelZoom(zoomFactor, zoomFactor);
      gl.glCopyPixels(00, checkImageWidth, checkImageHeight, GL.GL_COLOR);
      //gl.glPixelZoom(1.0f, 1.0f);
      //mousePoint = null;
    }
    else gl.glRasterPos2i(00);

    gl.glDrawPixels(checkImageWidth, checkImageHeight, GL.GL_RGB,
        GL.GL_UNSIGNED_BYTE, checkImageBuf);

    gl.glFlush();
  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
    
    gl.glViewport(00, w, h);
    height = h;
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(0.0(doublew, 0.0(doubleh);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

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

  /*
   * 3D array won't be used. I left it here for you to see.
   */
  private void makeCheckImage()
  {
    byte c = (byte0xFF;

    for (int i = 0; i < checkImageWidth; i++)
    {
      for (int j = 0; j < checkImageHeight; j++)
      {
        // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
        c = (byte)( ( ((byte)((i & 0x8)==0?0x00:0xff)//
            ^(byte)((j & 0x8)==0?0x00:0xff))));
        // checkImage[i][j][0] = (byte) c;
        // checkImage[i][j][1] = (byte) c;
        // checkImage[i][j][2] = (byte) c;
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
      }
    }
    checkImageBuf.rewind();
  }//

  public void keyTyped(KeyEvent e)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyChar()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;
      case 'r':
      case 'R':
        zoomFactor = 1.0f;
        System.out.println("zoomFactor reset to 1.0\n");
        break;
      case 'z':
        zoomFactor += 0.5;
        if (zoomFactor >= 3.0zoomFactor = 3.0f;
        System.out.println("zoomFactor is now " + zoomFactor);
        break;
      case 'Z':
        zoomFactor -= 0.5;
        if (zoomFactor <= 0.5zoomFactor = 0.5f;
        System.out.println("zoomFactor is now " + zoomFactor);
        break;

      default:
        break;
    }
    canvas.display();
  }

  public void keyReleased(KeyEvent e)
  {
  }

  public void mouseDragged(MouseEvent e)
  {

  }

  public void mouseMoved(MouseEvent mouse)
  {
    mousePoint = mouse.getPoint();
    //screeny = height - (int) mouse.getY();
    canvas.display();
  }

}

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.