Thursday, September 30, 2010

Bezier Curves as Ellipses

A couple weeks ago, I was exploring the source code for the Java runtime classes, trying to figure out exactly how Java draws a circle, and was surprised at what I found. A circle can be defined as an instance of the java.awt.geom.Ellipse2D class with equal width and height. Graphics2D attempts to draw the Ellipse2D shape by calling for its PathIterator. A PathIterator returns segments of the shape in the form of SEG_MOVETO, SEG_LINETO, SEG_QUADTO (quadratic Bezier curve), SEG_CUBICTO (cubic Bezier curve), or SEG_CLOSE. An Ellipse2D contains an initial MOVETO, four CUBICTO segments, and a final CLOSE.

I eventually got my head around what that meant and its implication: The standard 2D graphics library doesn't concern itself with center points and radii, or foci, or pi. In fact, it doesn't even render a real circle, but a very close approximation. It renders four cubic Bezier curves representing 90 degree arcs of the circle. I found this discovery strange at the time, but on reflection it made sense.

Tuesday, September 28, 2010

Form validation using custom attributes


A couple months ago, a friend's daughter came to me for help with a website she was writing for a diabetes seminar registration page. The input form needed to ask the user if they had diabetes, and if so, whether it was type 1 or type 2. She was using Dreamweaver, and was having difficulty getting the form to behave correctly with Spry Widgets form validation.

I wasn't familiar with Spry Widgets (or Dreamweaver, for that matter), so I asked her to show me the source code. We hacked out a semi-functional solution in a few minutes, but I wasn't very happy with it. Spry had input validation that intercepted form submit events. You defined some JavaScript objects declaring what input elements were required, and what format they had to be in (e.g., the input must look like a phone number, or it must look like an email address). When you submitted the form, anything that was wrong gets highlighted red, and you have to go back and correct it.

The definition syntax for a single field looked like this:

Monday, September 27, 2010

Building a Java Applet Sudoku Bot

(Warning: huge and crazy, with no practical target audience that I can imagine.)


I've been interested in the game of Sudoku since it became popular a few years ago, and have often daydreamed about what approach one could take to programmatically solve a puzzle. Although I'm not an expert player, when I play I find that I see complex hints about what numbers *must* go here, or *can not* go there. Explaining to another person the logic of each choice would be fairly simple, but coding a straightforward approach for a computer to do the same thing had, until recently, eluded me. I have now successfully licked that problem, and have written a moderately functional solver applet in Java.

This entry takes you through the start-to-finish process of the applet's creation. I decided from the outset not to make this a Swing applet, and to manage event handling and painting directly.

Sunday, September 26, 2010

Java Swing applet to calculate Fibonacci numbers

I understand this is a common skill builder in Java courses. To write this from scratch, you need to understand Swing input fields, layout management, action listeners, error trapping, and a little math. It also helps if you already think the Fibonacci sequence is cool.

So to help you seekers of assignment solutions, here you go. Study it, though, rather than just copying it wholesale. Maybe I'm not making this as streamlined as I should, or maybe the xor business is too much trouble for the modest speed and memory boost.

First, the working applet:

Saturday, September 25, 2010

Concise Perl

One of my favorite tech blogs is the relatively new Ksplice Software Blog, recently made popular with its unique solution to the recent Linux kernel exploit. In late May they posted an entry called The top 10 tricks of Perl one-liners, which brought back a slew of memories from several years ago when I was doing lots of system admin work from a Unix shell prompt, and also taught me a regular expression switch I had never come across before: \K. Commandline junkies being who they are, there was also ample discussion in the comments on everyone's favorite trick or idiom.

A few days after reading the article, I brought my eldest daughter, then 13, to work with me. She recently started high school, and was hanging out with me in the office completing a summer book summary assignment for an honors class due the first day of school. "Dad, what does 'concise' mean?" she asked me, while looking at the instructions. I replied something to the effect that it meant expressing a lot of detail in as few words as possible. The exchange reminded me of the above blog article, and also of some perl mojo I whipped up a couple weeks earlier at work.


Monday, September 20, 2010

Detecting when a mouse cursor is near a line

Warning, this post contains math!

First, I'm abandoning all of my "recreational" coding projects save one: a java applet for my wife to design clothes. Building a CAD system from the ground up is painful and slow, and I can't tinker any further with other fun things without sapping creative energy I need for my day job.

In the current early stages of this project, I've been thinking a lot about design, and what I do and don't like about similar programs. One of the things I don't like about simple CAD-like tools is their approach to manipulating existing lines. The line's endpoints are draggable, and so is a single point in the middle, but the line itself is not. Take this screenshot of Visio, for example:

Thursday, September 16, 2010

Quick experiment with Java 2D, JComponents, and MouseListeners

Here's something I've been toying around with for a couple days as, of all things, a relaxation experiment. Basically when a shape is hollow, you can drag it to change its size. When a shape is solid, dragging positions it. Clicking on a shape without dragging flips it from hollow to solid, and sets its z-order to the top.


Thursday, September 09, 2010

Labor Day adventure weekend, and the death of a friend

My wife is a saint. If you have any sort of relationship with Liberty, that may not be the first descriptor that jumps to mind, but I assure you it's true. In fact, there was a time when I believed her assertion that she has no empathy, but now I know that for what it really is: a complete ruse. It's her version of my blank expression that I meet most people with, the thing that's a little off-putting and helps me extricate myself from conversations, the thing that lets me keep my distance instead of getting too involved, and has nothing to do with what's actually going on upstairs.