Källkoden för /anders.enges/java/scribb2.asp
<!--#include file="../inc/navstuff.asp" -->
<H4>Vi vidareutvecklar Scribble</H4>
<ul><b>Tema:</b>
<li>Vector klassen</li>
<li><b>paint</b> funktionen</li>
</ul>
<p>Både vår Applet och VB programmet lider bägge av samma problem:
de "slarvar bort" teckningen om man tar fram ett annat program som täcker vårt program.
Detta beror på att vi bara ritar i fönstret men sparar inte koordinaterna. När fönstret
ritas om kommer programmet att rita ett tomt fönster. Detta är något pinsamt och
borde kanske avhjälpas...</p>
<p>Detta kommer vi att lösa i flera steg</p>
<h4>Steg 1 - sätt dit en paint funktion i MyDrawing klassen</h4>
<p>Observera att kod märkt med <b>fetstil</b> är ny. övrig kod skall inte modifieras, eller tas bort. Jag har dock plockat bort en hel del kod för att få kortare sidor.</p>
<PRE>
<FONT COLOR="#008000">/*
----------------------------------------------------------
class MyDrawing
----------------------------------------------------------
*/</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> 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="#008000"><b>// paint funktion </FONT>
     <FONT COLOR="#008000"><b>// denna funktion körs automatiskt när programmet behöver ritas om</FONT>
     <FONT COLOR="#0000FF">public void</FONT> paint<FONT COLOR="#800000">(</FONT> Graphics g<FONT COLOR="#800000">)</FONT>
     <FONT COLOR="#800000">{</FONT>
         <FONT COLOR="#008000">// Här borde vi göra någonting vettigt...</FONT>
     <FONT COLOR="#800000">}</FONT></b>

     <FONT COLOR="#008000">// resten av klassen ännu oförändrad</FONT>
    
</PRE>
</b>
<p>I paint funktionen har vi panelens Graphics och behöver bara använda t.ex. drawLine.
Problemet är bara att vi inte vet vd vi skall rita eftersom vi aldrig tagit
vara på koordinaterna.</p>
<p>Om vi föregående kapitels kod så så ser vi att det för varje gång vi drar ett
streck vi att generera ett antal punkter i en färg</p>
<p>Vi behöver ha någonstans att spara dessa punkter. För tillfället bortser vi från färgerna.</p>
<h4>Steg 2 - lagra punkterna i en variabel</h4>
<p>I Java, i likhet med de flesta andra programmeringsspråk, finns Arrays. En möjlighet
skulla vara att skapa en Array med punkter. Nackdelen med Arrays är att de har en fast storlek, och
vi vet ju inte riktigt hur många punkter vi behöver.</p>
<p>Vid en genomsökning av Java hjälp hittar man en klass benämns <b>Vector</b>, som har den egenskapen at den kan växa vid behov.<p>
<p>Modifiera programmet på följande sätt (fetstil betyder nytillsatt):</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>

<b><FONT COLOR="#0000FF">import</FONT> java.util.Vector<FONT COLOR="#800000">;</FONT></b>

<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="#008000">// ----------------------------------------------------</FONT>
     <b><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></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>
     <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">// ----------------------------------------------------</FONT>
         <b><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">// börja alltid från den andra, så kan vi </FONT>
         <FONT COLOR="#008000">// enkelt härleda den föregående</FONT>
         <FONT COLOR="#0000FF">for</FONT> <FONT COLOR="#800000">(</FONT><FONT COLOR="#0000FF">int</FONT> i <FONT COLOR="#800000">=</FONT> <FONT COLOR="#800080">1</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">// (Point) i nedanstående rader betyder att vi säger åt Vektorn att
             // det objekt den har haft hand om faktiskt är en Point. </font>
             Point p1 <FONT COLOR="#800000">=</FONT> <FONT COLOR="#800000">(</FONT>Point<FONT COLOR="#800000">)</FONT> allStrokes.elementAt<FONT COLOR="#800000">(</FONT>i<FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             Point p2 <FONT COLOR="#800000">=</FONT> <FONT COLOR="#800000">(</FONT>Point<FONT COLOR="#800000">)</FONT> allStrokes.elementAt<FONT COLOR="#800000">(</FONT>i<FONT COLOR="#800000">-</FONT><FONT COLOR="#800080">1</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
             g.drawLine<FONT COLOR="#800000">(</FONT>p1.x<FONT COLOR="#800000">, </FONT>p1.y<FONT COLOR="#800000">, </FONT>p2.x<FONT COLOR="#800000">, </FONT>p2.y<FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT>
         <FONT COLOR="#800000">}</FONT></b>
         <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
     <FONT COLOR="#800000">}</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> 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>
             <FONT COLOR="#008000">// ----------------------------------------------------</FONT>
             <b>allStrokes.removeAllElements<FONT COLOR="#800000">(</FONT><FONT COLOR="#800000">)</FONT><FONT COLOR="#800000">;</FONT></b>
             <FONT COLOR="#008000">// ----------------------------------------------------</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>
         <b>allStrokes.addElement<FONT COLOR="#800000">(</FONT>e.getPoint<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="#800000">}</FONT>
<FONT COLOR="#800000">}</FONT>
</PRE>
<p><a href="./scribble/Page2.htm" target="_new">Prova programmet i nuvarande skick</a></p>
<!--#include file="../inc/footer.asp" -->