GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Not able to compile this piece of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to compile this piece of code" Watch "Not able to compile this piece of code" New topic
Author

Not able to compile this piece of code

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi, i am not able to compile this piece of code.what s the issue? Is it that it is not having the main method?

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

class MyDrawPanel extends JPanel{

public void paintComponent( Graphics g){
g.setColor(Color.orange);
g.fillRect(20,50,100,1000);
}
}
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
sorry its working
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
oops the issue is that I am not able to run this piece code after successful compilation.Error coming is
Exception in thread "main" java.lang.NoSuchMethodError: main

Is it because of the main body is missing?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi Rakhee,

Well if it said NoSuchMethodException thats exactly what it misses, the main() method. The snippet will compile but you wont be able to execute it unless it has the public static void main(String arg[]) {} method.

Cheers,
Raj.
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
well then i tried putting this code:

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

class MyDrawPanel extends JPanel{

public static void main (String[] args){
MyDrawPanel mydrawpanel = new MyDrawPanel();
mydrawpanel.paintComponent();
}

public void paintComponent( Graphics g){
g.setColor(Color.orange);
g.fillRect(20,50,100,1000);
}
}

but this code is not even compiling.
error thrown is :

MyDrawPanel.java:8: cannot find symbol
symbol : method paintComponent()
location: class MyDrawPanel
mydrawpanel.paintComponent();
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hey,

You don't have a paintComponent() method declared for MyDrawPanel. The one you have expects a Graphics object as argument.

Cheers,
Raj.
[ May 15, 2008: Message edited by: Raj Kamal ]
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Well compiler compains correctly
Have a look at your error message




You don't have paintComponent() method in your MyDrawPanel class.
You have only paintComponent(Graphics g) defined in your class.
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
ok got it...then how to run this program.I am not able to do this.please help me out
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

Find JPanel tutorial and sample code => Here

Cheers,
Raj.
[ May 15, 2008: Message edited by: Raj Kamal ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You also need to choose a top-level container to add your panel to, otherwise you will never see it. Most popular type of top-level container would be a JFrame. You need to go through the whole of the Swing Tutorial which you quoted earlier.
 
GeeCON Prague 2014
 
subject: Not able to compile this piece of code