Site Sponsors:
Color Enforcement in Swing 

Santa's Clothes


When a local charity asks one to be Santa Clause for the season, the first thing to do is to consider the audience.

In as much as this charity's audience consisted some affluent, yet also some very poor children, it was for more reasons than for my geeky-girth that we decided that we might fit the fuzzy-red suit rather nicely.

Santa's Certificate


Once folks learned that we were indeed GUI-enabled however, thoughts turned to how we might warp our talents into creating something fun; A gift that might make a kid's season a little brighter:



After passing the hat, the idea was to gift each happy-lap-sitter a certificate; A smiling-enhancing present; -Something charity-affordable (read f-r-e-e) that a child might brandish to tell the world how nice they were trying to be.

Santa Tech


So in addition to donning the suit, we put-on a little Java for the season. While we could have used C#, I wanted to ensure that kids everywhere had a chance to get a certificate from elves welding as many different types of computers, as possible.

One thing leading to another (as they usually do) we also decided that since not all Elves like the same colors, that we needed a quick way to blanket an entire GUI with the same font, color, background, and foreground attributes.


package com.soft9000.gui;

import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;

/**
* An opportunity to demonstrate a way to recursively impose an arbitrary set of
* attributes on a Component/JComponent Hierarchy.
*
* @author profnagy
*/
public class StyleMaster {

/**
* Enforce the style on a Container.
*
* @param ref A Container - Like JFrame - While not Components, they have them :)
* @param example The Component to use for the change.
*/
public static void Enforce(Container ref, Component example) {
if (ref == null || example == null) {
return;
}
ref.setForeground(example.getForeground());
ref.setBackground(example.getBackground());
ref.setFont(example.getFont());
Enforce(ref.getComponents(), example);
}

/**
* Enforce the example style on a JComponent and any JComponent Children.
*
* @param ref The Component to change.
* @param example The Component to use for the change.
*/
public static void Enforce(JComponent ref, Component example) {
if (ref == null || example == null) {
return;
}
Enforce((Component) ref, example);
Enforce(ref.getComponents(), example);
}

/**
* Enforce the example style on a Component array and any JComponent Children.
*
* @param ref The Component to change.
* @param example The Component to use for the change.
*/
public static void Enforce(Component[] ref, Component example) {
if (ref == null || example == null) {
return;
}
for (Component comp : ref) {
try {
JComponent jcomp = (JComponent) comp;
Enforce(jcomp, example);
} catch (ClassCastException ex2) {
Enforce(comp, example);
}
}
}

/**
* Enforce the example style on a Component. (Everything gets here eventually.)
*
* @param ref The Component to change.
* @param example The Component to use for the change.
*/
public static void Enforce(Component ref, Component example) {
if (ref == null) {
return;
}
ref.setFont(example.getFont());
ref.setForeground(example.getForeground());
ref.setBackground(example.getBackground());
}
}

Since I was using Swing, the task was easy enough:

/** Creates new form jpNaughtyNice01 */
public jpNaughtyNice01(JFrame parent) {
this.parent = parent;
initComponents();
jbMasterElf.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
jbMasterElf.setForeground(this.jlblBrowser.getForeground());
jbMasterElf.setBackground(this.jpElfMode.getBackground());

StyleMaster.Enforce(this, jbMasterElf);
}

Our Christmas Present to aspiring Java GUI Guru's everywhere is to share with you how we were able to do that quickly, easily, and efficiently.

JComponent


While just about everything can be updated, note that the inability to set the tab-color of a JTabbedPane is well known:



What is sage, however, was to demo what setting the background color of a combo box did. By keeping the selection from looking like a JTextEdit, no matter what color we use the default dithering always seems to look rather nice:



By factoring in the foreground-color, even the blending of a button-background looks 'kewel, too.

But Wait - There's More!


(-I've always wanted to 'blog that) :)

But here is the real bonus - in as much as everything from JOptionPane to JFileChooser is a JComponent, you can use StyleMaster to make even the standard components match. So when it come time to use another component:


private void jbLocateCertActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser ref = new JFileChooser();
StyleMaster.Enforce(ref, this.jbMasterElf);
ref.showOpenDialog(parent);
}


-The above single line of code will Enforce() a nice change on just about anything 'Swing:





You've just gotta love recursion: Well done, Oracle/Sun!


--See you at the mall,

-R.A.Nagy


[ view entry ] ( 2525 views )   |  permalink  |  related link
Angry Buddha Springs? 

Loco Motion


Well - true to form - yet another game is sweeping the blogs today. We see it on FaceBook. We see it on Android. We see it on T-shirts in Wal*Mart.

-What was even all the buzz at summer camp this year? Angry Birds, of course!

One of the fun things about AB is the way that objects can bounce, rock, and wobble after they are hit .... even well after they are hit!

How did they make all of that happen in C++? With physics, of course!

Box2D


Since we have been ''into'' creating rich media applications of late, we decided to look into the actual software behind those 'wobbles in AB. Known as Box2D, in addition to C++, we were surprised to find portings of the Framework to Adobe Flash & Java.

NetBeans


After working the C++ download for awhile, we decided to give the Java version of Box2D a spin (I have someone else working on Flash.) We have pretty much abandoned Eclipse for reasons of code-loss (i.e. far too many files lost due to 'Workspace Issues.') -Fortunately, we have never lost so much as a semicolon with NetBeans.

1 + 1 = 11


Combining the two together, here is all we have to do:

1.) Get a copy of the SVN.

2.) Create a Maven Project on NetBeans.


3.) Point to the "Existing Code Base."


4.) Press "Finish"


While SVN will take a few moments to do it's thing(s), all works extremely well under Ubuntu. -Thereafter, moving it over to the Windows 2000 VM was a slice 'o cake, too.

RIA - Not?


While the documentation warns that the reference implementation for the Java rendering uses Java2D, it seems fine for appreciating the design and developer's use of the Box2D Framework. Gabe also had a great little snippet to share, too.

Enjoy!

-Rn

p.s. What is with the "Buddha Springs" you might ask? Well, it is part of the excellent theory of operations PDF, as offered by Master Erin Catto.

[ view entry ] ( 9296 views )   |  permalink  |  related link
The Neat Odd-Job Namesapce for C++ 
Just a quick note to let everyone know that we had a moment to update the STDNOJ Namespace on SourceForge.net.

Because we are working on for-profit commercial software applications once again, we have been switching from Java, back to using C++. Also, since we have recently switched from Windows to Linux, you can expect that the non-core classes (like StdSocket, etc.) will shortly be just as good as they are under Windows.

We continue to target Windows, OS X, Android and Linux.

Work has also resumed on the C++ Training.

Enjoy,

-Rn


[ view entry ] ( 1558 views )   |  permalink

<<First <Back | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Next> Last>>