Week 2 of Java was not as easy as last week. When I took Java last time I did not have to do any things with GUI. Yesterday, I had to write a program that drew a circle given a user entered radius. I totally get the logic of getting a number from the user and the math of drawing the circle. The piece of code that I couldn’t get was this:
Container pane = theGui.getContentPane(); CircleGuiProf circleGui = new CircleGuiProf(Color.yellow, radius); pane.add(circleGui, BorderLayout.CENTER); theGui.setVisible(true);
While I was sitting there watching it not work, I could feel my blood pressure rising and my face getting red. I will not forget that again. I have never enjoyed art/drawing/coloring and I have been terrible at it since I was a little kid. I am not excited about that carrying over in to programming. It is definitely something I need to work on.
I enjoying writing code and I didn’t want to end the day on a coding downer (plus it would haunt all night). So when I got home, I started in on the homework. This is the problem:
Part 1: Write a program that picks a random number between 1 and 10, and allows the user to guess.
It should tell them if their guess was correct or not, and exit when they get the right answer.
Part 2: If the user guesses incorrectly tell them if they need to go higher, or lower.
My professor said that we didn’t have to use pop-ups if we didn’t want to but after my fist-fight with graphics in class, I wanted to make sure I got the idea of JOptionPane. It took me some time but this is the code I came up with :
It works well, except for when the OK button is pressed with no data in the input box the program blows up. I assumed that would return a null value and I have dealt with that but it still doesn’t work. All you smart programming people reading this please feel free to tell me what I’m doing wrong in the comments.
I had fun coding this thing and it relieved some of my frustration. Now I am off to fix my OK button issue.