Posts Tagged ‘java’

Java – diving in

6 March 2014

What it takes to write a program – briefly?

Back when I was using DOS [the Disk Operating system sold with most PCs (personal computers)], there was a program included called QBasic. You ran the program and there was a window (a DOS window) where you could write your own little programs. Then you would run them from inside the QBasic program.

This was a very simple functionality for writing software. However, we still follow this fundamental pattern. The program you write software in is now called an IDE – Integrated Development Environment. You develop software by writing and editing code. An average biggish program will make use of many code libraries. These all have to be in places where the IDE can find them. As the functions implemented by your application become more complex, the connections between the various different parts become more numerous.

What does it take to write a program? I takes an IDE, or something like one.

Java

The Java language and approach was created in the 1990s by a computer scientist at Sun (Stanford University Network) Microsystems. By 2007 it had become completely open-source (all code freely available to the public). In 2009 Oracle, a huge software company, bought Sun with the promise to keep Java open source.

The Java approach was to create a language that would be the basis for programs that could run on any operating system. This language is VERY widely used. Its users include huge companies like Boeing, governments, the military, robotics competitions and programming hobbyists.

NetBeans

NetBeans is Oracle’s IDE for Java. It started as a student programming project in Prague (a beautiful city in central Europe). It was purchased by Sun in 1999, and was made open-source soon after that.

One reason it is called NetBeans is because Java is very widely used to create applications that run over networks. Network applications are more complicated than desktop applications because they require communication between computers and sharing resources among multiple users. Of course, Java can also be use to create desktop applications.

Here is a screenshot of a sample NetBeans project:

netbeans example

This shows two views of the same project. One view shows the actual folder structure of the project, and the other shows the project in a way that should assist a developer to work on it.

Note that the number of different parts that make up this “simple” project is quite large. This is rather daunting for the beginner!

What I wanted to do

Years ago I learned that a game controller, usually called a “joystick” after the control stick used in helicopters and jets (a rather base play on words – common among pilots), was extremely simple to make. It seemed like an ideal way to add some hardware knobs to any sort of control application. I wanted to have a piece of code that would allow me to use human-controlled joystick positions in my software applications.

In the “old days” of DOS, this wasn’t too hard to do. You could write commands that took data directly from the computer’s hardware interface.

But “modern” computers, in an attempt to become much more versatile, no longer access hardware directly, but communicate to the hardware through “drivers” that are little pieces of software that make some particular piece of hardware look to the operating system like a generalized, or generic, piece of hardware. So the operating system only has to worry about “printers” and “keyboards” and “game controllers” instead of all the different specific models that exist. And the hardware manufacturer is responsible for providing drivers for different operating systems that they want their hardware to work with.

Windows, in particular, has been through a lot of changes in how its hardware drivers work. This is partly because it is trying to anticipate future hardware innovations and make operating systems that will be compatible with them.

Thus, talking to an old-fashioned DOS joystick on a Windows computer is now basically impossible. You need an intermediate piece of hardware (thankfully not too expensive) to make the “legacy” joystick look like a USB game controller. Then you can choose one of several ways of talking to the USB game controller — your joystick.

I was not getting anywhere finding code examples that worked in my Microsoft IDE (Visual Studio – I learned to use it at Seattle Central Community College), so I thought I might try Java.

My first choice for a Java IDE was Eclispe. This is an open-source IDE developed by IBM. It is about 10 years younger than Java and is in fact written in Java. I had heard good things about it including the fact that it is very versatile through the use of feature “plug-ins.”

Here is a screenshot of just a portion of the application. It is really very similar to other IDEs. However, the best code examples I could find were developed using NetBeans. Each IDE uses its own internal folder structure. So you can’t really take a NetBeans Java project and just copy it over to Eclipse and have it work fine.

eclipse-example

Success!

If you have a USB joystick, on Windows 7 you can go to the Control Panel, choose Devices and Printers, and you should find listed your game controller (if it’s plugged in). You can right click it, select “…settings” and then click the Properties button. It should bring up a little window that graphically shows the joystick, sliders and buttons on the game controller, and the graphic will change as you move the controls or press the buttons. This is exactly what the code I found implements using Java. It’s a very interesting piece of code, and it is comprised of (count them!) three major parts and about 15 different functions. That doesn’t count all the stuff that Java takes care of for you. So this function is not as simple as I’d like it to be, but at least I finally have some code I can look at. (This is available in a zip file called JInputJoystickTestV2.zip from http://theuzo007.wordpress.com/2013/10/26/joystick-in-java-with-jinput-v2/ .)

More on project folders

It took me a while to learn to use folders to organize my code projects – even the ones I did without a real IDE (I learned to create PHP applications without using an IDE). I finally decided on a standard folder structure for my PHP projects, and it has helped me to “throw together” an idea, because I can just start by copying a similar project into a new folder.

Below is a little illustration of someone’s website folders. The “eng” stands for “English.” The other folders are similar to the ones I use. One for HTML pages, which may be “static” or contain code in them. One for images. A website can have a LOT of images! One for styles, which are files written in a language called CSS – Cascading Style Sheets – that describe how a website should look, but contain no content. And one for “js” – JavaScripts. JavaScripts are not really related to Java, and are a little controversial. They are basically little programs that download with a web page and run in your browser. They can therefore potentially do bad things to your computer. They are supposed to only do things to your web page, though, and now most browsers are built in a way to help ensure that JavaScripts only operate on the document being displayed. Still, I don’t like to use them. I prefer to rely on built-in browser functionality to do things on a web page.

And with that little digression, goodbye for today!

website-example