• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to compile this piece of code

 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry its working
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got it...then how to run this program.I am not able to do this.please help me out
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Find JPanel tutorial and sample code => Here

Cheers,
Raj.
[ May 15, 2008: Message edited by: Raj Kamal ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic