Documente Academic
Documente Profesional
Documente Cultură
Dibujo básico
En esta parte del tutorial Java 2D, hacemos algunos dibujos básicos.
Puntos
La primitiva gráfica más simple es un punto. Es un solo punto en la ventana. Hay una clase Point para
representar un punto en un espacio de coordenadas, pero no hay ningún método para dibujar un punto.
Para dibujar un punto, utilizamos el drawLine()método, donde proporcionamos un punto para los dos
argumentos del método.
PointsEx.java
paquete com.zetcode;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
Superficie pública () {
initTimer ();
}
temporizador de retorno;
}
zetcode.com/gfx/java2d/basicdrawing/ 1/13
26/9/2019 Dibujo básico en Java 2D
g2d.setPaint (Color.blue);
@Anular
public void paintComponent (Gráficos g) {
super.paintComponent (g);
doDrawing (g);
}
@Anular
public void actionPerformed (ActionEvent e) {
repintar ();
}
}
PointsEx público () {
initUI ();
}
setTitle ("Puntos");
setSize (350, 250);
setLocationRelativeTo (nulo);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
zetcode.com/gfx/java2d/basicdrawing/ 2/13
26/9/2019 Dibujo básico en Java 2D
El ejemplo dibuja al azar 2000 puntos en la ventana. Un temporizador se utiliza para dibujar puntos en un
ciclo.
g2d.setPaint (Color.blue);
Obtenemos un número aleatorio en el rango del tamaño del área que calculamos anteriormente.
Here we draw the point. As we already mentioned, we use the drawLine() method. We specify the same
point twice.
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
zetcode.com/gfx/java2d/basicdrawing/ 3/13
26/9/2019 Dibujo básico en Java 2D
Each action event, we call the repaint() method. It causes the whole client area to be redrawn.
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Timer timer = surface.getTimer();
timer.stop();
}
});
When the window is about to be closed, we retrieve the timer and close it with its stop() method. Timers
not explicitly cancelled may hold resources indefinitely. The EXIT_ON_CLOSE default closing operation clos
the JVM and all its threads so this is not necessary for our example. But as a good programming practice an
as a remainder, we do it nevertheless.
Figure: Points
Lines
A line is a simple graphics primitive. A line is an object which connects two points. Lines are drawn with th
drawLine() method.
LinesEx.java
package com.zetcode;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
zetcode.com/gfx/java2d/basicdrawing/ 4/13
26/9/2019 Dibujo básico en Java 2D
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public LinesEx() {
initUI();
}
add(new Surface());
setTitle("Lines");
setSize(350, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
A straight line is drawn. The parameters of the method are the x, y coordinates of the two points.
zetcode.com/gfx/java2d/basicdrawing/ 5/13
26/9/2019 Dibujo básico en Java 2D
Figure: Lines
BasicStroke
The BasicStroke class defines a basic set of rendering attributes for the outlines of graphics primitives.
These rendering attributes include width, end caps, line joins, miter limit, and dash.
BasicStrokesEx.java
package com.zetcode;
import java.awt.BasicStroke;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
zetcode.com/gfx/java2d/basicdrawing/ 6/13
26/9/2019 Dibujo básico en Java 2D
g2d.setStroke(bs1);
g2d.drawLine(20, 80, 250, 80);
g2d.setStroke(bs2);
g2d.drawLine(20, 120, 250, 120);
g2d.setStroke(bs3);
g2d.drawLine(20, 160, 250, 160);
g2d.setStroke(bs4);
g2d.drawLine(20, 200, 250, 200);
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public BasicStrokesEx() {
initUI();
}
add(new Surface());
setTitle("Basic strokes");
setSize(280, 270);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
zetcode.com/gfx/java2d/basicdrawing/ 7/13
26/9/2019 Dibujo básico en Java 2D
In this example, we show various types of dashes. A dash attribute is a pattern, which is created by mixing
opaque and transparent sections.
We are going to change the stroke attribute of the Graphics object; therefore, we work with a copy of the
Graphics object. (Remember that a copy must be created if we change attributes other than fonts, colours,
or rendering hints.)
g2d.setStroke(bs1);
We use the setStroke() method to apply the BasicStroke to the current graphics context.
g2d.dispose();
zetcode.com/gfx/java2d/basicdrawing/ 8/13
26/9/2019 Dibujo básico en Java 2D
Caps
Caps are decorations applied to the ends of unclosed subpaths and dash segments. There are three differen
end caps in Java 2D: CAP_BUTT, CAP_ROUND, and CAP_SQUARE.
CAP_BUTT — ends unclosed subpaths and dash segments with no added decoration.
CAP_ROUND — ends unclosed subpaths and dash segments with a round decoration that has a radius
equal to half of the width of the pen.
CAP_SQUARE — ends unclosed subpaths and dash segments with a square projection that extends
beyond the end of the segment to a distance equal to half of the line width.
CapsEx.java
package com.zetcode;
import java.awt.BasicStroke;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
zetcode.com/gfx/java2d/basicdrawing/ 9/13
26/9/2019 Dibujo básico en Java 2D
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public CapsEx() {
initUI();
}
add(new Surface());
setTitle("Caps");
setSize(280, 270);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
A basic stroke with a butt cap is created and applied. A CAP_BUTT adds no decoration.
zetcode.com/gfx/java2d/basicdrawing/ 10/13
26/9/2019 Dibujo básico en Java 2D
We draw three vertical lines to explain the differences between the end caps. Lines with CAP_ROUND and
CAP_SQUARE are bigger than the line with CAP_BUTT. Exactly how much bigger depends on the line size. In
our case a line is 8 px thick. Lines are bigger by 8 px—4 px on the left and 4 px on the right. It should be cle
from the picture.
Figure: Caps
Joins
Line joins are decorations applied at the intersection of two path segments and at the intersection of the
endpoints of a subpath. There are three decorations: JOIN_BEVEL, JOIN_MITER, and JOIN_ROUND.
JOIN_BEVEL — joins path segments by connecting the outer corners of their wide outlines with a
straight segment.
JOIN_MITER — joins path segments by extending their outside edges until they meet.
JOIN_ROUND — joins path segments by rounding off the corner at a radius of half the line width.
JoinsEx.java
package com.zetcode;
import java.awt.BasicStroke;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
zetcode.com/gfx/java2d/basicdrawing/ 11/13
26/9/2019 Dibujo básico en Java 2D
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public JoinsEx() {
initUI();
}
add(new Surface());
setTitle("Joins");
setSize(340, 110);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run () {
zetcode.com/gfx/java2d/basicdrawing/ 12/13
26/9/2019 Dibujo básico en Java 2D
}
}
Figura: Se une
zetcode.com/gfx/java2d/basicdrawing/ 13/13