• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

problem in Graphics program

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you jesper
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic