Knowledge Base Nr: 00136 appletBildNachladen.java - http://www.swe-kaiser.de

Downloads:

java: http kommunikation eines java-applets mit dem server über port 80
(durch firewall) - z.b. nachladen von bildern

  
class k_LiveCanvas extends Canvas
{
k_LiveCam2 liveCam = null;

public k_LiveCanvas(k_LiveCam2 lc)
{
liveCam = lc;
}

public void mypaint()
{
Graphics g = getGraphics();
paint(g);
}

public void paint(Graphics g)
{
if (liveCam.liveImg != null)
{
if (liveCam.tracker.checkID(0))
{
g.drawImage(liveCam.liveImg, 0, 0, this);
}
}
}
}

public class k_LiveCam2 extends java.applet.Applet implements Runnable
{
...
Image liveImg = null;
MediaTracker tracker = null;
k_LiveCanvas picCanvas = null;
...

public void init ()
{
...
//canvas
picCanvas = new k_LiveCanvas(this);
picCanvas.setBackground(Color.white);
picCanvas.setSize(768,576);
add(picCanvas);

tracker = new MediaTracker(this);
}

boolean doSingleDisplay()
{
try
{
//avoid caching
if (liveImg != null)
liveImg.flush();

try
{
URL req = new URL("http://" + p_host +"/cgi-bin/getLivePic.pl?picsize=1");
//das aufgerufene script muss direkt ein bild zurückgeben
//z.b über den location header (location: /images/picture.jpg)

liveImg = getImage(req);
tracker.addImage(liveImg, 0);
tracker.waitForID(0);
}
catch(InterruptedException e)
{
}

picCanvas.mypaint(); //verhindert flackern (unterdrückt das löschen des hintergrunds)
}
catch ( IOException e )
{
}

return true;
}

public void run () //thread for cyclic refreshing
{
while ( runflag )
{
if (isCyclic)
{
boolean bValid = doSingleDisplay();
}

mysleep(50); //give other threads a chance
}
}

}