• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in Graphics program  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


class MyPanel extends JPanel
{
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}

the above code compiles fine but,when i try to run it,it shows run time exception :main method not found.
I found this question Head First java book ,page no.-364
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a complete program. Java programs must always have a public static void main(String[] args) method somewhere. When you run the program. that's the method the JVM will call to run the code in your program.

For a graphics program, you'll need some more code to set up a window etc. to draw in. The code you posted is just a small part of a complete graphics program.

Probably this was meant in the book just to show how you would implement the paint() method, and it is not intended as a complete program.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats true but even after writing the main method how am i supposed to call the paint method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example.

For more information see Oracle's tutorials: Creating a GUI With JFC/Swing and 2D Graphics.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you jesper
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have one more question it is not at all related to the topic but i am really desperate for the answer.

question:can any one suggest names of brain friendly advance java books,by brain friendly i mean those books which follow the same approach as "Head first java".

thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!