Sei sulla pagina 1di 15

26/9/2019 Imágenes en Java 2D

Inicio Contenido Suscribirse Anterior Siguient

Imágenes
En esta parte del tutorial Java 2D, trabajaremos con imágenes.

BufferedImagees una clase fundamental para trabajar con imágenes en Java 2D. Es un rectángulo de
píxeles almacenados en la memoria.

Mostrar una imagen


En el primer ejemplo, mostramos una imagen en el panel.

DisplayImageEx.java

paquete com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

clase Surface extiende JPanel {

Imagen privada mshi;

Superficie pública () {

cargar imagen();
setSurfaceSize ();
}

private void loadImage () {

mshi = nueva ImageIcon ("mushrooms.jpg"). getImage ();


}

privado void setSurfaceSize () {

Dimensión d = nueva Dimensión ();


d.width = mshi.getWidth (nulo);
d.height = mshi.getHeight (nulo);
setPreferredSize (d);
}

doDrawing privado vacío (Gráficos g) {

zetcode.com/gfx/java2d/java2dimages/ 1/15
26/9/2019 Imágenes en Java 2D

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage (mshi, 0, 0, nulo);
}

@Anular
public void paintComponent (Gráficos g) {

super.paintComponent (g);
doDrawing (g);
}
}

La clase pública DisplayImageEx extiende JFrame {

DisplayImageEx público () {

initUI ();
}

initUI privado vacío () {

add (nueva Surface ());

paquete();

setTitle ("Hongos");
setLocationRelativeTo (nulo);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

public static void main (String [] args) {

EventQueue.invokeLater (new Runnable () {


@Anular
public void run () {
DisplayImageEx ex = new DisplayImageEx ();
ex.setVisible (verdadero);
}
});
}
}

En el ejemplo, mostramos una imagen en el panel. El tamaño de la ventana se ajusta al tamaño de la image

private void loadImage () {

mshi = nueva ImageIcon ("mushrooms.jpg"). getImage ();


}

Usamos la ImageIconclase para cargar la imagen. La imagen se encuentra en el directorio de trabajo actua

privado void setSurfaceSize () {

Dimensión d = nueva Dimensión ();

zetcode.com/gfx/java2d/java2dimages/ 2/15
26/9/2019 Imágenes en Java 2D

d.width = mshi.getWidth (nulo);


d.height = mshi.getHeight (nulo);
setPreferredSize (d);
}

We determine the size of the loaded image. With the setPreferredSize() method, we set the preferred
size of the Surface panel. The pack() method of the JFrame container will cause the frame to fit the size o
its children. In our case the Surface panel. As a consequence, the window will be sized to exactly display th
loaded image.

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage(mshi, 0, 0, null);
}

The image is drawn on the panel using the drawImage() method. The last parameter is the ImageObserve
class. It is sometimes used for asynchronous loading. When we do not need asynchronous loading of our
images, we can just put null there.

private void initUI() {


...
pack();
...
}

The pack() method sizes the container to fit the size of the child panel.

Grayscale image
In computing, a grayscale digital image is an image in which the value of each pixel is a single sample, that
is, it carries the full (and only) information about its intensity. Images of this sort are composed exclusively
of shades of neutral gray, varying from black at the weakest intensity to white at the strongest. (Wikipedia)

In the next example, we create a grayscale image with Java 2D.

GrayScaleImage.java

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

zetcode.com/gfx/java2d/java2dimages/ 3/15
26/9/2019 Imágenes en Java 2D

class Surface extends JPanel {

private Image mshi;


private BufferedImage bufimg;
private Dimension d;

public Surface() {

loadImage();
determineAndSetSize();
createGrayImage();
}

private void determineAndSetSize() {

d = new Dimension();
d.width = mshi.getWidth(null);
d.height = mshi.getHeight(null);
setPreferredSize(d);
}

private void createGrayImage() {

bufimg = new BufferedImage(d.width, d.height,


BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g2d = bufimg.createGraphics();


g2d.drawImage(mshi, 0, 0, null);
g2d.dispose();
}

private void loadImage() {

mshi = new ImageIcon("mushrooms.jpg").getImage();


}

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage(bufimg, null, 0, 0);
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class GrayScaleImageEx extends JFrame {

public GrayScaleImageEx() {

initUI();
}

zetcode.com/gfx/java2d/java2dimages/ 4/15
26/9/2019 Imágenes en Java 2D

private void initUI() {

Surface dpnl = new Surface();


add(dpnl);

pack();

setTitle("GrayScale image");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GrayScaleImageEx ex = new GrayScaleImageEx();
ex.setVisible(true);
}
});
}
}

There are several ways to create a grayscale image. We do it by writing image data into buffered image of
BufferedImage.TYPE_BYTE_GRAY type.

bufimg = new BufferedImage(d.width, d.height,


BufferedImage.TYPE_BYTE_GRAY);

We create a BufferedImage class of type BufferedImage.TYPE_BYTE_GRAY.

Graphics2D g2d = bufimg.createGraphics();


g2d.drawImage(mshi, 0, 0, null);

Here we draw the mushrooms image into the buffered image.

g2d.dispose();

Graphics objects created with createGraphics() method should be manually released. Graphics objects
which are provided as arguments to the paint() and update() methods of components are automatically
released by the system when those methods return.

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage(bufimg, null, 0, 0);
}

The buffered image is drawn on the panel with the drawImage() method.

zetcode.com/gfx/java2d/java2dimages/ 5/15
26/9/2019 Imágenes en Java 2D

Flipped image
The following example flips image. We are going to filter the image. There is a filter() method which is
transforming images.

FlippedImageEx.java

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {

private Image mshi;


private BufferedImage bufimg;
private final int SPACE = 10;

public Surface() {

loadImage();
createFlippedImage();
setSurfaceSize();
}

private void loadImage() {

mshi = new ImageIcon("mushrooms.jpg").getImage();


}

private void createFlippedImage() {

bufimg = new BufferedImage(mshi.getWidth(null),


mshi.getHeight(null), BufferedImage.TYPE_INT_RGB);

Graphics gb = bufimg.getGraphics();
gb.drawImage(mshi, 0, 0, null);
gb.dispose();

AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);


tx.translate(-mshi.getWidth(null), 0);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufimg = op.filter(bufimg, null);
}

zetcode.com/gfx/java2d/java2dimages/ 6/15
26/9/2019 Imágenes en Java 2D

private void setSurfaceSize() {

int w = bufimg.getWidth();
int h = bufimg.getHeight();

Dimension d = new Dimension(3*SPACE+2*w, h+2*SPACE);


setPreferredSize(d);
}

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.drawImage(mshi, SPACE, SPACE, null);


g2d.drawImage(bufimg, null, 2*SPACE + bufimg.getWidth(), SPACE);
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class FlippedImageEx extends JFrame {

public FlippedImageEx() {

initUI();
}

private void initUI() {

add(new Surface());
pack();

setTitle("Flipped image");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FlippedImageEx ex = new FlippedImageEx();
ex.setVisible(true);
}
});
}
}

In our code example, we horizontally flip an image.

zetcode.com/gfx/java2d/java2dimages/ 7/15
26/9/2019 Imágenes en Java 2D

AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);


tx.translate(-castle.getWidth(null), 0);

Flipping an image means scaling it and translating it. So we do an AffineTransform operation.

AffineTransformOp op = new AffineTransformOp(tx,


AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufferedImage = op.filter(bufferedImage, null)

This is one of the filtering operations available. This could be also done by pixel manipulation. But Java 2D
provides high level classes that make it easier to manipulate images. In our case, the AffineTransformOp
class performs scaling and translation on the image pixels.

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.drawImage(mshi, SPACE, SPACE, null);


g2d.drawImage(bufimg, null, 2*SPACE + bufimg.getWidth(), SPACE);
}

Both images are painted on the panel.

private void setSurfaceSize() {

int w = bufimg.getWidth();
int h = bufimg.getHeight();

Dimension d = new Dimension(3*SPACE+2*w, h+2*SPACE);


setPreferredSize(d);
}

We set the preferred size for the panel. We calculate the size so that we can place two images on the panel
and put some space between them, and the images and the window borders.

Blurred image

The next code example blurs an image. A blur means an unfocused image. To blur an image, we use a
convolution operation. It is a mathematical operation which is also used in edge detection or noise
elimination. Blur operations can be used in various graphical effects. For example creating speed illusion o
showing an unfocused vision of a human.

The blur filter operation replaces each pixel in image with an average of the pixel and its neighbours.
Convolutions are per-pixel operations. The same arithmetic is repeated for every pixel in the image. A kern
can be thought of as a two-dimensional grid of numbers that passes over each pixel of an image in sequenc
performing calculations along the way. Since images can also be thought of as two-dimensional grids of

zetcode.com/gfx/java2d/java2dimages/ 8/15
26/9/2019 Imágenes en Java 2D

numbers, applying a kernel to an image can be visualized as a small grid (the kernel) moving across a
substantially larger grid (the image). (developer.apple.com)

BlurredImageEx.java

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {

private BufferedImage mshi;


private BufferedImage bluri;

public Surface() {

loadImage();
createBlurredImage();
setSurfaceSize();
}

private void loadImage() {

try {

mshi = ImageIO.read(new File("mushrooms.jpg"));


} catch (IOException ex) {

Logger.getLogger(Surface.class.getName()).log(
Level.WARNING, null, ex);
}
}

private void createBlurredImage() {

float[] blurKernel = {
1 / 9f, 1 / 9f, 1 / 9f,
1 / 9f, 1 / 9f, 1 / 9f,
1 / 9f, 1 / 9f, 1 / 9f
};

BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));

zetcode.com/gfx/java2d/java2dimages/ 9/15
26/9/2019 Imágenes en Java 2D

bluri = blur.filter(mshi, new BufferedImage(mshi.getWidth(),


mshi.getHeight(), mshi.getType()));
}

private void setSurfaceSize() {

Dimension d = new Dimension();


d.width = mshi.getWidth(null);
d.height = mshi.getHeight(null);
setPreferredSize(d);
}

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage(bluri, null, 0, 0);
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class BlurredImageEx extends JFrame {

public BlurredImageEx() {

setTitle("Blurred image");
add(new Surface());

pack();

setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

BlurredImageEx ex = new BlurredImageEx();


ex.setVisible(true);
}
});
}
}

In the code example, we load an image from the disk, perform a blur operation on the image, and display th
result on the window.

zetcode.com/gfx/java2d/java2dimages/ 10/15
26/9/2019 Imágenes en Java 2D

private void loadImage() {

try {

mshi = ImageIO.read(new File("mushrooms.jpg"));


} catch (IOException ex) {

Logger.getLogger(Surface.class.getName()).log(
Level.WARNING, null, ex);
}
}

The the read() method of the ImageIO class reads an image from the disk and returns a BufferedImage.

float[] blurKernel = {
1 / 9f, 1 / 9f, 1 / 9f,
1 / 9f, 1 / 9f, 1 / 9f,
1 / 9f, 1 / 9f, 1 / 9f
};

This matrix is called a kernel. The values are weights that are applied to the neighbouring values of the pixe
being changed.

BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));


bluri = blur.filter(mshi, new BufferedImage(mshi.getWidth(),
mshi.getHeight(), mshi.getType()));

Here we apply the blur filter on the image.

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;


g2d.drawImage(bluri, null, 0, 0);
}

The blurred image is drawn on the window.

Reflection

In the next example we show a reflected image. This effect makes an illusion as if the image was reflected in
water. The following code example was inspired by the code from jhlabs.com.

ReflectionEx.java

package com.zetcode;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

zetcode.com/gfx/java2d/java2dimages/ 11/15
26/9/2019 Imágenes en Java 2D

import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {

private BufferedImage image;


private BufferedImage refImage;
private int img_w;
private int img_h;
private final int SPACE = 30;

public Surface() {

loadImage();
getImageSize();
createReflectedImage();
}

private void loadImage() {

try {

image = ImageIO.read(new File("rotunda.jpg"));


} catch (Exception ex) {

Logger.getLogger(Surface.class.getName()).log(
Level.WARNING, null, ex);
}
}

private void getImageSize() {

img_w = image.getWidth();
img_h = image.getHeight();
}

private void createReflectedImage() {

float opacity = 0.4f;


float fadeHeight = 0.3f;

refImage = new BufferedImage(img_w, img_h,


BufferedImage.TYPE_INT_ARGB);
Graphics2D rg = refImage.createGraphics();
rg.drawImage(image, 0, 0, null);
rg.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
rg.setPaint(new GradientPaint(0, img_h * fadeHeight,
new Color(0.0f, 0.0f, 0.0f, 0.0f), 0, img_h,
new Color(0.0f, 0.0f, 0.0f, opacity)));

zetcode.com/gfx/java2d/java2dimages/ 12/15
26/9/2019 Imágenes en Java 2D

rg.fillRect(0, 0, img_w, img_h);


rg.dispose();
}

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g.create();

int win_w = getWidth();


int win_h = getHeight();

int gap = 20;

g2d.setPaint(new GradientPaint(0, 0, Color.black, 0,


win_h, Color.darkGray));
g2d.fillRect(0, 0, win_w, win_h);
g2d.translate((win_w - img_w) / 2, win_h / 2 - img_h);
g2d.drawImage(image, 0, 0, null);
g2d.translate(0, 2 * img_h + gap);
g2d.scale(1, -1);

g2d.drawImage(refImage, 0, 0, null);

g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}

@Override
public Dimension getPreferredSize() {

return new Dimension(img_w + 2 * SPACE, 2 * img_h + 3 * SPACE);


}
}

public class ReflectionEx extends JFrame {

public ReflectionEx() {

initUI();
}

private void initUI() {

add(new Surface());
pack();

setTitle("Reflection");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

zetcode.com/gfx/java2d/java2dimages/ 13/15
26/9/2019 Imágenes en Java 2D

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

ReflectionEx ex = new ReflectionEx();


ex.setVisible(true);
}
});
}
}

In the example, we create an illusion of a reflected image.

refImage = new BufferedImage(img_w, img_h,


BufferedImage.TYPE_INT_ARGB);
Graphics2D rg = refImage.createGraphics();
rg.drawImage(image, 0, 0, null);

A copy of a loaded image is created.

rg.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
rg.setPaint(new GradientPaint(0, img_h * fadeHeight,
new Color(0.0f, 0.0f, 0.0f, 0.0f), 0, img_h,
new Color(0.0f, 0.0f, 0.0f, opacity)));

rg.fillRect(0, 0, img_w, img_h);

This is the most important part of the code. We make the second image transparent. But the transparency i
not constant; the image gradually fades out. This is achieved with the GradientPaint class.

g2d.setPaint(new GradientPaint(0, 0, Color.black, 0,


win_h, Color.darkGray));
g2d.fillRect(0, 0, win_w, win_h);

The background of the window is filled with a gradient paint. The paint is a smooth blending from black to
dark gray.

g2d.translate((win_w - img_w) / 2, win_h / 2 - img_h);


g2d.drawImage(image, 0, 0, null);

The normal image is moved to the center of the window and drawn.

g2d.translate(0, 2 * imageHeight + gap);


g2d.scale(1, -1);

Este código voltea la imagen y la traduce debajo de la imagen original. La operación de traducción es
necesaria porque la operación de escalado pone la imagen al revés y traduce la imagen hacia arriba. Para

zetcode.com/gfx/java2d/java2dimages/ 14/15
26/9/2019 Imágenes en Java 2D

entender lo que sucede, simplemente tome una fotografía y colóquela sobre la mesa y gírela.

g2d.drawImage (refImage, 0, 0, nulo);

Se dibuja la imagen reflejada.

@Anular
Dimensión pública getPreferredSize () {

devolver una nueva dimensión (img_w + 2 * SPACE, 2 * img_h + 3 * SPACE);


}

Otra forma de establecer un tamaño preferido para un componente es anular el


getPreferredSize()método.

Figura: Reflexión

En esta parte del tutorial de Java2D hemos trabajado con imágenes.

Inicio Contenido Principio de la página Anterior Siguient


ZetCode modificado por última vez el 18 de abril de 2015 © 2007 - 2019 Jan Bodnar Seguir en Facebook

zetcode.com/gfx/java2d/java2dimages/ 15/15

Potrebbero piacerti anche