Källkoden för /anders.enges/java/blaster3.asp
<!--#include file="../inc/navstuff.asp" -->
<H4>Övningar</H4>
<p></p>
<ol>
<li>Ändra bredd till 400 och höjd till 180 på appletten. Dett sker i HTML koden<br /><br /></li>
<li>Rita eller stjäl fyra bilder som du skall kalla bild0.gif ... bild3.gif eller bild0.jpg ... bild3.jpg<br />
Sätt dessa i images katalogen. Ändra sedan på Applet taggen i html sidan så att du visar dessa bilder istället för de tidigare<br /><br /></li>
<li>Fördubbla bläddringshastigheten på animationen.<br /><br /></li>
<li>Prova ändra i java koden så att <b>Image.SCALE_SMOOTH</b> ändras till <b>Image.SCALE_FAST</b>. Märker du någon skillnad?<br /><br /></li>
<li>Sätt in följande rad direkt i början av <b>paint</b> funktionen:<br />
<b>g.drawString( "Loading pictures. Please Wait...", 10, 10);</b><br/>
Varför kan detta vara en god idé?<br /><br /></li>
<li>Se till att vi har en vit bakgrund på appletten innan vi hunnit ladda bilderna<br /><br /></li>
<hr>
<li>Lite mer avancerat... (Överkurs - men kanske intressant)<br /><br />
Ändra på raden:<br />
<PRE>
             repaint<FONT COLOR="#800000">(</font> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
</pre>
Till:
<pre>
             repaint<FONT COLOR="#800000">(</FONT><FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">,</FONT><FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.width<FONT COLOR="#800000">-</FONT><FONT COLOR="#800080">20</FONT><FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.height<FONT COLOR="#800000">-</FONT><FONT COLOR="#800080">20</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
</pre>
Samt ändra om paint funktionen till följande:
<pre>
     <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> paint<FONT COLOR="#800000">(</FONT>Graphics g<FONT COLOR="#800000">)</FONT>
     <FONT COLOR="#800000">{</FONT>
         <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT> <FONT COLOR="#800000">(</FONT> tracker.statusAll<FONT COLOR="#800000">(</FONT><FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">&</FONT> MediaTracker.ERRORED <FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">!</FONT><FONT COLOR="#800000">=</FONT> <FONT COLOR="#800080">0</FONT> <FONT COLOR="#800000">)</FONT>
         <FONT COLOR="#800000">{</FONT>
             g.setColor<FONT COLOR="#800000">(</FONT> Color.red <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             g.fillRect<FONT COLOR="#800000">(</FONT> <FONT COLOR="#800080">0</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#800080">0</FONT><FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.width<FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.height <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             <FONT COLOR="#0000FF">return</FONT><FONT COLOR="#800000">;</FONT>
         <FONT COLOR="#800000">}</FONT>
         <b><FONT COLOR="#008000">// -----------------------------------------------------</FONT>
         <FONT COLOR="#008000">// skall vi rita om ALLT?</FONT>
         <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT>g.getClipBounds<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.height <FONT COLOR="#800000">=</FONT><FONT COLOR="#800000">=</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.height<FONT COLOR="#800000">)</FONT>
             g.drawImage<FONT COLOR="#800000">(</FONT> bg<FONT COLOR="#800000">,</FONT> <FONT COLOR="#800080">0</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#800080">0</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#0000FF">this</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
         <FONT COLOR="#008000">// ----------------------------------------------------- </FONT></b>
         <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT> tracker.statusID<FONT COLOR="#800000">(</FONT><FONT COLOR="#800080">1</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">=</FONT><FONT COLOR="#800000">=</FONT> MediaTracker.COMPLETE <FONT COLOR="#800000">)</FONT>
         <FONT COLOR="#800000">{</FONT>
             <b><FONT COLOR="#008000">// -----------------------------------------------------</FONT>
             <FONT COLOR="#008000">// Behöver inte rita om HELA appleten - bara den rektangel där</FONT>
             <FONT COLOR="#008000">// animationsbilden finns</FONT>
             g.setClip<FONT COLOR="#800000">(</FONT> <FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.width<FONT COLOR="#800000">-</FONT><FONT COLOR="#800080">20</FONT><FONT COLOR="#800000">,</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.height<FONT COLOR="#800000">-</FONT><FONT COLOR="#800080">20</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             <FONT COLOR="#008000">// får inte byta bild (index) innan denna loop är klar!</FONT>
             <FONT COLOR="#0000FF">synchronized</FONT> <FONT COLOR="#800000">(</FONT> <FONT COLOR="#0000FF">this</FONT> <FONT COLOR="#800000">)</FONT>
             <FONT COLOR="#800000">{</FONT>
                <FONT COLOR="#0000FF">for</FONT> <FONT COLOR="#800000">(</FONT> <FONT COLOR="#0000FF">int</FONT> j <FONT COLOR="#800000">=</FONT> <FONT COLOR="#800000">-</FONT> getSize<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.width<FONT COLOR="#800000">;</FONT> j <FONT COLOR="#800000"><</FONT><FONT COLOR="#800000">=</FONT> <FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">;</FONT> j<FONT COLOR="#800000">+</FONT><FONT COLOR="#800000">+</FONT> <FONT COLOR="#800000">)</FONT>
                <FONT COLOR="#800000">{</FONT>
                   <FONT COLOR="#008000">// en liten paus på 10 millisekunder</FONT>
                   <FONT COLOR="#0000FF">try</FONT>
                   <FONT COLOR="#800000">{</FONT>
                       Thread.sleep<FONT COLOR="#800000">(</FONT> <FONT COLOR="#800080">10</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                   <FONT COLOR="#800000">}</FONT>
                   <FONT COLOR="#0000FF">catch</FONT> <FONT COLOR="#800000">(</FONT>InterruptedException e<FONT COLOR="#800000">)</FONT>
                   <FONT COLOR="#800000">{</FONT>
                       <FONT COLOR="#0000FF">break</FONT><FONT COLOR="#800000">;</FONT>
                   <FONT COLOR="#800000">}</FONT>
                   <FONT COLOR="#008000">// rita in bilden - observera j variabelns användning</FONT>
                   g.drawImage<FONT COLOR="#800000">(</FONT>anim<FONT COLOR="#800000">[</FONT>index<FONT COLOR="#800000">]</FONT><FONT COLOR="#800000">,</FONT> j<FONT COLOR="#800000">,</FONT> <FONT COLOR="#800080">10</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#0000FF">this</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
               <FONT COLOR="#800000">}</FONT>
             <FONT COLOR="#800000">}</FONT>
             <FONT COLOR="#008000">// -----------------------------------------------------</FONT></b>
         <FONT COLOR="#800000">}</FONT>
     <FONT COLOR="#800000">}</FONT>
<FONT COLOR="#800000">}</FONT>
</PRE>
Vad gör programmet nu? Försök kontrollera från hjälpen vad de nya "orden" i programmet kan ha för betydelse. Prova även att ändra på loopen för att få bilderna att byta på lite olika sätt <a href="./project6/Page2.htm" target="_new">Klicka här för att få se en variant på detta tema</a></li>
</ol>
<!--#include file="../inc/footer.asp" -->