Dla mojej pracy domowej dla Gobelinu, muszę pokazać diament na stole z tablicy ciągów. Oto co mam do tej pory:jak narysować diament za pomocą elementu gobelinowego t: pętla
kod Index.java
public class Index
{
@Property
private Integer number;
@Property
private String [] table;
public Index() {
number = 9;
int temp = 0;
String tmp = "-";
table = new String[number * number];
if(singleCell == null)
singleCell="";
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
table[temp] = tmp;
temp++;
}
}
}
@OnEvent(component="diamond")
Object onDiamondLink() {
String swapValue = "*";
int sum = number/2 ;
int x1 = number-1;
int sumY = number/2;
int y1 = number+1;
int temp = x1 + sumY;
for (int i = 0; i < table.length; i++) {
table[i] = "-";
}
for (int i = 0; i < table.length; i++) {
if(i == sum) {
table[i] = swapValue;
sum = sum + x1;
}
if (i == sumY) {
table[i] = swapValue;
sumY = sumY + y1;
}
}
System.out.println("link diamond is activate");
return null;
}
public boolean isStartRow(){
return (myIndex%9 ==0);
}
public boolean isEndRow(){
return (myIndex%9 == 8);
}
public String getStartTR(){
return "<tr >";
}
public String getEndTR(){
return "</tr>";
}
kod index.tml:
<t:actionlink t:id="diamond" >Diamond table</t:actionlink>
<br/>
<h1>Result:</h1>
<table border="1" >
<t:loop t:source="table" t:value="singleCell" index="MyIndex">
<t:if test="startRow">
<t:outputraw value="startTR"/>
</t:if>
<td width="20px">
${singleCell}
</td>
<t:if test="endRow">
<t:outputraw value="endTR"/>
</t:if>
</t:loop>
</table>
Ten kod generuje ten wynik:
- - - - * - - - -
- - - * - * - - -
- - * - - - * - -
- * - - - - - * -
* - - - - - - - *
- - - - - - - * -
* - - - - - * - -
- * - - - * - - -
- - * - * - - - -
Poprawna potrzebna jest następująca wartość:
- - - - * - - - -
- - - * - * - - -
- - * - - - * - -
- * - - - - - * -
* - - - - - - - *
- * - - - - - * -
- - * - - - * - -
- - - * - * - - -
- - - - * - - - -
Wszelkie pomysły będą bardzo pomocne.
Prawie go przybiłeś, Jedyne, co przegapiłeś, to RowCount - ColCount == numer/2 część, odsyłam moją odpowiedź poniżej. To powinno być dla ciebie jasne. – nikhil
Jestem raczej rozczarowany tymi dwiema odpowiedziami. Nie ze względu na treść, ale ze względu na zasadę .. pytanie wyraźnie mówi "to jest dla mojej pracy domowej", a wy po prostu podałeś kod bez żadnego wyjaśnienia. Ludzie nie będą się uczyć przez kopiowanie/wklejanie kodu. – Coeffect
@Mannimarco Zrobiłem aktualizację, może on spróbować użyć komponentu outputraw, aby spróbować wyświetlić go jako html. Informacje na temat pierwszego planu można znaleźć w google lub na tapestry.org – sfrj