wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need Help with Multiple Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help with Multiple Classes" Watch "Need Help with Multiple Classes" New topic
Author

Need Help with Multiple Classes

Jay Mackie
Greenhorn

Joined: Jan 20, 2012
Posts: 4
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:



Thanks for your help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Welcome to the Ranch

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.
Jay Mackie
Greenhorn

Joined: Jan 20, 2012
Posts: 4
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:

import javax.swing.*;
String response;
response = JOptionPane.showInputDialog("Enter 'USA' for US Flag or 'Germany' for German Flag.");
if (response.equals("USA"))............else............."



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.

Thanks so much for your response!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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.
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

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.
Jay Mackie
Greenhorn

Joined: Jan 20, 2012
Posts: 4
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!
Jay Mackie
Greenhorn

Joined: Jan 20, 2012
Posts: 4
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!
 
GeeCON Prague 2014
 
subject: Need Help with Multiple Classes