This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am trying to write a program for school that asks the user to enter "USA" to paint the American Flag (that applet already works) or "Germany" to paint the German Flag (this applet also already works). The problem I am running in to is how to call the two other paint classes. When I compile the program the input box comes up and asks me to Enter USA or Germany and the if statement works (I tested it with a simple text output) but I don't know how to call the other two "FlagMaker classes". FlagMaker 2 draws the German Flag and FlagMaker3 draws the American Flag. Here is the code I have so far:
Why are you using the Applet class rather than JApplet? Why have you imported java.io.* which you appear not to be using? Why are you not importing the individual classes?
You ought not to use different classes for the different flags. You ought to use different instances. You could have a FlagDisplay class, and the FlagDisplay class has a List (or array) of Shape objects. Shape can be an abstract class or an interface, and its can have subtypes called Stripe Rectangle Star etc. Each concrete Shape instance has instructions about how to paint itself, size, location, colour etc.
You can call the paint methods from inside the FlagDisplay class.
You can get the FlagDisplay objects with those strings from a Map. Much better than a block of of-elses.
My, you can learn a lot if you do this project correctly . . . or make a right mess of it if you don’t I am not used to using applets, but on a JPanel or similar you would use the paintComponent(Graphics) method, so please confirm that paint(g) is correct.
Now you are not going to do all that at once. Nor should you. Your inclination will be to write a FlagDisplay class and try to get that working, but that is the wrong way to do it. What you want to do is to start from the other end. Get the Shape type working, then get its subtypes working, one by one. Then try putting them together to form a whole flag. I suggest you create a display to test those things on, with a JPanel on a JFrame. Then you can paint the individual Shape instances and see what they look like.
Joined: Jan 20, 2012
Let me try to give you more information. My teacher had us first write the "German Flag" program as a Java Application a couple weeks ago. Then this week he had us convert the "German Flag" Application into a Java Applet. Then he had us write an applet just like the German Flag but to display the "Simplified US Flag". The next part of the lab says this:
"You now have an applet that draws a German Flag and an applet that draws a USA Flag. For part 4 of today's lab, you are to combine both applets into one applet called FlagMaker4.java that can draw both flags. The applet prompts a user to pick a flag using a prompt similar to: "Enter 'USA' for US Flag or 'Germany' for German Flag." The following Java code can be used to display this prompt and choose between the two flags:
response = JOptionPane.showInputDialog("Enter 'USA' for US Flag or 'Germany' for German Flag.");
Now I have already figured out that declaring "response" as String separately like he says did not work and I fixed that issue. I also realized that in the if statement it has to be (response.equalsIgnoreCase("USA"); I just can't figure out what to put in the if statement to call the other applets.
To answer your questions, I am using the applet class because that is what he told us and I am not at all familiar with the classes yet as this is our second week in the class. I am imported java.io.* because I was researching how to get this resolved online myself before posting on here and must have forgot to take it out when the suggestion didn't work. How would I import the individual classes? Are you saying import the exact class instead of using the "*"? Like I think I said in my first post I am new to Java. I took a Java class a long time ago but never really kept up on it and have never messed with applets.
Jay Mackie wrote:I just can't figure out what to put in the if statement to call the other applets.
You don't put anything in the if statement to call the other applets. As it says in the requirements
Jay Mackie wrote:For part 4 of today's lab, you are to combine both applets into one applet called FlagMaker4.java that can draw both flags.
So all the code for drawing both flags needs to be in your new applet.
Joined: Oct 13, 2005
If you had had a proper object-oriented design for your application at first, you wouldn’t be suffering such problems now. You would have created a Flag object which draws Germany, and you would have found it easy to replace that with a Flag object representing the States.
You can import individual classes; I think you are using Color and Graphics from AWT:You didn’t say you were new to Java, and you didn’t post on the beginning forum, so I thought you had some experience.
Just saying, why would you have two different applets for two functions that are more or less the same thing? Just make an if statement, right? No need to have separate "Germny" and "USA" applets.
Joined: Jan 20, 2012
Thanks for all the help...I took it all into consideration and now have an odd issue. I got it all to work but when I type either 'USA' or 'Germany' the dialog box pops up twice asking the same question and on the second time it will actually draw the flag. If I enter something other than one of the two words it just exits like it is supposed to. Here is my code:
any suggestions? thanks!
Joined: Jan 20, 2012
Actually I just figured it out. I just changed the code to:
if anyone could explain to me why moving the joptionpane line of code out of the paint class would fix my issue, that would be helpful tho! Thanks again to everyone that helped me!