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:
- private static final double CENTRE_TARGET = 48
- private double startX;
- private double startY:
- startX = getWidth()/2;
- startX = getheight()/2;
- double x = CENTRE_TARGET;
- double y = CENTRE_TARGET;
- 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.
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.
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!
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.
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…
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.
Creating a Java Applet
Once I have finished creating a java program , I can either play it as a stand alone program or I could insert it into a webpage. Running a program through a webpage needs certain permissions to be set and it also needs the java prgam ported into an applet. This explains how to do this: https://see.stanford.edu/Course/CS106A/176 video explains things at 0:25:42