 Källkoden för /anders.enges/java/scribb1.asp
 Källkoden för /anders.enges/java/scribb1.asp 
<!--#include file="../inc/navstuff.asp" -->
<H4>Scribble som Java Applet</H4>
<ul><b>Tema:</b>
<li>Mushantering</li>
<li>Graphics klassen</li>
<li>Point klassen</li>
</ul>
<PRE>
<FONT COLOR="#0000FF">import</FONT> java.awt.<FONT COLOR="#800000">*</FONT><FONT COLOR="#800000">;</FONT>
<FONT COLOR="#0000FF">import</FONT> java.applet.<FONT COLOR="#800000">*</FONT><FONT COLOR="#800000">;</FONT>
<FONT COLOR="#0000FF">import</FONT> java.awt.event.<FONT COLOR="#800000">*</FONT><FONT COLOR="#800000">;</FONT>
<FONT COLOR="#008000">/*
En Applet för Webanvändning
Sätt in den i en websida med följande html kod
        <applet
                code=ScribbleApplet.class
                name=ScribbleApplet
                width=320
                height=200>
        </applet>
*/</FONT>
<FONT COLOR="#008000">/* 
----------------------------------------------------------
class ScribbleApplet
----------------------------------------------------------
OBS!
eftersom klassen heter ScribbleApplet måste filen heta 
ScribbleApplet.java
*/</FONT>
<FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">class</FONT> ScribbleApplet <FONT COLOR="#0000FF">extends</FONT> Applet
<FONT COLOR="#800000">{</FONT>
        <FONT COLOR="#008000">// initieraren körs alltid på en Applet</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> init<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>
        <FONT COLOR="#800000">{</FONT>   
                <FONT COLOR="#008000">// sätt en BorderLayout på appleten</FONT>
                <FONT COLOR="#0000FF">this</FONT>.setLayout<FONT COLOR="#800000">(</FONT><FONT COLOR="#0000FF">new</FONT> BorderLayout<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#008000">// och sätt in ritpanelen i mitten</FONT>
                <FONT COLOR="#0000FF">this</FONT>.add<FONT COLOR="#800000">(</FONT> <FONT COLOR="#FF0000">"Center"</FONT><FONT COLOR="#800000">,</FONT> <FONT COLOR="#0000FF">new</FONT> MyDrawing<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
        <FONT COLOR="#800000">}</FONT>
<FONT COLOR="#800000">}</FONT>
<FONT COLOR="#008000">/* 
----------------------------------------------------------
class MyDrawing 
----------------------------------------------------------
En Panel variant med ritegenskaper...
*/</FONT>
<FONT COLOR="#0000FF">class</FONT> MyDrawing <FONT COLOR="#0000FF">extends</FONT> Panel <FONT COLOR="#0000FF">implements</FONT> 
                                                        MouseMotionListener<FONT COLOR="#800000">,</FONT> <FONT COLOR="#008000">// för mouseDragged </FONT>
                                                        MouseListener        <FONT COLOR="#008000">// för mouseReleased </FONT>
                                                                                                  <FONT COLOR="#008000">// och mouseClicked</FONT>
<FONT COLOR="#800000">{</FONT>
        <FONT COLOR="#008000">// senaste muskoordinater</FONT>
        <FONT COLOR="#008000">// en Point klass är lättaste sättet att hålla reda på både x och y</FONT>
        <FONT COLOR="#0000FF">private</FONT> Point lastPoint <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">new</FONT> Point<FONT COLOR="#800000">(</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
        
        <FONT COLOR="#008000">// ritar vi ??</FONT>
        <FONT COLOR="#0000FF">private</FONT> <FONT COLOR="#0000FF">boolean</FONT> isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">;</FONT>
        <FONT COLOR="#008000">// konstruereren körs alltid då ett objekt av denna klass skapas</FONT>
        <FONT COLOR="#0000FF">public</FONT> MyDrawing<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>
        <FONT COLOR="#800000">{</FONT>
                <FONT COLOR="#008000">// se till att vi reagerar på mushändelser</FONT>
                <FONT COLOR="#0000FF">this</FONT>.addMouseListener<FONT COLOR="#800000">(</FONT> <FONT COLOR="#0000FF">this</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#0000FF">this</FONT>.addMouseMotionListener<FONT COLOR="#800000">(</FONT> <FONT COLOR="#0000FF">this</FONT> <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#008000">// sätt en vit bakgrund</FONT>
                <FONT COLOR="#0000FF">this</FONT>.setBackground<FONT COLOR="#800000">(</FONT> Color.white <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
        <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#008000">// alla dessa måste finnas om vi inte vill köra anonyma </FONT>
        <FONT COLOR="#008000">// funktioner, de behöver dock inte göra någonting</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseMoved<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">{</FONT>  <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mousePressed<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">{</FONT> <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseEntered<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">{</FONT> <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseExited<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">{</FONT> <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#008000">// vi klickade på panelen</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseClicked<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> 
        <FONT COLOR="#800000">{</FONT> 
                <FONT COLOR="#008000">// var det ett dubbelklick?</FONT>
                <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT>e.getClickCount<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">></FONT> <FONT COLOR="#800080">1</FONT><FONT COLOR="#800000">)</FONT>
                <FONT COLOR="#800000">{</FONT>
                        <FONT COLOR="#008000">// rita i så fall om panelen</FONT>
                        <FONT COLOR="#0000FF">this</FONT>.repaint<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#008000">// i VB måste vi hantera musklick och musförflyttning separat</FONT>
        <FONT COLOR="#008000">// i Java kombineras dessa i mouseDragged händelsen som sker när</FONT>
        <FONT COLOR="#008000">// man flyttar musen med en knapp nedtryckt</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseDragged<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> 
        <FONT COLOR="#800000">{</FONT>
                <FONT COLOR="#008000">// håller vi på och ritar?</FONT>
                <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT>isDrawing<FONT COLOR="#800000">)</FONT>
                <FONT COLOR="#800000">{</FONT>
                        <FONT COLOR="#008000">// kolla alla eventuella modifierare, </FONT>
               			<FONT COLOR="#008000">// t.ex. vilken musknapp vi använt</FONT>
                        <FONT COLOR="#0000FF">int</FONT> modifier <FONT COLOR="#800000">=</FONT> e.getModifiers<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                        <FONT COLOR="#008000">// kontrollerar om det var knapp 1 som blivit tryckt</FONT>
                        <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT> <FONT COLOR="#800000">(</FONT>modifier <FONT COLOR="#800000">&</FONT> MouseEvent.BUTTON1_MASK <FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">=</FONT><FONT COLOR="#800000">=</FONT> modifier <FONT COLOR="#800000">)</FONT>
                        <FONT COLOR="#800000">{</FONT>
                                <FONT COLOR="#008000">// sätt en blå förgrundsfärg</FONT>
                                <FONT COLOR="#0000FF">this</FONT>.setForeground<FONT COLOR="#800000">(</FONT> Color.blue <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                        <FONT COLOR="#800000">}</FONT>
                        <FONT COLOR="#0000FF">else</FONT> 
                        <FONT COLOR="#800000">{</FONT>
                                <FONT COLOR="#008000">// sätt en röd förgrundsfärg</FONT>
                                <FONT COLOR="#0000FF">this</FONT>.setForeground<FONT COLOR="#800000">(</FONT> Color.red <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>        
                        <FONT COLOR="#800000">}</FONT>
                        <FONT COLOR="#008000">// kontrollera shift tangenten</FONT>
                        <FONT COLOR="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT> e.isShiftDown<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT> <FONT COLOR="#800000">)</FONT>
                        <FONT COLOR="#800000">{</FONT>
                                <FONT COLOR="#008000">// sätt en svart förgrundsfärg</FONT>
                                <FONT COLOR="#0000FF">this</FONT>.setForeground<FONT COLOR="#800000">(</FONT> Color.black <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                        <FONT COLOR="#800000">}</FONT>
                        <FONT COLOR="#008000">// drag ett streck mellan föregående </FONT>
               			<FONT COLOR="#008000">// koordinater och nuvarande</FONT>
                        <FONT COLOR="#0000FF">this</FONT>.getGraphics<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.drawLine<FONT COLOR="#800000">(</FONT>lastPoint.x <FONT COLOR="#800000">,</FONT> 
                                                                                lastPoint.y <FONT COLOR="#800000">,</FONT> 
                                                                                e.getPoint<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.x <FONT COLOR="#800000">,</FONT> 
                                                                                e.getPoint<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>.y
                                                                                <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#800000">}</FONT>
                <FONT COLOR="#008000">// sätt undan muskoordinaterna i en point</FONT>
                lastPoint <FONT COLOR="#800000">=</FONT> e.getPoint<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
                <FONT COLOR="#008000">// vi har nu börjat rita...</FONT>
                <FONT COLOR="#008000">// nästa gång denna händelse sker skan vi dra streck</FONT>
                isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">true</FONT><FONT COLOR="#800000">;</FONT>
        <FONT COLOR="#800000">}</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#008000">// Vi släppte den netryckta musknappen</FONT>
        <FONT COLOR="#008000">//----------------------------------------------------------</FONT>
        <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> mouseReleased<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT> 
        <FONT COLOR="#800000">{</FONT>
                <FONT COLOR="#008000">// om vi släppt musen så skall vi sluta rita</FONT>
                isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">;</FONT>
        <FONT COLOR="#800000">}</FONT>
<FONT COLOR="#800000">}</FONT>
</PRE>
<h4>Resultatet</h4>
<p>
<a href="scribble/Page1.htm" target="_new">Klicka här för att provköra Appleten</a>
</p>  
<!--#include file="../inc/footer.asp" -->