Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange Behaviour Painting to the canvas

Status
Not open for further replies.

Helixcoil

Programmer
Sep 8, 2004
7
CA

I've been surfing the web for weeks now and can't find anyone who has experienced this strange behaviour when loading and displaying pictures on a canvas. I load an array of images from the disk drive in the Applet init method and then proceed to display them using my own graphic context and .drawImage in my UpdateCanvas method. The UpdateCanvas method calls repaint() and I override the Update method. The weird thing is that some images are displayed while others are not. I've tried using Mediatracker code thinking that the images had simply not fully loaded, and that's why they were not displayed but I had no luck with this. I introduced a delay of a second, again thinking that the compiler could not load everything in time, and no luck. I've posted the revalent code down here, the UpdateCanvas method is seperate from everything else cause I needed a method that I can allows me to update new display pictures when the user presses certain keys. Any help would be really appreciated, thanks...

Graphics gContext;
private Image TestArray = new private Image[80];
private Image Background = getImage(getDocumentBase(), "Background.gif");

public void init() {
for (i=0;i<80;i++) {
TestArray = getImage(getDocumentBase(), "Test" + i + ".gif");
}
Buffer = createImage(800, 800);
gContext = Buffer.getGraphics();
gContext.setColor(Color.white);
}

public void UpdateCanvas() {
gContext.drawImage(Background, 0, 0, this);
for(int i=0;i<80;i++) {
gContext.drawImage(TestArray, x, 100, this);
x=x+10;
}
repaint();
}

public void paint(Graphics g) {
g.drawImage(Buffer, 0, 0, this);
}

public void update(Graphics g) {
paint(g);
}
 
//please post your complete code
Code:
//try using mediatracker like this
import java.awt.*;
import java.awt.image.*;
import java.applet.*;

public class rotate extends Applet {
    private Image image, buffer, background, testArray[];
    Graphics gContext;
	public void init() {
                testArray = new Image[10];
		MediaTracker tracker = new MediaTracker(this);
        image = getImage(getCodeBase(), "c1.jpg");

  for (int i=0;i<=2;i++) {
   testArray[i] = getImage(getDocumentBase(), "c" + i + ".jpg");
		tracker.addImage(testArray[i], i);
  }
  background = getImage(getDocumentBase(), "background.gif");
  tracker.addImage(background, 3);
		try { tracker.waitForAll(); }
		catch (InterruptedException e) { }
  buffer = createImage(800, 800);
  gContext = buffer.getGraphics();
  gContext.setColor(Color.white);
	}
	
public void UpdateCanvas() {
 gContext.drawImage(background, 0, 0, this);
 int x = 0;
 for(int i=0;i<=2;i++) {
  gContext.drawImage(testArray[i], x, 100, this);
  x=x+10;
 }
 repaint();
}
	
public void paint(Graphics g) { 
 g.drawImage(buffer, 0, 0, this);
}
	
	public void update (Graphics g) {
		paint(g);
	}	

}
// how many byte do your images have?
 
Thanks Prosper, but like had said I had tried MediaTracker code before... Thanks though

I figured the problem out this morning, don't ask me to explain it. I had this problem consistently for weeks now and everytime I tested the code out it was from a webserver locally of my machine. Take a wild guess at what webserver I was using... :) that's right Microsoft's very own IIS, I switched my webserver today for a totally unrelated problem and suddenly my code was working fine. All the images were being drawn on the canvas and there were no problems... so just so you guys know IIS and Java don't mix very well...
 
Considering the when using an applet, the only thing the server really does is stream the byte code to the client so that the client machine can run the applet, I find that it was IIS odd ... not saying its impossible, but as stefan said, without knowing the real problem, its hard to blame something outright.

--------------------------------------------------
Free Database Connection Pooling Software
 
Your right, maybe I did jump the gun abit. I'm just telling you guys what I observed for 3 weeks now. I was convinced it was some programming error on my part, I mean who In their right mind would suspect the webserver for not being able to draw all my pics on the canvas. I tried everything and nothing work, I finally switch the sever for something alittle smaller and voila everthing worked... I'm not sure why, but it worked....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top