2011-06-23 14 views
6

Para mi tarea de Tapestry, tengo que mostrar un diamante en la mesa de un conjunto de cuerdas. Esto es lo que tengo hasta ahora:cómo dibujar diamantes utilizando el componente tapiz t: loop

código 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>"; 
    } 

código de 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> 

Este código genera esta salida:

- - - - * - - - - 
- - - * - * - - - 
- - * - - - * - - 
- * - - - - - * - 
* - - - - - - - * 
- - - - - - - * - 
* - - - - - * - - 
- * - - - * - - - 
- - * - * - - - - 

La correcta La salida que necesito es la siguiente:

- - - - * - - - - 
- - - * - * - - - 
- - * - - - * - - 
- * - - - - - * - 
* - - - - - - - * 
- * - - - - - * - 
- - * - - - * - - 
- - - * - * - - - 
- - - - * - - - - 

Cualquier idea será de gran ayuda.

+0

Casi lo has clavado, Lo único que te has perdido es RowCount - ColCount == number/2 parte, consulte mi respuesta a continuación. Debería estar claro para ti allí. – nikhil

+5

Estoy bastante decepcionado con las dos respuestas. No por el contenido, sino por el principio ... la pregunta claramente dice "esto es para mi tarea" y ustedes simplemente dieron el código sin ningún tipo de explicación. La gente no va a aprender copiando/pegando su código. – Coeffect

+0

@Mannimarco Hice una actualización, puede probar usar el componente outputraw para tratar de mostrar como html. La información anterior se puede encontrar en google o en tapestry.org – sfrj

Respuesta

2

¿Quieres dibujar un diamante? Prueba este algoritmo:

public class Diamond { 

    @Property 
    @Persist 
    private String diamond; 


    @SetupRender  
    init(){ 
     int n,i,j,k; 

    do { 

     n = (int)(Math.random() * 10 + 3); 

     }while(n % 2 == 0); 

     diamond += ""+n+"<br\/>"; 

     System.out.println(); 

    for (i = 1; i <= n; i++){ 

     for (k = n; k > i; k--) 
      diamond += "-"; 

     for (j =1; j <= i; j++) 
      diamond += "*"+"-"; 


     diamond += "<br\/>"; 

     } 

    for (i = n; i > 0; i--){ 

     for (k = n; k > i; k--) 
      diamond += "-"; 

     for (j =1; j <= i; j++) 
       diamond += "*"+"-"; 

       diamond += "<br\/>"; 

     } 
} 
} 

ACTUALIZACIÓN

Espera un segundo, que desea crear una página de tapicería, que atrae a ese diamante del asterisco ¿verdad?

Una opción sería utilizar:.

<t:outputraw value="${diamond}"/> 

sólo tiene que establecer que la parte de cuerda .java de su página (Ver el código anterior se actualizó) se deban hacer

Su salida como html, puedes usar los algoritmos que te dimos e insertar html breaks en lugar de println()

+0

hola gracias por la ayuda. Tengo que usar el componente loop y table en tml ... no outputdraw component – dusmanka

0

Esto debería imprimir la salida requerida:

public class Diamond 
{ 
    public static void main(String []args) 
    { 
    for(int i=0;i<9;i++){ 
     for(int j=0;j<9;j++) 
     if((i + j == 4) || (i-j == 4)||(i+j == 12) || (j-i == 4)) 
      System.out.print("*"); 
     else 
      System.out.print("-"); 
     System.out.println(); 
    } 
    } 
} 
Cuestiones relacionadas