0% found this document useful (0 votes)
49 views5 pages

Java 2D Ok

The document provides two examples of using Java 2D graphics to display images and text. The first example demonstrates using local fonts, and the second shows tiling an image across a polygon shape. It also includes an ImageUtilities class that simplifies loading images and waiting for them to load using a MediaTracker.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views5 pages

Java 2D Ok

The document provides two examples of using Java 2D graphics to display images and text. The first example demonstrates using local fonts, and the second shows tiling an image across a polygon shape. It also includes an ImageUtilities class that simplifies loading images and waiting for them to load using a MediaTracker.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

APELLIDOS Y NOMBRES: Javier Edson Candiotti Curi

CURSO: Tecnología Multimedia y Realidad Virtual

==============================================

JAVA 2D
Obtenga la siguiente imagen y texto mostrada en la figura, usado Java 2D.
Envía tu archivo a través de este medio.

Desarrollo:
Ejemplo 1
import java.awt.*;

/** An example of using local fonts with Java2D in Java 1.2.


*
* From tutorial on learning Java2D at
* http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html
*
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/

public class FontExample extends GradientPaintExample {


public FontExample() {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
env.getAvailableFontFamilyNames();
setFont(new Font("Goudy Handtooled BT", Font.PLAIN, 100));
}

protected void drawBigString(Graphics2D g2d) {


g2d.setPaint(Color.black);
g2d.drawString("Java 2D", 25, 215);
}

public void paintComponent(Graphics g) {


clear(g);
Graphics2D g2d = (Graphics2D)g;
drawGradientCircle(g2d);
drawBigString(g2d);
}
public static void main(String[] args) {
WindowUtilities.openInJFrame(new FontExample(), 380, 400);
}
Ejemplo 2
TiledImages.java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

/** An example of using TexturePaint to fill objects with tiled


* images. Uses the getBufferedImage method of ImageUtilities to
* load an Image from a file and turn that into a BufferedImage.
*
* From tutorial on learning Java2D at
* http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html
*
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/

public class TiledImages extends JPanel {


private String dir = System.getProperty("user.dir");
private String imageFile1 = dir + "/images/marty.jpg";
private TexturePaint imagePaint1;
private Rectangle imageRect;
private String imageFile2 = dir + "/images/bluedrop.gif";
private TexturePaint imagePaint2;
private int[] xPoints = { 30, 700, 400 };
private int[] yPoints = { 30, 30, 600 };
private Polygon imageTriangle = new Polygon(xPoints, yPoints, 3);

public TiledImages() {
BufferedImage image =
ImageUtilities.getBufferedImage(imageFile1, this);
imageRect =
new Rectangle(235, 70, image.getWidth(), image.getHeight());
imagePaint1 =
new TexturePaint(image, imageRect);
image = ImageUtilities.getBufferedImage(imageFile2, this);
imagePaint2 =
new TexturePaint(image, new Rectangle(0, 0, 32, 32));
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(imagePaint2);
g2d.fill(imageTriangle);
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(5));
g2d.draw(imageTriangle);
g2d.setPaint(imagePaint1);
g2d.fill(imageRect);
g2d.setPaint(Color.black);
g2d.draw(imageRect);
}
public static void main(String[] args) {
WindowUtilities.openInJFrame(new TiledImages(), 750, 650);
}
}

ImageUtilities.java
import java.awt.*;
import java.awt.image.*;

/** A class that simplifies a few common image operations, in


* particular creating a BufferedImage from an image file, and
* using MediaTracker to wait until an image or several images are
* done loading.
*
* From tutorial on learning Java2D at
* http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html
*
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/

public class ImageUtilities {

/** Create Image from a file, then turn that into a BufferedImage.
*/

public static BufferedImage getBufferedImage(String imageFile,


Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
BufferedImage bufferedImage =
new BufferedImage(image.getWidth(c), image.getHeight(c),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, c);
return(bufferedImage);
}

/** Take an Image associated with a file, and wait until it is


* done loading. Just a simple application of MediaTracker.
* If you are loading multiple images, don't use this
* consecutive times; instead use the version that takes
* an array of images.
*/

public static boolean waitForImage(Image image, Component c) {


MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}

/** Take some Images associated with files, and wait until they
* are done loading. Just a simple application of MediaTracker.
*/
public static boolean waitForImages(Image[] images, Component c) {
MediaTracker tracker = new MediaTracker(c);
for(int i=0; i<images.length; i++)
tracker.addImage(images[i], 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}
}

You might also like