Ir al contenido principal

Uso aplicado de la instrucción "For"


A continuación se presenta una pequeña aplicación, donde se utiliza la instrucción de control "For". Este ejemplo muestra como construir un Applet y como dibujar sobre ella (utilizando para ello la instrucción "For"), una seria de rombos, existen dos prototipos (métodos), el primer "paintRomboLine", resulta mas fácil de entender, pero necesita mas código para funcionar (4 instrucciones "for"), el segundo prototipo solo ocupa una instrucción "for", y realiza una serie de cálculos por cuadrantes.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;

import javax.swing.JApplet;

/**
 * Simple class to paint a rombo based in lines.
 * 
 * @author jsanca
 * 
 */
public class DrawRombo extends JApplet {

private static final long serialVersionUID = 7677516509658561962L;

public DrawRombo() throws HeadlessException {

super();
setSize(new Dimension(1200, 1200));
} // DrawCircularLine.

@Override
public void paint(Graphics g) {

int y = 180;
int x = 180;
int intensity = 4;

super.paint(g);
this.paintRomboLine(g);
this.paintRomboLine(g, x, y, intensity);

y = 270;
x = 90;
intensity = 2;
this.paintRomboLine(g, x, y, intensity);

y = 270;
x = 270;
intensity = 1;
this.paintRomboLine(g, x, y, intensity);

y = 360;
x = 180;
intensity = 8;
this.paintRomboLine(g, x, y, intensity);

y = 540;
x = 180;
intensity = 16;
this.paintRomboLine(g, x, y, intensity);

y = 450;
x = 90;
intensity = 90;
this.paintRomboLine(g, x, y, intensity);

y = 450;
x = 270;
intensity = 45;
this.paintRomboLine(g, x, y, intensity);
} // paint.

private void paintRomboLine(Graphics graphics) {

int x1 = 90;
int y1 = 90;
int x2 = x1;
int y2 = y1 - 90;

for (int i = 0; i <= 90; ++i) {

graphics.drawLine(x1, y1, x2++, y2++);
}

for (int i = 0; i <= 90; ++i) {

graphics.drawLine(x1, y1, x2--, y2++);
}

for (int i = 0; i <= 90; ++i) {

graphics.drawLine(x1, y1, x2--, y2--);
}

for (int i = 0; i <= 90; ++i) {

graphics.drawLine(x1, y1, x2++, y2--);
}

} // paintRomboLine.

private void paintRomboLine(Graphics graphics, final int x, final int y,
final int intensity) {

int x1 = Math.abs(x); // 180; // 90;
int y1 = Math.abs(y); // 180; // 90;
int x2 = x1;
int y2 = y1 - 90;
int cuadrante = 0;
int div = Math.abs(intensity);
div = (div == 0) ? 1 : div;
int iteration = 360 / div;

for (int i = 0; i <= iteration; ++i) {

if (i % (iteration / 4) == 0) {

cuadrante += 1;
}

x2 += (1 * intensity)
* ((cuadrante == 2 || cuadrante == 3) ? -1 : 1);
y2 += (1 * intensity)
* ((cuadrante == 3 || cuadrante == 4) ? -1 : 1);

graphics.drawLine(x1, y1, x2, y2);
}
} // paintCircularLine.

} // E:O:F:DrawCircularLine.


Comentarios

Entradas más populares de este blog

Pasos para remover Postgresql 8.3 en MAC OS

Tomado de: http://forums.enterprisedb.com/posts/list/1437.page In Mac OSX: (Assuming Default Locations) Via uninstaller: 1) In the installation directory, there will be a uninstall-postgresql.app file will be there, executing (double clicking) that will uninstall the postgresql installation. Manual Uninstallation: 1) Stop the server sudo /sbin/SystemStarter stop postgresql-8.3 2) Remove menu shortcuts: sudo rm -rf /Applications/PostgreSQL 8.3 3) Remove the ini file sudo rm -rf /etc/postgres-reg.ini 4) Removing Startup Items sudo rm -rf /Library/StartupItems/postgresql-8.3 5) Remove the data and installed files sudo rm -rf /Library/PostgreSQL/8.3 6) Delete the user postgres sudo dscl . delete /users/postgres

Enrique Bunbury - 3 de Octubre 2009 - Heredia CR

Via periodico la nacion, me entero de este conciertazo: http://www.nacion.com/viva/2009/julio/29/viva2042768.html Ah esperar mas detalles y la venta de la entradas, "Para solo si me perdonas ..." EDIT ..... Via http://www.enriquebunbury.com/hellville.aspx (gracias a andres por su post) venta anticipada: Entradas a la venta a partir del 13 de agosto en: www.costaricaticket.com Por ahi estare anunciando, el 13 si las estan vendiendo!

Depeche Mode - Costa Rica - 8 de Octubre 2009

Que año de conciertos, primero Jaguares, ahora los Cadillacs y en Octubre Depeche Mode, solo nos falta RadioHead, para ser feliz. Así como lo oyen; desde el sitio principal de Depeche (clickea aquí ), pueden ver la siguiente fecha: Thursday, October 8th, 2009 San Jose, Costa Rica En venta: June 15th, 2009 Demasiado bueno!!!!! La cita será en Octubre, eso si no nos mata la fiebre porcina :( saludos, J