Källkoden för /anders.enges/java/scribb3.asp
<!--#include file="../inc/navstuff.asp" -->
<H4>Steg 3 - dela upp teckningen i separata streck</H4>
<ul><b>Tema:</b>
<li>Vector klassen</li>
<li>Egendefinierade klasser</li>
</ul>
<p>För att kunna förbättra programmet måste vi inse att när vi trycker ner musknappen och
flyttar musen så kommer vi attt få en samling av punkter. När vi sedan släpper
musknappen och trycker ner den igen på ett nytt ställe kommer det att bli en <b>ny</b> samling
punkter.</p>
<p>Detta gör att en teckning kommer att bestå av en Vektor med "Strokes", där varje stroke i sin tur har en
vektor med punkter och en färg.</p>
<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="#0000FF">import</FONT> java.util.Vector<FONT COLOR="#800000">;</FONT>
<FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">class</FONT> ScribbleApplet <FONT COLOR="#0000FF">extends</FONT> Applet
<FONT COLOR="#800000">{</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="#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="#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="#0000FF">class</FONT> MyDrawing <FONT COLOR="#0000FF">extends</FONT> Panel <FONT COLOR="#0000FF">implements</FONT> MouseMotionListener<FONT COLOR="#800000">,</FONT> MouseListener
<FONT COLOR="#800000">{</FONT>
     <FONT COLOR="#0000FF">private</FONT> Vector allStrokes <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">new</FONT> Vector<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
     <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
     <b><FONT COLOR="#008000">// en egen klass för "strokes". finns senare i texten</FONT>
     private MyStroke thisStroke = new MyStroke();</b>
     <FONT COLOR="#008000">// ----------------------------------------------------</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="#0000FF">private</FONT> <FONT COLOR="#0000FF">boolean</FONT> isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">;</FONT>
     <FONT COLOR="#0000FF">public</FONT> MyDrawing<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT>
     <FONT COLOR="#800000">{</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="#0000FF">this</FONT>.setBackground<FONT COLOR="#800000">(</FONT><FONT COLOR="#0000FF">new</FONT> Color<FONT COLOR="#800000">(</FONT><FONT COLOR="#800080">255</FONT><FONT COLOR="#800000">,</FONT><FONT COLOR="#800080">255</FONT><FONT COLOR="#800000">,</FONT><FONT COLOR="#800080">255</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
     <FONT COLOR="#800000">}</FONT>
     <b><FONT color=#008000>// ----------------------------------------------------</FONT>
     <FONT color=#008000>// rätt mycket ändrad paint </FONT>
     <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="#008000">// kolla hur många strokes det finns</FONT>
         <FONT color=#0000ff>int</FONT> antal <FONT color=#800000>=</FONT> allStrokes.size<FONT color=#800000>(</FONT> <FONT color=#800000>)</FONT><FONT color=#800000>;</FONT>
         <FONT COLOR="#008000">// och loopa igenom dessa</FONT>
         <FONT color=#0000ff>for</FONT> <FONT color=#800000>( </FONT><FONT color=#0000ff>int</FONT> i <FONT color=#800000>=</FONT> <FONT color=#800080>0</FONT> <FONT color=#800000>;</FONT> i <FONT color=#800000><</FONT> antal<FONT color=#800000>;</FONT> i<FONT color=#800000>+</FONT><FONT color=#800000>+</FONT><FONT color=#800000> )</FONT>
         <FONT color=#800000>{</FONT>
             <FONT COLOR="#008000">// sätt undan stroken i en variabel av typen MyStroke</FONT>
             MyStroke stroke <FONT color=#800000>=</FONT> <FONT color=#800000>(</FONT>MyStroke<FONT color=#800000>)</FONT> allStrokes.elementAt<FONT color=#800000>( </FONT>i<FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
             <FONT COLOR="#008000">// kolla hur många punkter det fins i denna stroke</FONT>
             <FONT color=#0000ff>int</FONT> antal2 <FONT color=#800000>=</FONT> stroke.size<FONT color=#800000>( </FONT><FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
             <FONT color=#0000ff>if</FONT> <FONT color=#800000>(</FONT> antal2 <FONT color=#800000>></FONT> <FONT color=#800080>1</FONT> <FONT color=#800000>)</FONT>
             <FONT color=#800000>{</FONT>
                 <FONT COLOR="#008000">// sätt rätt färg</FONT>
                 g.setColor<FONT color=#800000>(</FONT> stroke.getColor<FONT color=#800000>(</FONT><FONT color=#800000>)</FONT> <FONT color=#800000>)</FONT><FONT color=#800000>;</FONT>
                 <FONT COLOR="#008000">// loopa sedan genom stokens vektor efter punkterna</FONT>
                 <FONT COLOR="#008000">// enklast att börja med den andra (1) - vi kan då
                 // alltid anta att det finns en föregående</FONT>
                 <FONT color=#0000ff>for</FONT> <FONT color=#800000>( </FONT><FONT color=#0000ff>int</FONT> j <FONT color=#800000>=</FONT> <FONT color=#800080>1</FONT> <FONT color=#800000>;</FONT> j <FONT color=#800000><</FONT> antal2<FONT color=#800000>;</FONT> j<FONT color=#800000>+</FONT><FONT color=#800000>+</FONT><FONT color=#800000> )</FONT>
                 <FONT color=#800000>{</FONT>
                     <FONT COLOR="#008000">// tag en punkt i vektorn och även föregående punkt</FONT>
                     Point pp1 <FONT color=#800000>=</FONT> stroke.getPointAt<FONT color=#800000>( </FONT>j<FONT color=#800000> - </FONT><FONT color=#800080>1</FONT><FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
                     Point pp2 <FONT color=#800000>=</FONT> stroke.getPointAt<FONT color=#800000>( </FONT>j<FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
                     <FONT COLOR="#008000">// och dra ett streck mellan desa punkter</FONT>
                     g.drawLine<FONT color=#800000>( </FONT>pp1.x<FONT color=#800000>,</FONT> pp1.y<FONT color=#800000>,</FONT> pp2.x<FONT color=#800000>,</FONT> pp2.y<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>// ----------------------------------------------------</FONT></b>
     <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> mouseClicked<FONT COLOR="#800000">(</FONT>MouseEvent e<FONT COLOR="#800000">)</FONT>
     <FONT COLOR="#800000">{</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>
             allStrokes.removeAllElements<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</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="#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="#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="#0000FF">if</FONT> <FONT COLOR="#800000">(</FONT>isDrawing<FONT COLOR="#800000">)</FONT>
         <FONT COLOR="#800000">{</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="#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="#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="#0000FF">this</FONT>.setForeground<FONT COLOR="#800000">(</FONT> Color.red <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             <FONT COLOR="#800000">}</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="#0000FF">this</FONT>.setForeground<FONT COLOR="#800000">(</FONT> Color.black <FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             <FONT COLOR="#800000">}</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">// ----------------------------------------------------</FONT>
         <FONT COLOR="#008000">// sätt in punkterna och förgen för NUVARANDE stroke (streck)</FONT>
         <b>thisStroke.addPoint<FONT COLOR="#800000">( </FONT>e.getPoint<FONT COLOR="#800000">( </FONT><FONT COLOR="#800000"> )</FONT><FONT COLOR="#800000"> )</FONT><FONT COLOR="#800000">;</FONT>
         thisStroke.setColor<FONT COLOR="#800000">( </FONT><FONT COLOR="#0000FF">this</FONT>.getForeground<FONT COLOR="#800000">( </FONT><FONT COLOR="#800000"> )</FONT><FONT COLOR="#800000"> )</FONT><FONT COLOR="#800000">;</FONT></b>
         <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
         lastPoint <FONT COLOR="#800000">=</FONT> e.getPoint<FONT COLOR="#800000">( </FONT><FONT COLOR="#800000"> )</FONT><FONT COLOR="#800000">;</FONT>
         isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">true</FONT><FONT COLOR="#800000">;</FONT>
     <FONT COLOR="#800000">}</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>
         isDrawing <FONT COLOR="#800000">=</FONT> <FONT COLOR="#0000FF">false</FONT><FONT COLOR="#800000">;</FONT>
         <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
         <FONT COLOR="#008000">// sätt in stroken i vektorn och skapa sedan en ny stroke</FONT>
         <b>allStrokes.addElement<FONT color=#800000>( </FONT>thisStroke<FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
         thisStroke <FONT color=#800000>=</FONT> <FONT color=#0000ff>new</FONT> MyStroke<FONT color=#800000>( </FONT><FONT color=#800000> )</FONT><FONT color=#800000>;</FONT></b>
         <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
     <FONT COLOR="#800000">}</FONT>
<FONT COLOR="#800000">}</FONT>
<b>
<FONT COLOR="#008000">// En klass med namnet MyStroke.
// Vi behöver inte skriva en konstruerare om vi inte har speciella
// saker som skall göras när objektet skapas. Java tillhandahåller vid
// behov en "default constructor"
// En klass måste inte heller ärva en annan om vi inte behöver
// "färdig funktionalitet"</font>
<FONT color=#0000ff>class</FONT> MyStroke
<FONT color=#800000>{</FONT>
     <FONT COLOR="#008000">// En vektor för punkterna </font>
     <FONT color=#0000ff>private</FONT> Vector theStroke <FONT color=#800000>=</FONT> <FONT color=#0000ff>new</FONT> Vector<FONT color=#800000>(</FONT><FONT color=#800000>)</FONT><FONT color=#800000>;</FONT>
     <FONT COLOR="#008000">// En Color för färgen</font>
     <FONT color=#0000ff>private</FONT> Color theColor<FONT color=#800000>;</FONT>

     <FONT COLOR="#008000">// en enkel size() funktion som bara returnerar Vektorns size()</font>
     <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>int</FONT> size<FONT color=#800000>(</FONT> <FONT color=#800000>)</FONT>
     <FONT color=#800000>{</FONT>
         <FONT color=#0000ff>return</FONT> theStroke.size<FONT color=#800000>( </FONT><FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
     <FONT color=#800000>}</FONT>
    
     <FONT COLOR="#008000">// returnera Pointen I i vektorn</font>
     <FONT color=#0000ff>public</FONT> Point getPointAt<FONT color=#800000>( </FONT><FONT color=#0000ff>int</FONT> i<FONT color=#800000> )</FONT>
     <FONT color=#800000>{</FONT>
         <FONT color=#0000ff>return</FONT> <FONT color=#800000>( </FONT>Point<FONT color=#800000> )</FONT> theStroke.elementAt<FONT color=#800000>( </FONT>i<FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
     <FONT color=#800000>}</FONT>
    
     <FONT COLOR="#008000">// lägg till en Point till vektorn </font>
     <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>void</FONT> addPoint<FONT color=#800000>( </FONT>Point p<FONT color=#800000> )</FONT>
     <FONT color=#800000>{</FONT>
         theStroke.addElement<FONT color=#800000>( </FONT>p<FONT color=#800000> )</FONT><FONT color=#800000>;</FONT>
     <FONT color=#800000>}</FONT>

     <FONT COLOR="#008000">// returnera färgen </font>
     <FONT color=#0000ff>public</FONT> Color getColor<FONT color=#800000>(</FONT> <FONT color=#800000>)</FONT>
     <FONT color=#800000>{</FONT>
         <FONT color=#0000ff>return</FONT> theColor<FONT color=#800000>;</FONT>
     <FONT color=#800000>}</FONT>
    
     <FONT COLOR="#008000">// sätt färgen </font>
     <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>void</FONT> setColor<FONT color=#800000>( </FONT>Color c<FONT color=#800000> )</FONT>
     <FONT color=#800000>{</FONT>
         theColor <FONT color=#800000>=</FONT> c<FONT color=#800000>;</FONT>
     <FONT color=#800000>}</FONT>
<FONT color=#800000>}</FONT></b>
</pre>
<p><a href="./scribble/Page3.htm" target="_new">Prova programmet i nuvarande skick</a></p>
<!--#include file="../inc/footer.asp" -->