Results 1 to 4 of 4
  1. #1
    Bluzman is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Help with referencing two objects

    Hi, I am new to Java and am working on my first assignment but am having one small problem. The assignment is to draw a round smiling face and a square frowning face. I have done that. The problem is that it is only showing the frown face. The only way it will show the smiling face is if I comment out the code for the frown face. I know it has to do with how I am referencing the objects but can't see what I am doing wrong. Any help would be appreciated. Thanks.

    Code is listed below:

    import java.awt.*;
    import javax.swing.*;


    public class faces extends JApplet
    {

    public void paint(Graphics g)
    {
    //fill background color
    getContentPane().setBackground(Color.blue);

    Smile(g);
    Frown(g);
    }

    public void Smile(Graphics s)
    {
    super.paint(s);
    //draw oval face and fill with skin color
    s.setColor(new Color(239,208,207));
    s.fillOval(40, 40, 120, 150);

    //draw eyebrows
    s.setColor(Color.black);
    s.drawArc(55, 65, 35, 40, 15, 150);
    s.drawArc(108, 65, 35, 40, 15, 150);

    //draw left eye and fill with white
    s.setColor(Color.white);
    s.fillOval(57, 75, 30, 30);

    //draw black border around eye
    s.setColor(Color.black);
    s.drawOval(57, 75, 30, 30);

    //draw right eye and fill with white
    s.setColor(Color.white);
    s.fillOval(110, 75, 30, 30);

    //draw black border around right eye
    s.setColor(Color.black);
    s.drawOval(110, 75, 30, 30);

    //draw eyeballs and fill with black
    s.setColor(Color.black);
    s.fillOval(68, 81, 10, 10);
    s.fillOval(121, 81, 10, 10);

    //draw nose
    s.drawLine(100, 100, 90, 125);
    s.drawLine(100, 125, 90, 125);
    //gObj.fillRect(60, 139, 80, 10);

    //draw smile
    s.setColor(Color.black);
    s.fillArc(60,110,80,60,180,180);

    s.setColor(new Color(239,208,207));
    s.fillArc(60,102,80,50,180,180);

    s.setColor(Color.red);
    s.drawString("A Happy Java Programmer", 75, 250);

    }

    public void Frown(Graphics f)
    {
    super.paint(f);
    //draw square face
    f.setColor(new Color(188,143,143));
    f.fillRect( 210, 40, 150, 150);

    f.setColor(Color.black);
    f.fillArc(240, 140, 80, 50, 15, 150);

    f.setColor(new Color(188,143,143));
    f.fillArc(240, 148, 80, 40, 15, 150);

    //draw left eye and fill with white
    f.setColor(Color.white);
    f.fillOval(240, 75, 30, 30);

    //draw black border around eye
    f.setColor(Color.black);
    f.drawOval(240, 75, 30, 30);

    //draw right eye and fill with white
    f.setColor(Color.white);
    f.fillOval(296, 75, 30, 30);

    //draw black border around right eye
    f.setColor(Color.black);
    f.drawOval(296, 75, 30, 30);

    f.setColor(Color.black);
    f.fillOval(305, 79, 15, 15);
    f.fillOval(249, 79, 15, 15);

    //draw eyeballs and fill with black
    f.setColor(Color.green);
    f.fillOval(310, 84, 5, 5);
    f.fillOval(254, 84, 5, 5);

    //draw nose
    f.setColor(Color.black);
    f.drawLine(285, 100, 275, 125);
    f.drawLine(285, 125, 275, 125);

    //draw eyebrows
    f.setColor(Color.black);
    f.drawArc(235, 60, 40, 40, 15, 150);
    f.drawArc(292, 60, 40, 40, 15, 150);

    f.setColor(Color.green);
    f.drawString("A Sad Non-Java Programmer", 200, 250);

    }
    }//end of class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with referencing two objects

    Get the super.paint(g) out of your two methods. Instead put it at the start of the paint method itself so that it is called only once and only at the beginning of your painting. Otherwise when you call the super method, you'll erase anything painted before the call.

  3. #3
    Bluzman is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with referencing two objects

    Thanks very much Fubarable!! That worked.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with referencing two objects

    You're quite welcome!

Similar Threads

  1. FileInputStream Referencing (Probably Really Easy)
    By loopsnhoops in forum New To Java
    Replies: 13
    Last Post: 06-01-2011, 09:59 AM
  2. Referencing object from another Thread
    By Floiancu in forum Threads and Synchronization
    Replies: 6
    Last Post: 05-15-2011, 01:20 PM
  3. referencing objects via method help
    By alacn in forum New To Java
    Replies: 13
    Last Post: 08-08-2010, 08:52 PM
  4. referencing files in .exe
    By minime12358 in forum Advanced Java
    Replies: 7
    Last Post: 07-26-2010, 08:28 PM
  5. referencing objects from static method
    By talktofrank in forum New To Java
    Replies: 4
    Last Post: 10-26-2009, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •