highlight and unhighlight the lines of the sourceCode

This commit is contained in:
M.Scholz 2012-06-19 16:54:07 +02:00
parent 54d96a1315
commit 62f2149d5a
2 changed files with 304 additions and 49 deletions

View File

@ -9,14 +9,17 @@ import java.awt.Color;
import java.util.Locale;
import java.util.Random;
import algoanim.primitives.IntMatrix;
import algoanim.primitives.SourceCode;
import algoanim.primitives.StringMatrix;
import algoanim.primitives.generators.Language;
import algoanim.properties.AnimationPropertiesKeys;
import algoanim.properties.MatrixProperties;
import algoanim.properties.RectProperties;
import algoanim.properties.SourceCodeProperties;
import algoanim.properties.TextProperties;
import algoanim.util.Coordinates;
import algoanim.util.DisplayOptions;
import algoanim.util.Offset;
import algoanim.util.Timing;
@ -47,26 +50,23 @@ public class MatrixGenerator implements Generator {
*/
private Language lang;
/**
* random number generator for user interaction
*/
private Random rand = new Random();
/**
* The info text as a SourceCode object (first slide)
*/
private SourceCode info = null;
/**
* The statement text as a SourceCode object (last several slides)
*/
private SourceCode statement = null;
/**
* The sourceCode
*/
private SourceCode sc = null;
/**
* The matrices
*/
private IntMatrix intMatrixA = null;
private IntMatrix intMatrixB = null;
private IntMatrix intMatrixResult = null;
// =========================================================================================================
/**
@ -89,16 +89,16 @@ public class MatrixGenerator implements Generator {
public void generateInfoText() {
// create first page of animation (info text) as a code group
this.info = lang.newSourceCode(new Coordinates(10, 75), "info", null, this.TEXT_PROPS);
this.info.addCodeLine("Das hier vorgestellte Verfahren eignet sich fuer die Multiplikation zweier ganzer Zahlen.", null, 0, null);
this.info.addCodeLine("Die Funktionsweise laesst sich in die folgenden fuenf Schritte gliedern:", null, 0, null);
this.info.addCodeLine("1. Schreibe die beiden zu multiplizierenden Zahlen nebeneinander.", null, 1, null);
this.info.addCodeLine("2. Die linke Zahl wird halbiert (Reste werden abgerundet), die rechte Zahl wird verdoppelt.", null, 1, null);
this.info.addCodeLine(" Die beiden berechneten Zahlen werden in die darauffolgende Zeile geschrieben.", null, 2, null);
this.info.addCodeLine("3. Schritt 2 wird solange wiederholt, bis in der linken Spalte eine 1 steht.", null, 1, null);
this.info.addCodeLine("4. Nun streicht man alle Zeilen, in denen die linke Zahl gerade ist.", null, 1, null);
this.info.addCodeLine("5. Schlussendlich werden alle uebrigen Zahlen der rechten Spalte addiert.", null, 1, null);
this.info = lang.newSourceCode(new Coordinates(10, 75), "info", null, TEXT_PROPS);
this.info.addCodeLine("Das hier vorgestellte Verfahren eignet sich zur Multiplikation zweier Matrizen.", null, 0, null);
this.info.addCodeLine("Zwei Matrizen können genau dann multipliziert werden, wenn die Spaltenanzahl der", null, 0, null);
this.info.addCodeLine("linken Matrix mit der Zeilenanzahl der rechten Matrix übereinstimmen. ", null, 0, null);
this.info.addCodeLine("Das Verfahren lässt sich leicht iterativ implementieren.", null, 0, null);
this.info.addCodeLine("Die Funktioneweise ist hierbei wie folgt:", null, 0, null);
this.info.addCodeLine("- Betrachte die i-te Zeile der linken Matrix und die i-te Spalte der rechten Matrix.", null, 1, null);
this.info.addCodeLine("- Nun wird des j-te Element der i-ten Zeilen mit dem j-ten Element der i-ten Spalte multipliziert.", null, 1, null);
this.info.addCodeLine("- Die einzelnen Ergebnisse der Multiplikationen werden addiert und man erhält das Element e(ij) der Ergebnismatrix E.", null, 1, null);
}
/**
@ -111,7 +111,7 @@ public class MatrixGenerator implements Generator {
textProperties.set(AnimationPropertiesKeys.COLOR_PROPERTY, Color.BLACK);
textProperties.set(AnimationPropertiesKeys.DEPTH_PROPERTY, 1);
textProperties.set(AnimationPropertiesKeys.FONT_PROPERTY, new java.awt.Font("Serif", Font.SANSSERIF, 24));
lang.newText(new Coordinates(11, 15), "Russische Bauernmultiplikation", "header", null, textProperties);
lang.newText(new Coordinates(11, 15), "Falksches Schema", "header", null, textProperties);
// header background
RectProperties rectProperties = new RectProperties();
@ -127,38 +127,140 @@ public class MatrixGenerator implements Generator {
public void generateSourceCode() {
// initialize source code object and add code lines
this.sc = lang.newSourceCode(new Coordinates(10, 147), "sourceCode", null, this.SC_PROPS);
this.sc.addCodeLine("public int russe(int a, int b){", null, 0, null); // 0
this.sc.addCodeLine("if(a == 1){", null, 1, null); // 1
this.sc.addCodeLine("return b;", null, 2, null); // 2
this.sc = lang.newSourceCode(new Coordinates(150, 45), "sourceCode", null, SC_PROPS);
this.sc.addCodeLine("public void falkschesSchema(int[][] matrixA, int[][] matrixB){", null, 0, null); // 0
this.sc.addCodeLine("int[][] result = new int[matrixA.length][matrixB[0].length];", null, 1, null); // 1
this.sc.addCodeLine("int summe = 0;", null, 1, null); // 1
this.sc.addCodeLine("for(int i=0; i<matrixA.length; i++){", null, 1, null); // 2
this.sc.addCodeLine("for( int j=0; j<matrixB[0].length; j++){", null, 2, null); // 3
this.sc.addCodeLine("for(int k=0; k<matrixA[0].length; k++){", null, 3, null); // 3
this.sc.addCodeLine("summe = summe + matrixA[i][k]*matrixB[k][j];", null, 4, null); // 3
this.sc.addCodeLine("result[i][j]=summe;", null, 4, null); // 3
this.sc.addCodeLine("}", null, 3, null); // 3
this.sc.addCodeLine("summe = 0;", null, 3, null); // 3
this.sc.addCodeLine("}", null, 2, null); // 3
this.sc.addCodeLine("}", null, 1, null); // 3
this.sc.addCodeLine("if(a % 2 == 1){", null, 1, null); // 4
this.sc.addCodeLine("return b + russe(a/2, b*2);", null, 2, null); // 5
this.sc.addCodeLine("}else{", null, 1, null); // 6
this.sc.addCodeLine("return russe(a/2, b*2);", null, 2, null); // 7
this.sc.addCodeLine("}", null, 1, null); // 8
this.sc.addCodeLine("}", null, 0, null); // 9
}
/**
* Generates the one single matrix including matrixA, matrixB und the calculated result matrix
*/
public void generateMatrix(){
MatrixProperties mp = new MatrixProperties();
mp.set(AnimationPropertiesKeys.FILL_PROPERTY, Color.WHITE);
//generate matrixB // X-Achse, Y-Achse
this.intMatrixB = lang.newIntMatrix(new Offset(20, 40, sc, AnimalScript.DIRECTION_S), matrixB, "matrixB", null, mp);
//generate matrixA with an offset to matrixB
this.intMatrixA = lang.newIntMatrix(new Offset((-23*matrixB.length), 10, intMatrixB, AnimalScript.DIRECTION_SW), matrixA, "matrixA", null, mp);
//generate result matrix
int[][] result = new int[matrixA.length][matrixA[0].length];
//fill result[][] with zeros
for(int i = 0; i < result.length; i++){
for(int j = 0; j < result[i].length; j++){
result[i][j] = 0;
}
}
this.intMatrixResult = lang.newIntMatrix(new Offset(0, 10, intMatrixB, AnimalScript.DIRECTION_SW), result, "table", null, mp);
}
/**
* Falksches Schema
*
* @param matrixA
* @param matrixB
*/
public void falk(int[][] matrixA, int[][] matrixB){
int[][] result = new int[matrixA.length][matrixB[0].length];
int summe=0;
for(int i=0; i<matrixA.length; i++){
for( int j=0; j<matrixB[0].length; j++){
for(int k=0; k<matrixA[0].length; k++){
summe = summe + matrixA[i][k]*matrixB[k][j];
result[i][j]=summe;
}
summe=0;
private void falk(){
//STEP
lang.nextStep();
info.hide();
//generate the source code
generateSourceCode();
//generate matrices
generateMatrix();
//STEP
lang.nextStep();
sc.highlight(0);
//STEP
lang.nextStep();
sc.unhighlight(0);
sc.highlight(1);
//STEP
lang.nextStep();
sc.unhighlight(1);
sc.highlight(2);
int[][] result = new int[matrixA.length][matrixB[0].length]; // 1
int summe = 0; // 2
for(int i=0; i<matrixA.length; i++){ // 3
//STEP
lang.nextStep();
sc.unhighlight(2);
sc.unhighlight(9);
sc.highlight(3);
for( int j=0; j<matrixB[0].length; j++){ // 4
//STEP
lang.nextStep();
sc.unhighlight(9);
sc.unhighlight(3);
sc.highlight(4);
for(int k=0; k<matrixA[0].length; k++){ // 5
//STEP
lang.nextStep();
sc.unhighlight(7);
sc.unhighlight(4);
sc.highlight(5);
summe = summe + matrixA[i][k]*matrixB[k][j]; // 6
//STEP
lang.nextStep();
sc.unhighlight(5);
sc.highlight(6);
result[i][j]=summe; // 7
//STEP
lang.nextStep();
sc.unhighlight(6);
sc.highlight(7);
} // 8
//STEP
lang.nextStep();
sc.unhighlight(7);
sc.highlight(9);
summe = 0; // 9
}
}
//STEP
lang.nextStep();
sc.unhighlight(9);
}
/**
* Generates the statement for the last slides
*/
private void generateStatement(){
//hide old stuff
sc.hide();
intMatrixA.hide();
intMatrixB.hide();
intMatrixResult.hide();
//TODO: implement the statement for the last view slides!
}
@ -172,9 +274,14 @@ public class MatrixGenerator implements Generator {
public String generate(AnimationPropertiesContainer props, Hashtable<String, Object> primitives) {
matrixA = (int[][]) primitives.get("matrixA");
matrixB = (int[][]) primitives.get("matrixB");
//SC_PROPS = (SourceCodeProperties) props.getPropertiesByName("Quelltext");
//TEXT_PROPS = (SourceCodeProperties) props.getPropertiesByName("Text");
SC_PROPS = (SourceCodeProperties) props.getPropertiesByName("Quelltext");
TEXT_PROPS = (SourceCodeProperties) props.getPropertiesByName("Text");
generateHeader();
generateInfoText();
falk();
generateStatement();
lang.finalizeGeneration();
return lang.toString();
@ -197,19 +304,19 @@ public class MatrixGenerator implements Generator {
@Override
public String getDescription() {
return "Das hier vorgestellte Verfahren eignet sich zur Multiplikation zweier Matrizen. Zwei Matrizen kšnnen genau dann" +
return "Das hier vorgestellte Verfahren eignet sich zur Multiplikation zweier Matrizen. Zwei Matrizen k&ouml;nnen genau dann" +
"multipliziert werden, wenn die Spaltenanzahl der linken Matrix mit der Zeilenanzahl der rechten Matrix" +
"&uuml;bereinstimmt. Das Verfahren l&auml;sst sich leicht iterativ implementieren." +
"<br>Die Funktioneweise ist hierbei wie folgt:" +
"<br>- Betrachte die i-te Zeile der linken Matrix und die i-te Spalte der rechten Matrix." +
"<br>- Nun wird des j-te Element der i-ten Zeilen mit dem j-ten Element der i-ten Spalte multipliziert." +
"<br>- Die einzelnen Ergebnisse der Multiplikationen werden addiert und man erhŠlt das Element e(ij) der Ergebnismatrix E.";
"<br>- Die einzelnen Ergebnisse der Multiplikationen werden addiert und man erh&auml;lt das Element e(ij) der Ergebnismatrix E.";
}
@Override
public String getCodeExample() {
return "public1 int[][] falk(int[][] matrixA, int[][] matrixB){" +
return "public1 int[][] falkschesSchema(int[][] matrixA, int[][] matrixB){" +
"\n int[][] result = new int[matrixA.length][matrixB[0].length];" +
"\n int summe=0;" +
"\n for(int i=0; i < matrixA.length; i++){" +

View File

@ -3,11 +3,159 @@
<Folder name="Root">
<Primitive type="intMatrix">
<name>matrixA</name>
<value><intMatrix>1, 2, 3, 4; 5, 6, 7, 8</intMatrix></value>
<value><intMatrix>1, 2; 3, 4</intMatrix></value>
</Primitive>
<Primitive type="intMatrix">
<name>matrixB</name>
<value><intMatrix>1, 2, 3, 4; 5, 6, 7, 8</intMatrix></value>
<value><intMatrix>5, 6; 7, 8</intMatrix></value>
</Primitive>
<AnimationProperties type="SourceCodeProperties">
<AnimationPropertyItem type="BooleanPropertyItem">
<name>bold</name>
<isEditable value="false" />
<label></label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>color</name>
<isEditable value="true" />
<label>Farbe</label>
<value><Color>(0, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>highlightColor</name>
<isEditable value="true" />
<label>Markierungsfarbe Quelltext</label>
<value><Color>(255, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="BooleanPropertyItem">
<name>hidden</name>
<isEditable value="false" />
<label></label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="FontPropertyItem">
<name>font</name>
<isEditable value="true" />
<label>Schriftart</label>
<value><Font>SansSerif</Font></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="StringPropertyItem">
<name>name</name>
<isEditable value="true" />
<label></label>
<value><String>Quelltext</String></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>contextColor</name>
<isEditable value="false" />
<label></label>
<value><Color>(0, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="BooleanPropertyItem">
<name>italic</name>
<isEditable value="false" />
<label></label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>depth</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>row</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>size</name>
<isEditable value="false" />
<label></label>
<value><int>10</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>indentation</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
</AnimationProperties>
<AnimationProperties type="SourceCodeProperties">
<AnimationPropertyItem type="BooleanPropertyItem">
<name>bold</name>
<isEditable value="true" />
<label>Fett</label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>color</name>
<isEditable value="true" />
<label>Farbe</label>
<value><Color>(0, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>highlightColor</name>
<isEditable value="false" />
<label></label>
<value><Color>(255, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="BooleanPropertyItem">
<name>hidden</name>
<isEditable value="false" />
<label></label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="FontPropertyItem">
<name>font</name>
<isEditable value="true" />
<label>Schriftart</label>
<value><Font>SansSerif</Font></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="StringPropertyItem">
<name>name</name>
<isEditable value="true" />
<label></label>
<value><String>Text</String></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="ColorPropertyItem">
<name>contextColor</name>
<isEditable value="false" />
<label></label>
<value><Color>(0, 0, 0)</Color></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="BooleanPropertyItem">
<name>italic</name>
<isEditable value="true" />
<label>Kursiv</label>
<value><boolean value="false" /></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>depth</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>row</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>size</name>
<isEditable value="true" />
<label>Größe</label>
<value><int>10</int></value>
</AnimationPropertyItem>
<AnimationPropertyItem type="IntegerPropertyItem">
<name>indentation</name>
<isEditable value="false" />
<label></label>
<value><int>1</int></value>
</AnimationPropertyItem>
</AnimationProperties>
</Folder>
</PropertiesTreeModel>