14.04.2013 Views

Gráficos Con Java 2D - Abaco

Gráficos Con Java 2D - Abaco

Gráficos Con Java 2D - Abaco

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Las siguientes líneas transforman el TextLayout para que sea centrado en el origen y luego<br />

introduce el objeto Shape resultante de la llamda a getOutline dentro del array shapes.<br />

AffineTransform textAt = new AffineTransform();<br />

textAt.translate(0,<br />

(float)textTl.getBounds().getHeight());<br />

shapes[2] = textTl.getOutline(textAt);<br />

Podemos elegir un primitivo accediendo al índice apropiado dentro del array shapes.<br />

Shape shape =<br />

shapes[Transform.primitive.getSelectedIndex()];<br />

Cómo se realiza el dibujo dependen de las opciones elegidas.<br />

● Cuando el usuario elige stroke, se llama a Graphics<strong>2D</strong>.draw para realizar el dibujo, Si se elige text<br />

como primitivo, las líneas son recuperadas y el dibujo se hace con el método draw.<br />

● Cuando el usuario elige fill, se llama a Graphics<strong>2D</strong>.fill o Graphics<strong>2D</strong>.drawString para realizar el<br />

dibujado.<br />

● Cuando el usuario elige stroke and fill, se llama a fill o drawString para rellenar el Shape, y luego<br />

se llama a draw para dibujar la línea exterior.<br />

Nota:<br />

Para rellenar y puntear un gráfico primitivo, necesitamos hacer dos llamadas separadas a<br />

métodos: fill o drawString para rellenar el interior, y draw para dibujar el exterior.<br />

Los tres estilos de línea usados en este ejemplo -- ancho, estrecho y punteado -- son ejemplares de<br />

BasicStroke.<br />

// Sets the Stroke.<br />

...<br />

case 0 : g2.setStroke(new BasicStroke(3.0f)); break;<br />

case 1 : g2.setStroke(new BasicStroke(8.0f)); break;<br />

case 2 : float dash[] = {10.0f};<br />

g2.setStroke(new BasicStroke(3.0f,<br />

BasicStroke.CAP_BUTT,<br />

BasicStroke.JOIN_MITER,<br />

10.0f, dash, 0.0f));<br />

break;<br />

El estilo de punteado de este ejemplo tiene 10 unidades de punteado alternados con 10 unidades de<br />

espacio. El principio del patrón del punteado se aplica al principio de la línea -- la fase de punteado<br />

es 0.0.<br />

En este ejemplo se usan tres estilos de dibujo -- sólido, gradiente y polka. El dibujo de color sólido<br />

es un ejemplar de Color, el gradiente un ejemplar de GradientPaint, y el patrón un ejemplar de<br />

TexturePaint.<br />

// Sets the Paint.<br />

...<br />

case 0 : g2.setPaint(Color.blue); break;<br />

case 1 : g2.setPaint(new GradientPaint(0, 0,<br />

Color.lightGray,<br />

w-250, h, Color.blue, false));<br />

break;<br />

case 2 : BufferedImage bi = new BufferedImage(5, 5,<br />

BufferedImage.TYPE_INT_RGB);<br />

Graphics<strong>2D</strong> big = bi.createGraphics();<br />

big.setColor(Color.blue);<br />

big.fillRect(0, 0, 5, 5);<br />

big.setColor(Color.lightGray);<br />

big.fillOval(0, 0, 5, 5);<br />

Rectangle r = new Rectangle(0,0,5,5);<br />

g2.setPaint(new TexturePaint(bi, r));<br />

break;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!