double buffer

B

Bob

I am tring to make a simple animated ball the code at the under, and to stop
flickering I tried to use some code I found for double buffering at bottom
but I cannot get it to work can anyone help me?




import javax.swing.JApplet;

import java.awt.*;

public class Ball2 extends JApplet {

private int x = 0;

private int y = 0;

private int dx = 1;

private int dy = 2;

private int diameter = 50;

private int width = 0;

private int height = 0;

public void init() {

width = getSize().width;

height = getSize().height;

}

public void paint(Graphics g) {

while (true) {

try {Thread.sleep(75);}

catch(Exception e){}

x += dx;

if (x < dx) dx = dx+10;

if (x > width-diameter) dx = -dx-10;

y += dy;

if (y < dy) dy = dy+10;

if (y > height-diameter) dy = -dy-10;

g.setColor(this.getBackground());

g.fillRect(0,0,width,height);

g.setColor(Color.red);

g.fillOval(x,y,diameter,diameter);

}

}

}




Double Buffering


Image offImage;
Graphics offScreen;
public void update(Graphics g)
{
Dimension dim =getSize();
if(offScreen==null)
{
offImage=createImage(dim.width,dim.height);
offScreen=offImage.getGraphics();
}
offScreen.setColor(getBackground());
offScreen.fillRect(0,0,dim.width,dim.height);
offScreen.setColor(getForeground());
offScreen.drawImage(offImage,0,0,this);
g.drawImage(offImage,0,0,null);
}
 
M

MacTotoche

Bob said:
I am tring to make a simple animated ball the code at the under, and to stop
flickering I tried to use some code I found for double buffering at bottom
but I cannot get it to work can anyone help me?
Here is something you can copypaste for what it's worth.

import java.applet.*;
import javax.swing.JApplet;

import java.awt.*;

// javac Ball2.java && java Ball2

public class Ball2 extends Applet { // or JApplet

private int x = 0;
private int y = 0;
private int dx = -1;
private int dy = -1;
private int diameter = 50;
private int width = 0;
private int height = 0;
private Thread disp;

private boolean doubleBuffer = false; // select mode here

public static void main(String[] args) {
Frame f = new Frame("Ball2");
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});

Ball2 applet = new Ball2();

f.add("Center", applet);
f.setSize(500, 400);
f.show();

applet.init();
applet.start();
}

public void start() {
super.start();

width = getWidth();
height = getHeight();
x = width/2;
y = height/2;

disp = new Thread() {
public void run() {
while (true) {
try {Thread.sleep(10);}
catch(Exception ex){ /*ex.printStackTrace();*/}

if (x == 0) dx = 1;
if (x + diameter == width) dx = -1;
x += dx;
if (y == 0) dy = 1;
if (y + diameter == height) dy = -1;
y += dy;

Ball2.this.repaint();
}
}
};
disp.start();
}

private void paint1(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,width,height);
try {Thread.sleep(10);}
catch(Exception ex){ /*ex.printStackTrace();*/}
g.setColor(Color.red);
g.fillOval(x,y,diameter,diameter);
}


private Image offImage = null;
private Graphics offScreen = null;

private void paint2(Graphics g) {
Dimension dim = getSize();
if (offImage == null) {
offImage = createImage(dim.width,dim.height);
offScreen = offImage.getGraphics();
}
offScreen.setColor(Color.white);
offScreen.fillRect(0,0,dim.width,dim.height);
try {Thread.sleep(10);}
catch(Exception ex){ /*ex.printStackTrace();*/}
offScreen.setColor(Color.blue);
offScreen.fillOval(x,y,diameter,diameter);
g.drawImage(offImage,0,0,null);
}

public void paint(Graphics g) {
if (!doubleBuffer)
paint1(g);
else
paint2(g);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top