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

Thread: Issue with main

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Issue with main

    Hello everyone, Im still a beginner at JAVA and I seem to be having an issue with the main in most of my programs,
    my colleagues usually help out with it, but I want to know what am I doing wrong,
    for example im trying to make this code work, but the main issue rose up again
    // DrawPanel.java
     
     
    import java.awt.*;
    import javax.swing.*;
     
    class DrawPanel extends JPanel {
     
      public void paintComponent(Graphics g) {
     
       super.paintComponent(g);
     
        int i;
     
        // draw a spiral
        cx = 250; cy = 100;
        Polygon spiral = new Polygon();
        for (i = 0; i < 360; i++) {
    	double t = i / 360.0;
    	spiral.addPoint(
    	  (int)(cx + r * t * Math.cos(t * 8 * Math.PI )),
    	  (int)(cy + r * t * Math.sin(t * 8 * Math.PI )));
        }
     
        g.drawPolygon(spiral);
        }
     
     
      } //paintComponent
    } //class DrawPanel
     
    class DrawFrame extends JFrame {
      public DrawFrame(String s) {
        setTitle(s);
        setSize(300, 400);
     
        Container contentPane = getContentPane();
        contentPane.add(new DrawPanel());
      } // DrawFrame
     
      public static void main(String[] args) {
        JFrame f = new DrawFrame("My Drawing Frame");
        f.show();
      } //main
     
    } // DrawFrame

    thx for your feedbacks in advance


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Issue with main

    Quote Originally Posted by meisme View Post
    ...main issue rose up again
    You want to create a main() method that is executed when you run java.

    So, for example, for your project, when you try run java DrawPanel, The class DrawPanel must be a public class in a file named DrawPanel.java, and there must be a public static void main() method with the proper arguments.

    Furthermore...

    For your project, if you want to run java DrawFrame, the class DrawFrame must be a public class in a file named DrawFrame.java, and there must be a public static void main() method with the proper arguments.

    For your project, there are no decisions to be made when starting out. (Other, more "interesting" projects have a lot more decisions.)

    Since your main() method is the DrawFrame class, you would have the class defined in a file named DrawFrame.java. I suggest that you build your project file a piece at a time, I would start a brand new project with nothing in it except a file named DrawFrame.java, and it would look something like the following:
    //
    // DrawFrame.java
    //
    import java.awt.*;
    import javax.swing.*;
     
    class DrawFrame extends JFrame {
     
        public DrawFrame(String s) {
            setTitle(s);
            setSize(300, 400);
     
            Container contentPane = getContentPane();
            contentPane.add(new DrawPanel());
        } // End DrawFrame constructor
     
        public static void main(String[] args) {
            JFrame f = new DrawFrame("My Drawing Frame");
            // According to javac -version 1.6.0_22, show()
            // is deprecated.  Nowadays, I think you are
            // supposed to use setVisible
            //f.show();
     
            f.setVisible(true);
     
        } //main
     
    } // End DrawFrame class

    Of course if you try compiling, this will result in a compiler error, since it has "new DrawPanel()", and there is no DrawPanel class (yet) but I would try compiling anyhow.

    Then, if everything looks OK except the part about the compiler not knowing about DrawPanel, I would add DrawPanel stuff.

    You can put the DrawPanel stuff in a separate file, or you can put the class in the same file as DrawFrame. For simple applications like you seem to have, I don't see anything wrong with putting everything in one file, but more "interesting" projects might be easier to manage if you put the classes in separate files.

    Anyhow, I might try the following:

    //
    // DrawFrame.java
    //
    import java.awt.*;
    import javax.swing.*;
     
    class DrawFrame extends JFrame {
     
        public DrawFrame(String s) {
            setTitle(s);
            setSize(300, 400);
     
            Container contentPane = getContentPane();
            contentPane.add(new DrawPanel());
        } // End DrawFrame constructor
     
        public static void main(String[] args) {
            JFrame f = new DrawFrame("My Drawing Frame");
            // According to javac -version 1.6.0_22, show()
            // is deprecated.  Nowadays, I think it is
            // recommended that you use setVisible
            //f.show();
     
            f.setVisible(true);
     
        } //main
     
    } // End DrawFrame class
     
    //
    // This will be filled out with specific parameters later
    //
    class DrawPanel extends JPanel {
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
     
        } //paintComponent
     
    } //class DrawPanel

    Now, you should be able to compile and execute this.

    Make sure that this arrangement of the classes in the file will compile (and execute) before getting to the Good Stuff of putting in the specific code to perform the task.

    My suggestion is to add a few lines at a time and recompile often to avoid having to wade through many lines of code to find an extra '}' or a missing '}' or some such thing.


    Cheers!

    Z

Similar Threads

  1. Syntax of main
    By RKM904 in forum The Cafe
    Replies: 2
    Last Post: September 7th, 2012, 01:09 PM
  2. How to do the main ?
    By sarrrry92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 22nd, 2012, 09:51 AM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM