• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Best double buffer scheme in 1.1 applets

This topic is 5553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm still using 1.1 for applet support. I was wondering what people think the best (i.e. fastest, least flicker) double buffer scheme is? Currently I'm creating an image with MemoryImageSource, manipulating the pixels in run(), and copying that image to the Graphics object using drawImage every time paint is called. I still seem to get a bit of flicker now and then then, especially during input handling. [edited by - Absolution on December 5, 2002 5:48:44 PM]

Share this post


Link to post
Share on other sites
Advertisement

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class spiralColor extends Applet
implements MouseListener, MouseMotionListener
{
Graphics bufferg; // Sets bufferg for graphic use
Image buffer; // Image to be used to draw next frame
double[] xSpiral = new double[129];
double[] ySpiral = new double[xSpiral.length];
int[] imageXPostion = { 316, 354, 394};
int imageYPosition =248;
int[] green = new int[xSpiral.length]; int[] red = new int[xSpiral.length]; int[] blue = new int[xSpiral.length];
int xMouse, yMouse, R, G, B, incRed , incGreen , incBlue, xPosition = 2;
int spiralXCenter = 147, spiralYCenter = 142;
boolean redColor, blueColor = true, greenColor, start;

public void init()
{
setBackground(Color.white); // sets the background color
buffer = createImage(getSize().width , getSize().height);//Createsthe next frame to be displayed
bufferg = buffer.getGraphics();//Calls the back frame
addMouseListener(this);
addMouseMotionListener(this);
}

public void update(Graphics gr)
{
paint(bufferg); // Updates paint
gr.drawImage(buffer , 0, 0, this);// Draws the next frame
}

public void paint(Graphics gr)
{
double Pi = 3.1415926535897932384626433832795;
int spiralRadius = 20, colorTouched =0;
if(redColor ){ R = 254; G = 0; B = 0; incRed = 0; incGreen = 2; incBlue =2;}
if(blueColor ){ R = 0; G = 0; B = 254; incRed = 2; incGreen = 2; incBlue =0;}
if(greenColor){ R = 0; G = 254; B = 0; incRed = 2; incGreen = 0; incBlue =2;}
gr.setColor(getBackground());// Sets up the color for the erasing frame
gr.fillRect(0 , 0,getSize().width, getSize().height);//Erases the old frames
gr.setColor(Color.black);
gr.drawString("V", imageXPostion[xPosition], imageYPosition);
gr.drawRect(0,0,20,20);
gr.drawRect(0,0, getSize().width - 1, getSize().height-1);
gr.drawLine(getSize().width - 120,249,getSize().width,249);
gr.drawRect(getSize().width -81,249,40,39);
gr.drawRect(getSize().width - 121, 0, getSize().width - 121, getSize().height);
gr.drawString("Green:",getSize().width - 115, 30);
gr.drawString("Red :",getSize().width - 115, 45);
gr.drawString("Blue :",getSize().width - 115, 60);
gr.setColor(Color.red); gr.fillRect(getSize().width - 120,250, 39, 39);
gr.setColor(Color.green); gr.fillRect(getSize().width - 80,250, 39,39);
gr.setColor(Color.blue); gr.fillRect(getSize().width - 40,250, 39, 39);

for(int i = xSpiral.length -1; i>0 ; i--)
{
green = G; blue[i] = B; red[i] = R;
xSpiral[i] = spiralXCenter - (spiralRadius)*Math.sin(Pi);
ySpiral[i] = spiralYCenter + (spiralRadius)*Math.cos(Pi);
spiralRadius +=1; Pi -= (3.1415926535897932384626433832795/10);
gr.setColor(new Color(red[i],green[i],blue[i]));
gr.fillRect((int)xSpiral[i],(int)ySpiral[i],15,15);
if( xMouse>= (int)xSpiral[i] && xMouse <= (int)xSpiral[i] + 15)
if( yMouse>= (int)ySpiral[i] && yMouse <= (int)ySpiral[i] +15 && colorTouched !=1)
{
gr.fillRect(0,0,20,20);
gr.setColor(Color.black);
gr.drawString("" + G ,getSize().width - 75, 30);
gr.drawString("" + R ,getSize().width - 75, 45);
gr.drawString("" + B ,getSize().width - 75, 60);
colorTouched =1;
}
R += incRed; B +=incBlue; G +=incGreen;
gr.setColor(Color.black);
gr.drawRect((int)xSpiral[i],(int)ySpiral[i],15,15);
}
if(colorTouched ==0) setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
else setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// CROSSHAIR_CURSOR, DEFAULT_CURSOR,
// E_RESIZE_CURSOR, HAND_CURSOR
// MOVE_CURSOR, N_RESIZE_CURSOR,
// NE_RESIZE_CURSOR, NW_RESIZE_CURSOR;
// S_RESIZE_CURSOR,SE_RESIZE_CURSOR,
// SW_RESIZE_CURSOR, TEXT_CURSOR,
// W_RESIZE_CURSOR, WAIT_CURSOR
}

public void mouseDragged(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mousePressed(MouseEvent evt)
{
int x = evt.getX(); int y = evt.getY();
if(x>=getSize().width - 120 && x<=getSize().width - 81)
if(y>=250 && y <= 290){ redColor = true; blueColor = false; greenColor = false; xPosition =0; repaint();}
if(x>=getSize().width - 80 && x<=getSize().width - 41)
if(y>=250 && y <= 290){ redColor = false; blueColor = false; greenColor = true; xPosition =1; repaint();}
if(x>=getSize().width - 40 && x<=getSize().width - 1)
if(y>=250 && y <= 290){ redColor = false; blueColor = true; greenColor = false; xPosition =2; repaint();}
}
public void mouseClicked(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){ repaint();}
public void mouseMoved(MouseEvent evt)
{
int x = evt.getX(); int y = evt.getY();
xMouse = x; yMouse = y;
repaint();
}

} // end class spiralColor

Share this post


Link to post
Share on other sites

  • Advertisement