Java Tutorial: Graphics

Java Tutorial: Working with Files and Directories in Java

Recap & Sorting the Files and Directories
Opening and Closing the Folders
A Solution Using JavaScript


To make a nice, graphical view we’ll need some graphics elements    to put inside <img> HTML-tags. It’s like collecting a jig saw puzzle. If you look at figure 1 then you’ll see that we only need these 6 pieces:

open folderclosed folderfile

I have made two directory images – since we’ll soon implement an open/close-directory function in our application. As you can see I’ve dropped the plus/minus-signs on the directory-pictures to simplify things a bit.

The pictures are made with a simple graphics utility (like Paint Shop Pro) using a few screen dumps with FrontPage pages. Be prepared to use some time to get the size of the pictures right — if you don’t simply use mine from the zip-file. Now it’s simply a matter of putting these pieces together to make a directory structure. To show you how it works let’s create figure 1 (without the text). The border of each picture has been made visible to show how it’s done:

To make the graphics view we’ll have to code a new version of MyFileStructure – the class that builds the output. A couple of notes about how this is done:

  1. the “level”, which is one of the parameters to the methods in MyFileStructure, is used to determine how many of the vertical lines to place first. If level=4 then you get this result:
  2. in front of a file or folder we must place an “e-bar” or “t-bar”. The “e-bar” is used if the file or folder is the last in its directory
  3. the very first folder doesn’t have any bar in front of it.
  4. to determine if one uses an open or closed folder we must add a new parameter to the methods in MyFileStructure.

The “Explorer” class

The new class — called MyFileStructureExplorer (since it has a Windows File Explorer look) — may be coded like this: