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 ] ( 621 views )   |  permalink
New Product: Fiark FTT 

Geeksomnia


What do you do when you have too much energy to get to sleep?

Some count sheep, some program themselves with T.V, some walk their neighbours cat ...

Others write software.

Fiark


This nights meanderings witnessed the creation of Fiark, a 'niftly (yes, I used a 50ies-ism) little 'ware designed to "float" my files between Ubuntu, Centos, MS Windows, OS/X... AnYwhErE a REAL JVM can be 'empestered.

Fiark Features


- Minimalist Design
- Remembers Settings
- Fast Backup
- Faster Restore
- Fully Threaded



Fiark will:


- Preserve file types, dates, and times.
- Skip over Symbolic Links!
- "Float" files between OS/X <=> Windows <=> Linux
- Support ALL Java platforms! (OS/X, Ubuntu, MS Windows, ... & more!)

Fiark Notes


- Other tools (Zip, Tar, gzip) NOT required!
- Archive saved in standard ZIP Format
- Requires Java 1.7, or greater

Bottom Line


If you - like myself - have had quite enough managing date & time attributes (etc) while crafting querky-little scripts using (1) any number of mismatched tools (XCOPY, TAR, gzip ...!) across outright hostile (2) file systems (Ext4, ExFAT, FAT/DOS, NTFS, (etc, etc, etc,) then (3) Fiark was written for you, too!


Enjoy the Journey,

-Rn



[ view entry ] ( 840 views )   |  permalink  |  related link
DIY Soldering Pencil Replacement Tip  
When it came time to replace the COPPER heat-emitter for my thermal-transfer light sabre, I was dismayed to discover that they were nowhere to be found!

Locating a reasonable IRON tip online, I was just as disappointed that each tip cost approximately 50% of simply purchasing another 'blade.

Yet the soldering iron is the soul of the geek-guy: Why toss a perfectly operational unit into the ocean, when we can DIY?



Measuring the diameter of the old emitter, I quickly found a coated iron nail in the shop. Not only did the diameters match, but the shank of the nail (16 penny) was long enough to allow for an extraction of the desired length.

After amputation, the skill drill allowed for the tip to be both honed & buffed to a customized solder-tinning point.



Opting to shock the coating off (heat the tip up in the iron, then screw-it loose into a bucket of ice water) in the man-cave before entering into the air-conditioned robotics room also proved to be a good idea. (You can also just use an uncoated iron nail or rod to start with.)



Of course I also used the above sharpening process to re-condition my older emitter, as well. When time permits, I plan to visit the geek-guy temple so as to requestion a thermally-superior copper rod so as to use these forces (Yo! ... Da?) to calmly fashion a similar lattice.



Before dropping the heated tip into the water, a few shots of WD-40 at thermal-maximum may even provide a reasonable patina.

Total cost for a new tip? About 2 - 5 cents per "crystal" (We support lots of Eagle Scout Projects. When required, we buy project-fasteners by the gross.)


Sharing is caring!

-Rn


[ view entry ] ( 980 views )   |  permalink

<<First <Back | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Next> Last>>