Site Sponsors:
"An Interview with a Saint" 
Didja know that some readers tell me that I have penned a classic?

No - the book is not on UML, SQL, Java, .NET, LAMP, or C/C++. If it were, I would not be so willing to practically give the title away. :-)



Formerly selling for $14.98, we just cut the paperback price by more than 50%.

For a limited time (just a few more days), the kindle edition is only $0.99.


Sharing is caring!

-Rn

[ view entry ] ( 583 views )   |  permalink  |  related link
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 ] ( 590 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 ] ( 796 views )   |  permalink  |  related link

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