Results 1 to 8 of 8
  1. #1
    revanger is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default how do i call a class from main class

    Hallo...I am quite new to java programming and recently I came across a problem which I can't solve almost a week.I have a small garphic project called SmileyFace and when I compile it there's is awlays some problem with it. Could anybody help me just to give me a hint or a clue where am I mistaking by writing the code...

    Here's the code:


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

    public class RunSmileyFace
    {

    public static void main(String[] args)
    {
    SmileyFace myObjRef = new SmileyFace();
    myObjRef.SmileyFace();
    }
    }

    public class SmileyFace extends JFrame
    {
    public void SmileyFace()
    {
    setTitle("Smiley Face");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(250,220);
    setLocation(300,300);
    getContentPane().setBackground(Color.yellow);
    setVisible(true);
    }

    public void paint(Graphics g)
    {
    super.paint(g); // call the paint method of the superclass, Jframe
    g.setColor(Color.red);
    g.drawOval(85,75,75,75); // the face
    g.setColor(Color.blue);
    g.drawOval(100,95,10,10); // the right eye
    g.drawOval(135,95,10,10); // the left eye
    g.drawArc(102,115,40,25,0,-180); // the mouth
    g.drawString("Smiley Face", 90,175);
    }
    }// end of the code

    I will be very grateful if some one can help me out with it, because I have tried so many different ways and the result is zilch....Thanks.

  2. #2
    gold.dudu is offline Member
    Join Date
    Jul 2010
    Location
    IL
    Posts
    7
    Rep Power
    0

    Default

    what seems to fail?
    can you post the error msgs?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Instead of using the JFrame as the canvas to draw on, use a JPanel that is added to the JFrame. Override the JPanel's paintComponent() method and put your drawing code there.

    Search on this forum for code samples of paintComponent.

  4. #4
    revanger is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi, gold.dudu...the error message is :

    class SmileyFace is public,should be decleared in a file named SmileyFace.java

  5. #5
    revanger is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi Norm, I would try that you have suggested me, but in the case I wrote the code like this is there anything wrong, can you tell is it the way correct of calling class SmileyFace from the main class or have done it right, or I should call the first one in some other way...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by revanger View Post
    Hi Norm, I would try that you have suggested me, but in the case I wrote the code like this is there anything wrong, can you tell is it the way correct of calling class SmileyFace from the main class or have done it right, or I should call the first one in some other way...
    Obviously your current code is incorrect (otherwise it would've run and you wouldn't be here). Lucky for you your code isn't cast in stone so are able to change it; write the following (public) classes:

    Java Code:
    public class SmileyPanel extends JPanel {
       public void paintComponent(Graphics g) {
          // paint a smiley face here
       }
    }
    public class SmileyFrame extends JFrame {
       // add a new SmileyPanel to the contentPane here
    }
    public class Application {
       // this class contains the main method
       // and instantiates a SmileyFrame
    }
    compile it and run your Application class ...

    kind regards,

    Jos

  7. #7
    gold.dudu is offline Member
    Join Date
    Jul 2010
    Location
    IL
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by revanger View Post
    Hi, gold.dudu...the error message is :

    class SmileyFace is public,should be decleared in a file named SmileyFace.java
    if i get it right, you should just use a different java class file for "SmileyFace" class, it should be in same package where runSmileyFace class is.
    so you will have 2 files (classes) one for each public class

  8. #8
    Hema26 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    hi
    the class u r declaring is public so it should be saved with the same name otherwise it will give error. if u have created a pubic class SmileyFace then it should be saved with the name SmileyFace.java
    i hope now this error wont come again
    regards
    Hema26

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  2. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  3. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 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
  •