Vahid Hashemi
Vahid Hashemi

Reputation: 5240

trying to attach a frame to a panel and draw a polygon , but there is no drawin on the frame

I'm developing a simple gui using javax.swing and tried to draw some polygons usings command buttons 3 to 9 say if you press command button number 3 it should draw a triangle for you. since the source code was a little bit long so I decided to copy it in pastebin.com

http://pastebin.com/R7jhTpee

the funny part is if I create a standalone class and call the paintComponent directly it will draw the things for me but when I want to attach it to another frame it doesn't work.

Upvotes: 1

Views: 133

Answers (2)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

The basic problem is that you are overriding the wrong method in the JPanel. You have:

 @Override
public void paintComponents(Graphics g) {

but it should be:

 @Override
public void paintComponent(Graphics g) {
                         ^

Upvotes: 5

Ryan Amos
Ryan Amos

Reputation: 5452

I'm pretty sure your problem is in the main method. You've got a bit of a jumble there. The first frame you make is an instance of JFrame, which is not what you want. The second one is an instance of MyFrame. This second frame is very small and you can't see it, depending on the operating system you're using. Simply get rid of the JFrame and expand the MyFrame and your code should work.

Upvotes: 1

Related Questions