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 6 of 6

Thread: Case Study JSMileFace and JSmileFace2

  1. #1
    Junior Member linneadrep's Avatar
    Join Date
    Jan 2014
    Location
    In your <3
    Posts
    4
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Case Study JSMileFace and JSmileFace2

    This was asked:
    a. Write an application that extends JFrame and that displays a yellow smiling face on
    the screen. Save the file as JSmileFace.java.
    b. Add a JButton to the JSmileFace program so the smile changes to a frown when
    the user clicks the JButton. Save the file as JSmileFace2.java.
    and my code for JSmileFace
    // JSmileFace.java
    import javax.swing.*;
    import java.awt.*;
     
    public class JSmileFace{
      public static void main(String[] args) {
      JSmileFace j = new JSmileFace();
      }
     
      public JSmileFace(){
      JFrame frame = new JFrame("JSmileFace");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new MyComponent());
      frame.setSize(400,400);
      frame.setVisible(true);  
      }
     
      public class MyComponent extends JComponent{
      public void paint(Graphics g){
     
      g.setColor(Color.yellow);
      g.drawOval(100, 50, 200, 200);
      g.setColor(Color.yellow);
      g.fillOval(100,50,200,200);
     
      g.setColor(Color.BLACK);
      g.fillOval(155, 100, 10, 20);
      g.fillOval(230, 100, 10, 20);
     
     
     
      g.setColor(Color.BLACK);
      g.drawArc(150, 160, 100, 50, 180, 180);
      }}
      }
    public static void main(String[] args)
    {
    }


    Now, my problem is the JSmileFace2 I have no idea on how I would start with it.
    I need advice, ideas on how I will solve this problem and tips also where I need to learn about this that
    can be applied also out here.

    Thank you


  2. #2
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Case Study JSMileFace and JSmileFace2

    Well first things first JSmileFace2 extends jsmile.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Case Study JSMileFace and JSmileFace2

    If you wrote the first application, the one you posted, it's difficult to understand how can say you don't know where to start with the second. You use the first as a start for the second. Since that is obvious, I'm thinking you didn't state your question very well. What exactly is your question? What do you need help with?

    And here's a tutorial on using JButtons and Event Listeners.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    linneadrep (January 11th, 2014)

  5. #4
    Junior Member linneadrep's Avatar
    Join Date
    Jan 2014
    Location
    In your <3
    Posts
    4
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Case Study JSMileFace and JSmileFace2

    err. well I think I just realized what to start like extending . but next to that I still wasn't sure.
    well this is my code. for second
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    public abstract class JSmileFace2 extends JFrame implements ActionListener{
      JButton click=new JButton("Click here");
     
     
     
     
    public JSmileFace2(){
    Container con=getContentPane();
    con.setLayout(new FlowLayout());
    con.add(click);
     click.addActionListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
     
    }
     
      public void paint(Graphics g){
     
      g.setColor(Color.yellow);
      g.drawOval(100, 50, 200, 200);
      g.setColor(Color.yellow);
      g.fillOval(100,50,200,200);
     
      g.setColor(Color.BLACK);
      g.fillOval(155, 100, 10, 20);
      g.fillOval(230, 100, 10, 20);
     
     
     
      g.setColor(Color.BLACK);
      g.drawArc(150, 160, 100, 50, 180, 180);
      }
      public void actionPerformed(ActionEvent e)
      {
          Graphics pen=getGraphics();
     
          pen.setColor(Color.BLACK);
          pen.drawLine(40, 40, 40, 40);
     
     
      }
     
    public static void main(String[]args){
    JSmileFace2 frame=new JSmileFace2() {};
    frame.setVisible(true);
    }
    }
    {
    }

    what I'm confuse is -_- uh
    1.) no frown is appearing
    2.)the button only shows when my mouse is drag to its location
    3.)well I didn't really extend the JSmileFace
    4.)when I tried adding the repaint method on the actionperformed what happens is the button overlaps the smiley
    so my question is what's wrong with my code? what did I lack on this? and where is the frown?

    thank you for the link and sorry for confusing you.

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Case Study JSMileFace and JSmileFace2

    Are you getting any errors?

    Why is JSmileFace2 an abstract class? Why not concrete?

    Overriding the paint() method of JFrame is not the correct way to accomplish what you're trying to do, ever. Instead, add a JPanel to the JFrame and draw on the JPanel by overriding the JPanel's paintComponent() method. You can learn more about drawing/painting in Java by reading the Custom Painting tutorial.

  7. #6
    Junior Member linneadrep's Avatar
    Join Date
    Jan 2014
    Location
    In your <3
    Posts
    4
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Case Study JSMileFace and JSmileFace2

    It runs but had no frown.

    --- Update ---

    yea your right overriding didn't solve it -_- I just want it to run. well I know its still wrong cause I'm not sure.
    ok I'll be learning it first. Thank you so much sir.

Similar Threads

  1. how to study java the right ways ?
    By javabigkid in forum The Cafe
    Replies: 2
    Last Post: October 22nd, 2013, 10:56 AM
  2. Exam study review question
    By michael305rodri in forum Object Oriented Programming
    Replies: 3
    Last Post: October 17th, 2012, 06:10 PM
  3. Please help me with my Case Study
    By dalexquisite in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2012, 11:58 PM
  4. [SOLVED] Exercise for study course help?
    By SweetyStacey in forum Object Oriented Programming
    Replies: 12
    Last Post: April 25th, 2010, 02:01 PM