programming posts

Design Java UI Like Visual Basic

One of the most frustrating and time-consuming tasks in Java is creating basic, boring, ordinary UI.  As in, windows, buttons, scroll bars, text fields, etc.

This is in stark contrast to, say, Visual Basic, where you can just drag and drop elements onto a form, and what you see is what you get.  In Java, if you want anything other than just the basics, you're going to have to do a lot of typing.

That is, unless, you are using the WindowBuilder Engine and Eclipse.

  • In Eclipse (3.7 at time of this writing), go to Help -> Install New Software.
  • Enter this address:
    • http://download.eclipse.org/windowbuilder/WB/integration/3.7
  • Download everything listed, then restart Eclipse:

After this, right-click in your project's src folder to create a new item.  Chose "other", then select the "Application Window" under Window Builder->Swing Designer:

Open the file you've created, and notice something cool-- you now have a "design" tab at the bottom of the code window.  Click it.  You now see a screen like so:


Notice it now looks a great deal like Visual Basic?

LibGDX Keycodes to Strings

If anyone out there is using the wonderful libGDX library for their mobile games, and are using the wonderful Controllers library that goes along with it, perhaps you've run into a problem which I found:  I wanted a way to let the player see the names of they keys they had mapped on the keyboard to different controls.

Well, in order to do that, I needed to know what the English names were associated with they keycodes delivered, for example, to the keyDown method.

So, I made a HashMap for my own uses, and thought I'd put it here.  It isn't complete, but it has most of the keys.  Enjoy!

public static HashMap<Integer, String> keycodeNames 
                          = new HashMap<Integer, String>();
 
  static {
    keycodeNames.put(7, "NUM_0");
    keycodeNames.put(8, "NUM_1");
    keycodeNames.put(9, "NUM_2");
    keycodeNames.put(10, "NUM_3");
    keycodeNames.put(11, "NUM_4");
    keycodeNames.put(12, "NUM_5");
    keycodeNames.put(13, "NUM_6");
    keycodeNames.put(14, "NUM_7");
    keycodeNames.put(15, "NUM_8");
    keycodeNames.put(16, "NUM_9");
    keycodeNames.put(29, "A");
    keycodeNames.put(57, "ALT_LEFT");
    keycodeNames.put(58, "ALT_RIGHT");
    keycodeNames.put(75, "APOSTROPHE");
    keycodeNames.put(77, "@");
    keycodeNames.put(30, "B");
    keycodeNames.put(4, "BACK");
    keycodeNames.put(73, "\\");
    keycodeNames.put(31, "C");
    keycodeNames.put(5, "CALL");
    keycodeNames.put(27, "CAMERA");
    keycodeNames.put(28, "CLEAR");
    keycodeNames.put(55, ",");
    keycodeNames.put(32, "D");
    keycodeNames.put(67, "BACKSPACE");
    keycodeNames.put(112, "FORWARD_DEL");
    keycodeNames.put(23, "CENTER");
    keycodeNames.put(20, "DOWN");
    keycodeNames.put(21, "LEFT");
    keycodeNames.put(22, "RIGHT");
    keycodeNames.put(19, "UP");
    keycodeNames.put(33, "E");
    keycodeNames.put(6, "ENDCALL");
    keycodeNames.put(66, "ENTER");
    keycodeNames.put(65, "ENVELOPE");
    keycodeNames.put(70, "=");
    keycodeNames.put(64, "EXPLORER");
    keycodeNames.put(34, "F");
    keycodeNames.put(80, "FOCUS");
    keycodeNames.put(35, "G");
    keycodeNames.put(68, "GRAVE");
    keycodeNames.put(36, "H");
    keycodeNames.put(79, "HEADSETHOOK");
    keycodeNames.put(3, "HOME");
    keycodeNames.put(37, "I");
    keycodeNames.put(38, "J");
    keycodeNames.put(39, "K");
    keycodeNames.put(40, "L");
    keycodeNames.put(71, "[");
    keycodeNames.put(41, "M");
    keycodeNames.put(90, "FAST_FORWARD");
    keycodeNames.put(87, "NEXT");
    keycodeNames.put(85, "PLAY_PAUSE");
    keycodeNames.put(88, "PREVIOUS");
    keycodeNames.put(89, "REWIND");
    keycodeNames.put(86, "STOP");
    keycodeNames.put(82, "MENU");
    keycodeNames.put(69, "MINUS");
    keycodeNames.put(91, "MUTE");
    keycodeNames.put(42, "N");
    keycodeNames.put(83, "NOTIFICATION");
    keycodeNames.put(78, "NUM");
    keycodeNames.put(43, "O");
    keycodeNames.put(44, "P");
    keycodeNames.put(56, ".");
    keycodeNames.put(81, "+");
    keycodeNames.put(18, "#");
    keycodeNames.put(26, "POWER");
    keycodeNames.put(45, "Q");
    keycodeNames.put(46, "R");
    keycodeNames.put(72, "]");
    keycodeNames.put(47, "S");
    keycodeNames.put(84, "SEARCH");
    keycodeNames.put(74, ";");
    keycodeNames.put(59, "SHIFT_LEFT");
    keycodeNames.put(60, "SHIFT_RIGHT");
    keycodeNames.put(76, "/");
    keycodeNames.put(1, "SOFT_LEFT");
    keycodeNames.put(2, "SOFT_RIGHT");
    keycodeNames.put(62, "SPACE");
    keycodeNames.put(17, "STAR");
    keycodeNames.put(63, "SYM");
    keycodeNames.put(48, "T");
    keycodeNames.put(61, "TAB");
    keycodeNames.put(49, "U");
    keycodeNames.put(0, "UNKNOWN");
    keycodeNames.put(50, "V");
    keycodeNames.put(25, "VOLUME_DOWN");
    keycodeNames.put(24, "VOLUME_UP");
    keycodeNames.put(51, "W");
    keycodeNames.put(52, "X");
    keycodeNames.put(53, "Y");
    keycodeNames.put(54, "Z");
    keycodeNames.put(129, "CONTROL_LEFT");
    keycodeNames.put(130, "CONTROL_RIGHT");
    keycodeNames.put(131, "ESCAPE");
    keycodeNames.put(132, "END");
    keycodeNames.put(133, "INSERT");
    keycodeNames.put(92, "PAGE_UP");
    keycodeNames.put(93, "PAGE_DOWN");
    keycodeNames.put(94, "PICTSYMBOLS");
    keycodeNames.put(95, "SWITCH_CHARSET");
    keycodeNames.put(255, "BUTTON_CIRCLE");
    keycodeNames.put(96, "BUTTON_A");
    keycodeNames.put(97, "BUTTON_B");
    keycodeNames.put(98, "BUTTON_C");
    keycodeNames.put(99, "BUTTON_X");
    keycodeNames.put(100, "BUTTON_Y");
    keycodeNames.put(101, "BUTTON_Z");
    keycodeNames.put(102, "BUTTON_L1");
    keycodeNames.put(103, "BUTTON_R1");
    keycodeNames.put(104, "BUTTON_L2");
    keycodeNames.put(105, "BUTTON_R2");
    keycodeNames.put(106, "BUTTON_THUMBL");
    keycodeNames.put(107, "BUTTON_THUMBR");
    keycodeNames.put(108, "BUTTON_START");
    keycodeNames.put(109, "BUTTON_SELECT");
    keycodeNames.put(110, "BUTTON_MODE");
    keycodeNames.put(144, "NUMPAD_0");
    keycodeNames.put(145, "NUMPAD_1");
    keycodeNames.put(146, "NUMPAD_2");
    keycodeNames.put(147, "NUMPAD_3");
    keycodeNames.put(148, "NUMPAD_4");
    keycodeNames.put(149, "NUMPAD_5");
    keycodeNames.put(150, "NUMPAD_6");
    keycodeNames.put(151, "NUMPAD_7");
    keycodeNames.put(152, "NUMPAD_8");
    keycodeNames.put(153, "NUMPAD_9");
  }

 


Software License Fine Print

This code is considered "open source," and is released under the GNU Public License version 3 or any later version (http://www.gnu.org/copyleft/gpl.html). In a nut shell, you can change this code however you want, and redistribute it, but ONLY if it is ALSO under the GNU Public License version 3 or later.  This code/software is given "as-is", with no warranty or guarantee.

If you use this code/software, I would appreciate credit and a link back to this site (richardpeacock.com), though this is not required.

Dropbox and Eclipse IDE

Even though I have already written about the usefulness of Dropbox for programmers, I'd like to share another tip specifically for the Eclipse IDE, and any IDE based on Eclipse.

You may not have realized it, but Eclipse (and derivatives-- Aptana, Titanium, etc) can be run as a "portable" application.  Meaning, you can just copy the entire Eclipse directory to another computer or, say, a USB drive, and it will run exactly the same, with all of your settings in tact.

This means it is a perfect friend for Dropbox.  I place my Eclipse IDEs in folders under Dropbox, and make sure my Workspace is pointed to a folder within Dropbox, and presto-- my projects, settings, etc, are on all of my computers and ready to go.

And any changes I made to my source code, preferences, layout, etc, are automatically spread to my other computers.

Now, this does require that your Dropbox folder is at the same path location on all of your computers.  But, luckily, Dropbox lets us specify exactly what path to use.  So, for example, you could just make sure that your Dropbox folder is always at C:\dropbox.

 

iOS 6 Autorotate Solution

When Apple moved from iOS 5 to iOS 6, they changed the way developers must handle autorotation in their apps. 

Basically, it isn't too bad, as it should just work automatically.  But, I ended up struggling for hours without success, until I came upon this helpful website with a small, almost throw-away piece of advice:

In your AppDelegate class, in the application: didFinishLaunchingWithOptions method, you might have a line like this:

[window addSubview:viewController.view];

This is especially likely if you are updating an app from iOS 5 or below.

This line will actually prevent autorotation from working in iOS 6!  Change it to this:

window.rootViewController = tabBarController;
// (or set to whatever navController you might be using)

So, to reiterate, I spent hours and hours pulling my hair out, when it turns out I needed to change one line of code, in an area of the app which seems to have nothing to do with autorotation at all.  Thanks Apple!

Java Associative Array (Sort Of)

Attached File(s): 

Ever wish that Java had associative arrays (aka assoc arrays) like in PHP?  As in, $x["name"] = "Bill" ?  Well, I created a helper class for Java which comes close to approximating it.  It can even loop through it similar to foreach().

ipad-java.jpgI call the class PseudoAssocArray, because it actually uses a HashMap to do the behind the scenes work. 

Simply include the file attached at the top of the page in your project, then use it like so:

PseudoAssocArray temp = new PseudoAssocArray(1);
temp.put("name", "April");
temp.put("age", "26");
// ...
String n = temp.get("name");

You can even make it be 2 dimensional by supplying a 2 in the constructor.  For example:

PseudoAssocArray temp = new PseudoAssocArray(2);
temp.put(0, "name", "April");
temp.put(1, "name", "Samuel");
// ...
String n = temp.get("1", "name");

Loop through like so:

temp.resetCounter(); //always do this first
while (temp.hasMore()) {
  Object obj = temp.getNext();
  // or: String s = (String) temp.getNext();
}

To loop through and get both key and value, try this:

temp.resetCounter();
while (temp.hasMore()) {
  Map<String, Object> entry = temp.getNextMap();
  String key = (String) entry.get("key");
  String value = (String) entry.get("value");
}

Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Draw a Box Around a Coordinate In Google Maps Based on Miles or Kilometers

Attached File(s): 

This post explains how, given latitude and longitude coordinates in Google Maps (specifically, in the Static API), how one can draw a bounding box around them, based on a distance in either miles or kilometers.

gmap_bounding.pngLet's say you already know the lat/long of a particular point on the map, and now you want to draw a box around it, or maybe you just need to know the lat/long coordinates of another point, so many km or miles away?  Well, this is the script for you!

Based on a script retrieved from here:
http://www.sitepoint.com/forums/showthread.php?656315-adding-distance-gps-coordinates-get-bounding-box

The trick is that Google Maps wants you to provide it with the lat/long coordinates for each corner of the box.  After several hours of searching, I finally found a script which I was able to modify to give me what I needed.

Simply download the script linked at the top of the page, and then use the following code to create the image you see here. 

Enjoy!


    $lat = -33.872986011907216;
    $lng = 150.90904516601563;

    // Create the static map api image.
    $static_maps_url = "http://maps.googleapis.com/maps/api/staticmap";
    $static_maps_url .= "?center=$lat,$lng";
    $static_maps_url .= "&zoom=15";
    $static_maps_url .= "&size=300x300";
    $static_maps_url .= "&maptype=roadmap";
    $static_maps_url .= "&sensor=false";
    $static_maps_url .= "&markers=color:blue|$lat,$lng";

    // Figure out the corners of a box surrounding our lat/lng.
    $d = 0.3;  // distance
    $path_top_right = bpot_getDueCoords($lat, $lng, 45, $d);
    $path_bottom_right = bpot_getDueCoords($lat, $lng, 135, $d);
    $path_bottom_left = bpot_getDueCoords($lat, $lng, 225, $d);
    $path_top_left = bpot_getDueCoords($lat, $lng, 315, $d);
   
    $static_maps_url .= "&path=color:334433|weight:5|fillcolor:0xFFFF0033|";
    $static_maps_url .= "$path_top_left|$path_top_right|$path_bottom_right|";
    $static_maps_url .= "$path_bottom_left|$path_top_left";
     
    // Now, draw the image from Google Maps API!
    print "<img src='$static_maps_url'>";


 

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Encode Any String to Only Alphanumeric Chars - Better Than URLEncode

Have you ever been faced with a situation where you need to encode a string with all sorts of problem characters into a URL-safe string, but, for whatever reason, urlencode() just won't do the job? 

What you need is a function that will give you nothing but numbers and letters-- no %'s or &'s, spaces, or any other non-alphanumeric character.

Well, look no further that PHP's bin2hex() function, and its pack() function.

It can convert this tricky string:

(!X>4Ob=h/&hN\'

Into this much nicer string, which can easily pass through a URL, MySQL query, XML tags, etc, since it is guaranteed to only ever be letters and numbers:

2821583e344f623d682f26684e5c27

And then decoding is a snap.  Here are the functions you need:

function hex_encode($input) {
  return bin2hex($input);
}

function hex_decode($input) {
  return pack("H*", $input);
}

Enoy!

Android Tip: Preferences - Easier Than Using a Database - With Code Sample!

Attached File(s): 

If you have done any programming for an Android phone, you've probably already used (or at least heard of) Android's built-in database option of SQLite.  SQLite is a fine system, but it can be cumbersome to work with, especially if you only need to store a few values. 

samsung-omnia-hd-android.jpgEnter your new best friend: Preferences.

Think of "Preferences" as a way to save data (like Strings) outside of your application.  It's like a database in that way, but it is much easier to put data in and get data out.  If you build a Preferences Activity at some point, then it will actually be using this very system-- but that's another blog post.

I have included my own class for working with Preferences.  It is linked at the top of the post.  To use it, instantiate a Prefs object like so:

Prefs prefs = new Prefs(context);
// Prefs prefs = new Prefs(this) will work 
// if you are within an Activity

Now, to store a String value, all you have to do is this:

String mUserNameString = "rpeacock";
prefs.setPreference("username", mUserNameString);

To retrieve that value later, even after the app has closed and restarted, all you have to do is this:

String temp = prefs.getPreference("username");

Isn't that easy?  More to the point, isn't that much easier than creating an SQLite object, constructing a query, executing the query, then closing the connection?  That's why I use Preferences everywhere I can.  It's also great for accessing values between Activities, services, etc.

The code I attached only does Strings, but you can easily extend it to work with any value.  Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Dropbox and Programming - A Match Made in Heaven

For those that don't know, Dropbox is a free service which lets you share a folder across multiple computers, even with different OSes like Mac, Windows, and Linux.  It's great for keeping all your personal files with you on both your work and home PC (without the need for a USB drive).  But where it really shines for me is when it comes to programming.

Dropbox-Review-2.jpgProgramming from multiple computers is always a bit of a pain.  What I normally do is I keep all my files on a USB drive, and just have to remember to carry it with me every where I go.  I'm always worried about breaking or losing it, so I have to back it up like crazy.

Another option is to use a version control system like Subversion or Git.  They work (usually), but you've got to remember to commit changes before moving to another computer.  For newbies they can be complicated to set up, and might be overkill for smaller one-man projects. Plus my IDE for Android development, Eclipse, just doesn't want to play nice with Subversion.

Enter Dropbox, my new hero.

Oracle_query() and Other Oracle Convenience Functions

Attached File(s): 

If you're used to using MySQL with PHP, you already know of the simple and easy to use mysql_query(), mysql_fetch_object(), mysql_affected_rows() and so forth functions.  Well, if you've ever dipped your toes into the tepid waters of Oracle, you know that it lacks the friendly PHP functions that MySQL has.  So, I created a set of convenience functions designed to mirror the MySQL ones.

Just download the attached file at the top, and use it like so:

<?php
  
  include_once("oracle_common_functions.php");

  oracle_connect($username, $pass, $host, "TEST");
  $res = oracle_query(" SELECT * FROM $table_name ") or echo(oracle_error());
  while ($cur = oracle_fetch_array($res)) {
    var_dump($cur);
  }  
  
  oracle_close();
  
?>

Just remember that to use any of this, you also need PHP to be compiled with the necessary oci libraries.

Complete list of functions in the file:

oracle_connect()
oracle_query()
oracle_fetch_array()
oracle_fetch_object()
oracle_affected_rows()
oracle_close()
oracle_error()
oracle_errno()
oracle_escape_string()

Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Syndicate content