General Programming Notes

What follows in this section is issues I may have had thinking about and designing code and how I went about solving my issue.  There might also be quick reference notes (which I expect will be drowned out by the main content given there’s no index on the page).

Drawing Graphical Elements.

Given I had trouble wrapping my head around drawing rectangles (GRec) and ovals (GOval) in Java, here’s a breakdown.

First off, to place the first pixel, you need to determine the size of the graphics window.

To find the x dimension: getWidth.  Thus the centre is getWidth/2.

To find the y dimensions getHeight.  Thus the centre is getHeight/2.

To draw a circle in the center of the screen, you need to know that the first pixel is placed at the top left of the graphical element, not the center (think you are drawing a square or rectangle and not an oval).  Thus, you need to displace the “center” by half the width of the radius.

An example of this code is:

  1. private static final double CENTRE_TARGET = 48
  2. private double startX;
  3. private double startY:
  4. startX = getWidth()/2;
  5. startX = getheight()/2;
  6. double x = CENTRE_TARGET;
  7. double y = CENTRE_TARGET;
  8. GOval target = new GOval (startX – x, startY – y, 2*x, 2*y);

I’m assigning double to the values in case the outcome of the calculation involves a decimal.  If I used int and the result was 2.1 or 10.9, the result would be 2 or 10 as code truncates (forget about rounding here), and the resultant graphical element might look wonky.  The first x, y coordinate is the start of the circle and the second x, y elements is how big  – the diameter.

What the GOval code translates to is…

“Draw a round graphic element that begins at the x coordinate that is in the center of the screen minus the radius of the circle and where the y coordinate is in the center of the screen minus the radius of the circle and where size of the circle is two times the radius (so the diameter) in both the x and y directions.”

For those who need to see this, I’ve got a couple of drawn diagram which should explain how things are done.

Finds the size and center of the graphics window in Java – 0, 0 is TOP LEFT

Diagram 2 - Draws a circle

Draws a circle in Java

Assigning Variables and Constants

Work out everything, and I mean EVERYTHING that has a number assigned to it and whack it outside the run process.  That way, if a number needs to be changed, the programmer (you or a successor) can change the value once and does not need to scour code to change each and every instance of the reference.

By convention, constants go above the run.  Variables come at the bottom.

02/06/2017 Update

Variables that needs to be passed between methods need to be declared as variables.  Variables that are only used in a method do not need to be declared outside of the method.

Saving Data in Memory to a File

Many of the assignments in CS106A suggested the ability to save data would be a good extension.  I did not add such a feature as I assumed this would be taught later.  It was not.  So, here is how to save files… they way I want to save does not work, but a generic way does.  I’ll need to figure out if I can do it the way I want.

Basically the issue is.  I ask for a file name.  The program accepts it and correctly assigns it to the variable fileName.  I concatenated fileName with .txt.  This is correctly held in memory (the screen shot shows that saveme.txt is assigned to fileName.

If I specify the name of the file to save as “bla de bla”, then a file called “bla de blah” is saved.  If I do not use quotes then the variable for fileName (saveme.txt) is not saved an an IOException is thrown.    Presumably there is a way to specify the file name or else programs would be stuck saving hard-coded file names which is not how they work!

Save Files works in Java , but not the way I want it to work

Save Files works in Java , but not the way I want it to work

Apparently access is denied.  Denied by what?  Not the computer’s file system as we have seen a file is created if the name’s in quotes.  Here I’ve got as far as working out that if I put “blad de bla.txt” as the file name, the access denied error is thrown.  Without an extension, things work.   I have also worked out the code works fine if I leave out the quotes and reference the fileName variable.

So, for the purposes of programming in java, not having a file extension is not going to stuff up any programs as one tells the program what format to expect.  Still, I want file extensions.

The pesky IOException I need to understand

The pesky IOException I need to understand

Some testing shows if I set the extension to be .foo, foobar saves as foobar.foo. File extension .doc works.  Hmm, NFI why txt is a no go.  At least I can give the files my own extensions and go from there.

I posted about this issue on stackoverflow and it seems people do not bother to read the issue or the code and just say “refer to existing answer which is similar but not sufficiently so”.  Not that it’s a pressing issue.  It’s a nice to know in the event I want to specifically save txt files.

For a lark, I also added file read option to my test code and at least that works…

Data from an existing txt file loads correctly

Data from an existing txt file loads correctly

So, the save file problem was an interesting one.  After some additional testing, I discovered and (perhaps hastily) concluded:

  • the code works with Eclipse 3.2.1 and Java 6 Update 2
    the code does not work with: Eclipse 4.6.3, Windows 7 SP 1 and Java 8 (121 64 bit).

The good news is I have taught myself how to save data from a running program.  Despite “solving” the problem, it still bugged me.  A system restore to the older Eclipse and Java on my main testing VM and then upgrading again to the latest Eclipse and Java 8 and the file now saves.  So definitely something to do with the IDE installation.