Can anyone help me!

I am new to Java 2D and I am trying to drawing circles on an image.
At the moment I am using BufferedImage and trying to use the technique,
'Double Buffering'

The 'Double Buffering' was meant to prevent any flickering, but when I
click the mouse the image to add a circle, the display flickers.

Can anyone point out my errors or provide me with some better code

Thanx

David
-------------------------------
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.Ellipse2D ;
import java.io.*;

import java.util.Stack ;   // Use for the Stack class

import com.sun.image.codec.jpeg.*;



public class DrawCirclesOnImage extends Component {

   private String imageToLoad ;
   private BufferedImage imgOffScreenJPEG;
   private BufferedImage bufferImage ;

   private Stack coordsStack ;
   private int coordsCount ;


   public DrawCirclesOnImage( String file ) {
     coordsCount = 0 ;
     imageToLoad = file ;

     coordsStack = new Stack() ;
   }


   public void paint(Graphics g) {
     Graphics2D g2 = (Graphics2D)g;

     // If the offscreen image is not defined, create it.
     if (imgOffScreenJPEG == null)
        createOffscreenImage();

     Dimension d = getSize();
     int w = d.width, h = d.height;
     bufferImage = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB ) ;
     bufferImage = makeBufferedImage( imgOffScreenJPEG,
BufferedImage.TYPE_INT_RGB ) ;

     drawGPSObjects( ( Graphics2D )bufferImage.getGraphics() ) ;

     g2.drawImage( bufferImage, null, null );
   }


   private void createOffscreenImage() {
     // Create a BufferedImage the same size as this component.
     Dimension d = getSize();

     int w = d.width, h = d.height;

     imgOffScreenJPEG = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB ) ;

     // Obtain the Graphics2D for the offscreen image.
     Graphics2D g2 = imgOffScreenJPEG.createGraphics();

     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

     // Load an image from a file.
     try {
       InputStream in = getClass().getResourceAsStream(imageToLoad);
       JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
       BufferedImage image = decoder.decodeAsBufferedImage();
       in.close();

       // Draw the loaded image on the offscreen image.
       g2.drawImage(image, 0, 0, w, h, null);

     }
     catch (Exception e) {
       System.out.print(e);
     }
   }


   private void drawGPSObjects( Graphics2D g2 ) {

     int OBJECT_CIRCLE_SIZE = 60 ;

     Stack tempStack = ( Stack )coordsStack.clone() ;
     Dimension tempDim ;

     // Set composite so that circles are draw half see-through
     g2.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER,
( float )0.3 ) ) ;

     while( !tempStack.isEmpty() ) {

       tempDim = new Dimension( ( Dimension )tempStack.pop() ) ;

       Ellipse2D e = new Ellipse2D.Double( tempDim.width - (
OBJECT_CIRCLE_SIZE / 2),
                   tempDim.height - ( OBJECT_CIRCLE_SIZE / 2 ),
OBJECT_CIRCLE_SIZE, OBJECT_CIRCLE_SIZE ) ;

       g2.setPaint( Color.red ) ;
       g2.fill( e ) ;
       g2.draw( e ) ;
     }

     // Reset composite
     g2.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER ) ) ;
   }


   public void addCoordinate( int x, int y ) {
     if( coordsCount == 10 )
       return ;

     Dimension coords = new Dimension( x, y ) ;

     coordsStack.push( coords ) ;
     coordsCount++ ;

     repaint() ;
   }


   public void update( Graphics g ) {
     paint( g ) ;
   }


   private BufferedImage makeBufferedImage( Image image, int imageType ) {

     BufferedImage bImage = new BufferedImage( image.getWidth(null),
image.getHeight(null), imageType ) ;

     Graphics2D g2 = bImage.createGraphics() ;

     g2.drawImage( image, null, null ) ;

     return bImage ;
   }

}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to