Site Sponsors:
Dynamically Captioned Images 
There I was writing a game designer. Suddenly I found myself in need of lots of new images. -Need-em-now pictures to represent game elements, as well as place holders for future graphical artist imagineering.

If you also like to use Java's Swing to create graphical user interfaces, then I'll bet that you have had a similar need.

Rather than spending the rest of the evening 'Gimping out, the need was to simply slap some text over a PNG or three. Once armed with such an 'entexted image, I could then get back to allowing cool coding pastimes to help us pass-the-time.

Here is what I came up with:

public BufferedImage drawTextie(String resource, Point pt, Color color, int fontSize, String caption) {
try {
if (resource == null || point == null || color == null || caption == null) {
return null;
}
URL url = this.getClass().getResource(resource);
if (url == null) {
return null;
}
ImageIcon ic = new javax.swing.ImageIcon(url);
BufferedImage bi = new BufferedImage(ic.getIconWidth(), ic.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D gr = (Graphics2D) bi.getGraphics();
ic.paintIcon(null, gr, 0, 0);
gr.setFont(gr.getFont().deriveFont((float) fontSize));
gr.setColor(color);
gr.drawString(caption, pt.x, pt.y);
return bi;
} catch (Exception ex) {

}
return null;
}


Having so many parameters makes any function massively annoying to use directly. We decided to add some ease-of-use member function support:

package com.soft9000.graphics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.ImageIcon;

/**
* Load an imaged resource, then slap a textual caption over it.
*
* @author profnagy
*/
public class ImagedText {

private Color color = Color.red;
private Point point = new Point(0, 0);
private int FontSize = 18;

private BufferedImage image = null;
private String resource;

public ImagedText(String resource) {
this.resource = resource;
}

public ImagedText(String resource, Point point, int FontSize, Color color) {
this.resource = resource;
setPoint(point);
this.FontSize = FontSize;
setColor(color);
}

public boolean drawString(String caption) {
this.setImage(drawTextie(this.getResource(), this.getPoint(), this.getColor(), this.getFontSize(), caption));
if (this.getImage() == null) {
return false;
}
return true;
}

public boolean drawString(int x, int y, String caption) {
this.point.x = x;
this.point.y = y;
return drawString(caption);
}

public boolean drawString(Point pt, String caption) {
setPoint(point);
return drawString(caption);
}

public boolean drawString(Point pt, String caption, int fontSize) {
setFontSize(fontSize);
setPoint(point);
return drawString(caption);
}

public Color getColor() {
return color;
}

public final void setColor(Color color) {
if (color != null) {
this.color = color;
}
}

public Point getPoint() {
return point;
}

public final void setPoint(Point point) {
if (point != null) {
this.point.x = point.x;
this.point.y = point.y;
}
}

public int getFontSize() {
return FontSize;
}

public void setFontSize(int FontSize) {
this.FontSize = FontSize;
}

public BufferedImage getImage() {
return image;
}

public void setImage(BufferedImage image) {
this.image = image;
}

public String getResource() {
return resource;
}

public void setResource(String resource) {
if (resource != null) {
this.resource = resource;
}
}

public BufferedImage drawTextie(String resource, Point pt, Color color, int fontSize, String caption) {
try {
if (resource == null || point == null || color == null || caption == null) {
return null;
}
URL url = this.getClass().getResource(resource);
if (url == null) {
return null;
}
ImageIcon ic = new javax.swing.ImageIcon(url);
BufferedImage bi = new BufferedImage(ic.getIconWidth(), ic.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D gr = (Graphics2D) bi.getGraphics();
ic.paintIcon(null, gr, 0, 0);
gr.setFont(gr.getFont().deriveFont((float) fontSize));
gr.setColor(color);
gr.drawString(caption, pt.x, pt.y);
return bi;
} catch (Exception ex) {

}
return null;
}
}


Here is an example of how to use it:

ImagedText toolMinor = new ImagedText("/game/images/Img200x200.png");
ImagedText toolMajor = new ImagedText("/game/images/Img200x200.png", new Point(10, 50), 18, Color.yellow);

if (toolMajor.drawString("Major Tool") {
this.lblToolImage.setIcon(new ImageIcon(toolMajor.getImage()));
}


In this application, we chose to show graphics quickly by simply updating an ImageIcon for a JLabel.

Your mileage may vary.



Sharing is caring,


-Rn

[ view entry ] ( 864 views )   |  permalink

<<First <Back | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Next> Last>>