Image - Beispiel

Sourcecode - Ausschnitt : mit THREAD

Dr. M. Halfpap


public class Bilder extends Applet implements Runnable 
{	Image bild1, bild2, bild = null;
	int bildbreite = 350;
	int bildhoehe = 50;
	int xpos = 80;
	int xAkt = 80;
	int yAkt = 50;
	int ypos = 50;
	Thread thread;
	MediaTracker bildWaerter = new MediaTracker(this);
}

public void init() 
{	super.init(); 
	bild1 = getImage(getCodeBase(),"text1.jpg");
	bild2 = getImage(getCodeBase(),"text2.jpg");
	bildWaerter.addImage(bild1,0);
	bildWaerter.addImage(bild2,0);
	try {bildWaerter.waitForID(0); } catch (InterruptedException e) {};
	bild = bild1;
}

public void paint(Graphics g) 
{	// super.paint(g);
	g.clearRect(80,50,350,50);
	g.drawImage(bild, xAkt, yAkt, bildbreite, bildhoehe, getBackground(), this);
}

public void pause(int time) 
{	try { Thread.sleep(time); }
	catch (InterruptedException e) {}
}

public void run() 
{	zeichne();  }

public void start() 
{	if (thread == null)
	{	thread = new Thread(this);
		thread.start();  
	}
}

public void stop()
{	if (thread != null)
	{	thread.stop();
		thread = null;
	}
}

public void update(Graphics g) 
{
	// Durch Überschreiben beschleunigte und sicherere
	// Ausgabe
	paint(g);
}

public void zeichne() 
{	do
	{  for (int i = 1; i<18; i++)
	   {	 xAkt = (int)(xpos+i*10);
		 bildbreite = (int)(bildbreite-20);
		 repaint(); pause(50);
	   };
	   xpos = xAkt;
	   if (bild == bild1) { bild = bild2;} else { bild = bild1;} ;
	   for (int i = 1; i<18; i++)
	   {	 xAkt = (int)(xpos-i*10);
		 bildbreite = (int)(bildbreite+20);
		 repaint(); pause(50);
	   };
	   xpos = xAkt;
	} while (thread != null);  
}