Sign in to follow this  

[java] trouble doing double buffering

This topic is 3723 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 tried to double buffer, and now I get no image on the screen. <code> import java.awt.*; import java.applet.*; import java.awt.event.*; import static java.lang.Math.*; class PlanetClass extends Applet{ public int X=0; public int Y=0; public double Theta=0; public double Velocity=0; public double Radius=0; public Image Picture; public boolean active = true; public double GetTheta(double Time){ return((Velocity*Time)/(Radius*1000000));//radius is 10^6 so we need to multiply by 1000000 } public void Setup(double Rad,double Vel,int XPos){ Radius = Rad; Velocity = Vel; X = XPos; return ; } } public class JavaSolar2 extends Applet implements MouseListener,Runnable{ Image CharImage; Image VillImage; int Start_X = 600; int Start_Y = 600; int Mouse_X = 0; int Mouse_Y = 0; int Timer=0; String Version = "Version 1.00"; Thread AIThread; boolean airunning = true; PlanetClass Planet[] = new PlanetClass[11]; Image buffer; // The off-screen image for double-buffering Graphics bufferGraphics; // A Graphics object for the buffer public void init() { buffer = this.createImage(1200, 1200); bufferGraphics = buffer.getGraphics(); for (int i=0; i<=10;i++){ Planet[i] = new PlanetClass(); Planet[i].Y = 0; } Planet[1].Picture = getImage(getCodeBase(), "images/sun.gif"); Planet[1].Setup(0,0,0); Planet[2].Picture = getImage(getCodeBase(), "images/mercury.gif"); Planet[2].Setup(57.9,47.9,(int)(57.9/13)); Planet[3].Picture = getImage(getCodeBase(), "images/Venus.gif"); Planet[3].Setup(108.2,35,(int)(108.2/13)); Planet[4].Picture = getImage(getCodeBase(), "images/earth.gif"); Planet[4].Setup(149.6,29.8,(int)(149.6/13)); Planet[5].Picture = getImage(getCodeBase(), "images/mars.gif"); Planet[5].Setup(227.9,24.1,(int)(227.9/13)); Planet[6].Picture = getImage(getCodeBase(), "images/jupiter.gif"); Planet[6].Setup(778.6,13.1,(int)(778.6/13)); Planet[7].Picture = getImage(getCodeBase(), "images/saturn.gif"); Planet[7].Setup(1433.5,9.7,(int)(1433.5/13)); Planet[8].Picture = getImage(getCodeBase(), "images/uranus.gif"); Planet[8].Setup(2872.5,6.8,(int)(2872.5/13)); Planet[9].Picture = getImage(getCodeBase(), "images/neptune.gif"); Planet[9].Setup(4495.1,5.4,(int)(4495.1/13)); Planet[10].Picture = getImage(getCodeBase(), "images/pluto.gif"); Planet[10].Setup(5870.0,4.7,(int)(5870.0/13)); addMouseListener(this); AIThread= new Thread(this); // Create Thread AIThread.start(); //Run Thread } public void update(Graphics g) { paint(g); } public void paint(Graphics g){ bufferGraphics.setColor(this.getBackground()); bufferGraphics.fillRect(0, 0, 1200, 1200); // clear the buffer bufferGraphics.setColor(Color.black); for (int i=0; i<11;i++){ if (Planet[i].active) bufferGraphics.drawImage(Planet[i].Picture, Planet[i].X+Start_X,Planet[i].Y+Start_Y, this); } bufferGraphics.drawString(Version+" Timer "+Timer+" days",10,10); buffer.drawString("Mouse X "+Mouse_X+" Mouse_Y "+Mouse_Y,10,20); for (int i=1; i<=11;i++) bufferGraphics.drawString("X ="+Planet[i].X+"y= "+Planet[i].Y,10,10*i+30); // Then copy the off-screen buffer onto the screen g.drawImage(buffer, 0, 0, this); } public void destroy() { removeMouseListener(this); } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); boolean notchanged = true; for (int i=0; i<=10;i++){ Planet[i].active = false; } for (int i=1; i<=10;i++){ if ((x > 9)&&(x < 50)&&(y > 10*i+29)&&(y < 10*i+40)){ Planet[i+1].active = true; notchanged = false; } } for (int i=1; i<=10;i++){ if ((Planet[i].X+Start_X <= x)&&(Planet[i].X+21+Start_X >= x)&&(Planet[i].Y+Start_Y <= y)&&(Planet[i].Y+21+Start_Y >= y)){ Planet[i].active = true ^ Planet[i].active; notchanged = false; } } if (notchanged) for (int i=1; i<=10;i++){ Planet[i].active = true; } Mouse_X = x; Mouse_Y = y; repaint(); } public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void run() { // loop until told to stop while (airunning) { for (int i=2; i<=10;i++){ Planet[i].Theta = Planet[i].GetTheta(60*60*48)+Planet[i].Theta; //60*60*24 is a day in seconds if (Planet[i].Theta > 6.28) Planet[i].Theta = Planet[i].Theta - (2*PI); Planet[i].X = Planet[1].X + (int)(Planet[i].Radius*sin(Planet[i].Theta)/13); Planet[i].Y = Planet[1].Y + (int)(Planet[i].Radius*Math.cos(Planet[i].Theta)/13); } Timer = Timer + 1; repaint(); try { // Wait 5 milliseconds before continuing AIThread.sleep(10); } catch (InterruptedException e) { System.out.println(e); } // he has wait and will now restart his actions. } } } </code>

Share this post


Link to post
Share on other sites
Don't keep a reference of back buffers Graphics object outside of paint().

Try something like this:



public class JavaSolar2 extends Applet implements MouseListener,Runnable{

// Other instance variables ...

Image buffer; // The off-screen image for double-buffering

public void init() {
buffer = this.createImage(1200, 1200);

// Other initialisation ...
}

public void paint(final Graphics g) {

if (buffer == null) {
// Problem !!
return;
}

final Graphics bufferGraphics = buffer.getGraphics();

bufferGraphics.setColor(this.getBackground());
bufferGraphics.fillRect(0, 0, 1200, 1200); // clear the buffer
bufferGraphics.setColor(Color.black);

for (int i = 0; i < 11; i++){
if (Planet[i].active) {
bufferGraphics.drawImage(Planet[i].Picture, Planet[i].X+Start_X,Planet[i].Y+Start_Y, this);
}
}

bufferGraphics.drawString(Version+" Timer "+Timer+" days",10,10);
buffer.drawString("Mouse X "+Mouse_X+" Mouse_Y "+Mouse_Y,10,20);

// ArrayIndexOutOfBounsException ??
for (int i=1; i<=11;i++) {
bufferGraphics.drawString("X ="+Planet[i].X+"y= "+Planet[i].Y,10,10*i+30);
}

// Then copy the off-screen buffer onto the screen
g.drawImage(buffer, 0, 0, this);


// Release resources.
bufferGraphics.dispose();
}

// Other methods ...
}




Share this post


Link to post
Share on other sites

This topic is 3723 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this