• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to draw Polygon to no avail

 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is kind of embarrassing but for some reason I'm getting this error message:"The method add(int, int) is undefined for the type Polygon". Its annoying because I know it must be something small but I can't figure it out. Any help would be appreciated


 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to CodeRanch.

It's pretty simple really. The compiler says add(int, int) is not a method in Polygon. Maybe you should take another look at the API.
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome

Well I see now it was addPoint() not just add(), wow I feel stupid
I'll probably be posting further stupidity in this thread in the very near future lol.
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so nothing is showing up on my screen, I think I've put the PaintComponent method in a place where it won't get called maybe, but putting it into the constructor didn't work, just gave me some errors. Using google I noticed many people have a seperate class for it and just call it via an object at main, but I want to have a menu bar within a frame and the panel that the shapes will be on to be on that (I read menu bars can be put on Panels but it turns out messy). Can anyone tell me what I'm doing wrong? I thought that maybe it was my attempt at the polygon that was incorrect (probably is) but its not working even with a rectengle.

The code hasn't changed from above, only this part
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using google I noticed many people have a seperate class for it and just call it via an object at main,


You don't need a separate class but you do need a custom component. Then the custom painting is done on the comonent and the component is added to the frame.

but I want to have a menu bar within a frame and the panel that the shapes will be on to be on that


Not sure what that means but you can add a JMenuBar and a JPanel to the same frame so I don't see the problem.

The reason your code doesn't work is because a JFrame does not have a paintComponent() method, that is why you do custom painting in a child component.

Read the Custom Painting tutorial for a working example.
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally! I managed to get a triangle to appear using polygons!
Thank you
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm back guys and with another problem: I can't seem to set the background color on my panel.
Heres what I have:







Does anyone see where I might be going wrong? I get no errors and I believe I'm doing the right thing by setting the background via the object, but nothing shows up except the triangle. Any help would be appreciated
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a typo:



Should be:



(without the "s")
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omg! Thank you! I would have never spotted that, there was no error message, I wonder why..
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There actually is a method with that name.
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I'll look it up
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm losing my mind abit here, I'm trying to get one of the polygons to change color when a user clicks on it. I'm trying to use MouseListener with MouseAdapter, but it seems I'm doing this quite wrong. After reading some stuff online it seems its abit more complicated then I first thought, because I need to check to see if the mouse is within the polygon first or not (probably using an if statement and booleans somehow) and if it is then its colour changes. If anyone has some advice on this I'd be greatful to hear it, this is my feeble atempt...



Rest of the code is the same as before. Thanks
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!