Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: need help w/ entry level homework...

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Red face need help w/ entry level homework...

    I am in an entry level Java class and am supposed to implement 3 classes, ItalianFlag, ItalianFlagViewer and ItalianFlagComponent and use as much object oriented design as possible so one can specify the coordinates x and y and width to draw the Italian flag. I am supposed to test the design by drawing two flags in my viewer.
    My problem is that I have the Italian Flag designed, but cannot get it to appear twice on my viewer. I'll post my codes in hopes someone can point out my flaw. I am actually happy I got this far...

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
     
     
    public class ItalianFlagComponent extends JComponent{
     
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    ItalianFlag flag1 = new ItalianFlag(0,0,0, Color.black);
    ItalianFlag flag2 = new ItalianFlag(200, 200, 200, Color.black);
    flag1.draw(g2); 
    flag2.draw(g2);
    }
    }

    import java.awt.geom.*;
    import java.awt.*;
     
    public class ItalianFlag {
    public int x,y;
    public Color c;
     
    public ItalianFlag(double x, double y, double aWidth) { 
    }
    public ItalianFlag(double x, double y, double aWidth, Color c){
    this.c= c;
    }
    public void draw(Graphics2D g2) {
     
    Rectangle leftRectangle = new Rectangle(x+15, y+10, 45, 45); 
    Rectangle rightRectangle = new Rectangle (x+100, y+10, 45, 45);
    Point2D.Double r1= new Point2D.Double(x+15, y+10);
    Point2D.Double r2= new Point2D.Double(x+100, y+10);
    Line2D.Double top= new Line2D.Double(r1,r2);
    Point2D.Double r3= new Point2D.Double(x+45, y+55);
    Point2D.Double r4= new Point2D.Double(x+115, y+55);
    Line2D.Double bottom= new Line2D.Double(r3,r4);
    g2.setColor(Color.GREEN);
    g2.fill(leftRectangle);
    g2.setColor(Color.RED);
    g2.fill(rightRectangle); 
     
    g2.setColor(c);
    g2.draw(leftRectangle);
    g2.draw(rightRectangle);
    g2.draw(top);
    g2.draw(bottom);
     
    } 
    }


    import javax.swing.*;
    public class ItalianFlagViewer {
     
    	public static void main(String[] args) {
     
    	JFrame frame = new JFrame();
     
    	frame.setSize(300,400);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	ItalianFlagComponent component = new ItalianFlagComponent();
    		frame.add(component);
    		frame.setVisible(true);
    	}
     
     
    }

    I hope that's not too jumbled, I'm not sure how to clearly post codes where they are easily readable.
    Thanks,
    Brandon
    Last edited by rbread80; October 3rd, 2010 at 09:43 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need help w/ entry level homework...

    (Sorry for this default newbie response)
    A few things before we can help you, remember these on all your other posts:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code. This makes it easier for everyone to read your code.
    2. Explain to us what exactly your code is doing that is wrong. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly. This gives everyone an understanding of what the final product should be, compared to what you have now.

    Make those changes to your post, and do those on all your future posts, and you will receive help MUCH faster. Until those are done, I really can't help you much, as I don't have a full understanding to what is going on.


    Ok, now that that is out in the open...
    1) Can you specify the purpose of each class?
    2) You seemed to have included the ItalianFlagComponent class twice, and that confused me a bit.
    3) Out of curiosity, can you get 1 flag to show up? I've never tried doing this sort of thing, but what I know about Graphics2d tells me you shouldn't be able to draw a component by just making that component have a draw method. That usually requires some sort of inheritance, especially when we are talking about drawing. However, I am not entirely sure on that. I am unsure how sending Graphics2D objects through methods affects the ability to draw.
    Last edited by aussiemcgr; October 3rd, 2010 at 09:39 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help w/ entry level homework...

    Sorry, I have very little experience with programming, and posting code online. Sorry if my ettiquitte is is bad.... I'll try to clean it up. As far as the java tags are concerned, do I put highlight=java] and [/highlight before and after each class file that I post, or after all that I needed to post combined? Right now, I have 3 classes, from what I understand, ItalianFlag creates the object of the Italian flag. ItalianFlagViewer creates the JFrame window, and ItalianFlagComponent extends the JFrame componenet and creates the variable instructions for ItalianFlag. Again, I have been having trouble setting up the structure of my codes. I have been able to run it sucessfully in the regard that I get the Italian flag to open in a new JFrame window, but I cannot get it to run the two flags in the same window. I hope this helps and after I promise to use java tags next time (assuming I figure that out too)...
    thanks again,
    Brandon

    Ok... think I've got the java tag thing down... i'll update my original post!
    Last edited by rbread80; October 3rd, 2010 at 09:24 PM.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need help w/ entry level homework...

    You don't seem to have included the ItalianFlagViewer class above. Instead, you have included the ItalianFlag class and then the ItalianFlagComponent class twice.

    Since you can get the 1 flag to paint, it would mean that they way you are doing it is correct, but needs to be tweaked. That is very good to know. Without seeing the ItalianFlagViewer class, I can assume that the problem is one of these two most common issues with Graphics:
    1) When 1 Flag is drawn, the other is erased because of incorrectly overriding methods
    2) The Second Flag is drawn directly over the 1st Flag, appearing as if only 1 Flag is drawn, while there are actually two.

    Those are the two most common issues with Graphics and Graphics2D, and that is where I would assume the problem is. Another, less likely possibility, is that the 2nd Flag is printed outside of the viewable area of the ItalianFlagViewer. That is less likely because of the way drawing is done in JFrames. However, that problem still does occasionally happen.


    BTW, your code tags are correct. I'm sure you can visibly see how much easier it is to read your code since it is now Color Coded and clearly separated. It also helps that the JAVA Standard Object Names are linked to their appropriate APIs as it makes it faster for people to help you to review the APIs for the Objects you are using. All in all, most of the more respected helpers on the forum will overlook your posts if your code isn't in tags. You'll probably see more people get involved now that your code is in tags.

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    rbread80 (October 3rd, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help w/ entry level homework...

    ok, fixed it to include the ItalianFlagViewer just for continuity sake. I think you are right that it is not the problem, but what do I know? And yes, I completely understand what the java tags help! thanks for making me a better poster.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need help w/ entry level homework...

    Are you able to provide a picture of the output?

    I think I found your issue, it is obvious now that I actually payed attention to your constructors for ItalianFlag. Notice how you have two contructors, where you send them the x,y,width, and Color:
    public ItalianFlag(double x, double y, double aWidth) {
    }
    public ItalianFlag(double x, double y, double aWidth, Color c){
    this.c= c;
    }

    Notice what you forgot? In the second Constructor you set the Object's c variable. However, in both Constructors you do not set the Object's x,y, and width variables. What does this mean? It means that each time you create an ItalianFlag object, its x and y variables default to 0 since you didnt set them.

    What does this mean for your second flag? It means that the issue you have is #2 on the 2 most common issue with Graphics and Graphics2D. Fix your Constructors so that they set the x and y variables for the class, and that should fix your problem. I know you know how to do that, since you did it with the c variable in the second constructor.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    rbread80 (October 3rd, 2010)

  9. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help w/ entry level homework...

    THANK YOU SO MUCH! I can't believe I for got to do that... well, actually I can. Maybe I won't do it again in the future, but I probably will. Again, thanks!

Similar Threads

  1. Setting a transparency level of a picture issue
    By Asido in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2012, 05:42 AM
  2. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM
  3. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 06:39 PM
  4. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM

Tags for this Thread