Originally Posted by
meisme
...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