Skip to main content
gamedev.net
🔒 Locked

[java] Graphics buffers

Started by Slayer-X 6 replies 1.5k views
Original Post
Slayer-X
Slayer-X
I''m having trouble with graphics in an applet I''m making. When I draw everything in this world builder to the screen, it flickers where I''m drawing it. I made an optimized paint method, which draws only the latest thing modified, and everything except the last thing drawn gets erased (this also happens when another window is over the graphical area of the world builder). How would I go about buffering my graphics and copying it to the screen (or fixing the problem if I don''t have to buffer)?
JavaCoolDude
JavaCoolDude

import java.awt.*;

public class GraphApplet extends java.applet.Applet
{
int numberOfLines =100;
Graphics bufferg; // Sets bufferg for graphic use
Image buffer; // Image to be used to draw next frame,
public Image newPic;
int[] linesY = new int[numberOfLines];
int[] linesTrueLen = new int[numberOfLines];
int[] linesLength = new int[numberOfLines];
boolean change = true;

public void init()
{
setBackground(Color.cyan);
for (int i = 0 ; i {
linesLength = i;
linesY[i] = i;
linesTrueLen[i] = -1;
}

for (int i = 0 ; i {
while(linesTrueLen[i] == -1)
{
int pickNum = (int)(100*Math.random());
linesTrueLen[i] = linesLength[pickNum];
linesLength[pickNum] =-1;
}
}
buffer = createImage(getSize().width , getSize().height);//Createsthe next frame to be displayed
bufferg = buffer.getGraphics();//Calls the back frame
}
public void update(Graphics g)
{
paint(bufferg); // Updates paint
g.drawImage(buffer , 0, 0, this);// Draws the next frame
}

public void paint(Graphics g)
{
if(!change)
System.exit(0);
int holdLength ;
change = false;
g.clearRect(0,0,getSize().width, getSize().height);
g.setColor(Color.black);

for (int i = 0 ; i g.drawLine(0,linesY[i],linesTrueLen[i],linesY[i]);

for (int i =0 ; i {
if(linesTrueLen[i+1] {
holdLength = linesTrueLen[i+1];
linesTrueLen[i+1] = linesTrueLen[i];
linesTrueLen[i]= holdLength ;
change = true;
}
}

try
{
Thread.sleep(100);
}
catch(InterruptedException e){}
repaint();
}

}

Hope this helps.
PS: You should look for the update() method
Slayer-X
Slayer-X
Ok, so let''s say I create the following:

Graphics graphBuffer;
Image buffer;
int rows, cols;
int pixels[];
pixels = new int[cols][rows];

I make my own fillRect and text drawing methods to write the pixel values into the array, then use:

buffer = createImage(new MemoryImageSource(cols, rows, pixels, 0, cols));

To make the buffer from the array of pixels. Would this work? If so, how fast would this be? As long as it doesn''t slow the applet down a lot, I''m fine. I''m just trying to get rid of a flicker, and I''m thinking this will work. If I''m wrong, or you have a better idea than I do, let me know.
Slayer-X
Slayer-X
I looked up a few things about Image and saw how you were drawing without using the pixel array. Thanks for the help.

btw: Ignore my last post, I don''t need to do that anymore.
Slayer-X
Slayer-X
I''m not sure what I''m doing wrong, but here is what I''m doing in my code:

private Graphics gBuffer;
private Image buffer;

public void init()
{
buffer = karelGraphics.createImage(numCols, numRows);
gBuffer = buffer.getGraphics();
} // end init

public void paint()
{
// drawing using gBuffer

g.drawImage(buffer, 0, 0, this);
}

So, what am I doing wrong?
Wrathnut
Wrathnut
Here is how I draw my graphics in applets I hope it helps


        
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Applet1 extends Applet implements Runnable{

Image offscreenImg;
Graphics offscreenG;

public void init(){

// Create Image & Graphic type variables for buffering animation.

offscreenImg = createImage(xborder, yborder);
offscreenG = offscreenImg.getGraphics();

}

public void run(){
while (true){
repaint();
try{Thread.sleep(delay);}
catch(InterruptedException e){;}
}
}

// Override the update in the Graphics class to reduce flicker even more.

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

public void paint(Graphics g){

//Paint background

offscreenG.setColor(Color.black);
offscreenG.fillRect(0,0,xborder,yborder);

//Draw images

offscreenG.drawImage(gManager.getCurrentImg("player"), 0, 0, this);

offscreenG.drawImage(gManager.getCurrentImg("enemy"), 150, 0, this);


// Actually paint image onscreen.

g.drawImage(offscreenImg, 0, 0, this);
}

}



Anime:Drugs would be cheaper.



[edited by - wrathnut on December 18, 2002 11:19:37 PM]
Slayer-X
Slayer-X
I had the same steps you do, but for some reason it wouldn''t run initialize the offScreenImage and offScreenGraphics in init(). I fixed it, so all is well now =). Thanks for your help.
harleyrana
harleyrana
The best way to do rendering is with BufferedStategy.

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

It takes care of the hassle of managing VolatileImage buffers, and gives you easy access to video memory.

Harley.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.