Ayuda para dibujar

Hola, Quisiera saber que contenedor (TextArea,JtexPane,etc) puedo usar si quiero dibujar un rectangulo, circulo... Si alguien tiene un ejemplo de como dibujar un circulo y mostrarlo en un contenedor lo agradeceria muchisimo.

1 respuesta

Respuesta
1
Este es un demo clásico de figuras en un JPanel, cópialo, compílalo, y córrelo.
Espero que te sirva bastante.
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class FigurasGeometricas extends JPanel {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
dibujarFiguras(g2);
}
public void dibujarFiguras(Graphics2D g2) {
Dimension d = getSize();
int gridWidth = 400 / 6;
int gridHeight = 300 / 2;
int rowspacing = 5;
int columnspacing = 7;
int rectWidth = gridWidth - columnspacing;
int rectHeight = gridHeight - rowspacing;
Color fg3D = Color.lightGray;
g2.setPaint(fg3D);
g2.drawRect(80, 80, 400 - 1, 310);
g2.setPaint(fg);
int x = 85;
int y = 87;
// Dibuja Línea
g2.draw(new Line2D.Double(x, y+rectHeight-1, x + rectWidth, y));
x += gridWidth;
// Dibuja Rectángulo en 2D
g2.setStroke(stroke);
g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
x += gridWidth;
// Dibuja Rectángulo Redondeado en 2D
g2.setStroke(dashed);
g2.draw(new RoundRectangle2D.Double(x, y, rectWidth,
rectHeight, 10, 10));
x += gridWidth;
// Dibuja arco en 2D
g2.setStroke(wideStroke);
g2.draw(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
135, Arc2D.OPEN));
x += gridWidth;
// Dibuja Elipse en 2D
g2.setStroke(stroke);
g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
x += gridWidth;
// Dibuja Polígono
int x1Points[] = {x, x+rectWidth, x, x+rectWidth};
int y1Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath poligono = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x1Points.length);
poligono.moveTo(x1Points[0], y1Points[0]);
for ( int index = 1; index < x1Points.length; index++ ) {
poligono.lineTo(x1Points[index], y1Points[index]);
};
poligono.closePath();
g2.draw(poligono);
x = 85;
y += gridHeight;
// draw GeneralPath (polyline)
int x2Points[] = {x, x+rectWidth, x, x+rectWidth};
int y2Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x2Points.length);
polyline.moveTo (x2Points[0], y2Points[0]);
for ( int index = 1; index < x2Points.length; index++ ) {
polyline.lineTo(x2Points[index], y2Points[index]);
};
g2.draw(polyline);
x += gridWidth;
// fill Rectangle2D.Double (red)
g2.setPaint(red);
g2.fill(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
g2.setPaint(fg);
x += g

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas