Jump to content

Painting on JPanels Phunni help!


Recommended Posts

I'm trying to create a small JApplet whic uses an JPanel as a Canvas.

 

I've implemented a mouse listener and I can pick up the coordinates anywhere on my JPanel.

 

I can draw any shape I want to but each new shape wipes out the previous one.

 

That is only one shape on the canvas - JPanel at a time.

 

The mouse listener picks up the coordinates on click and release on release I also execute repaint(); I guess this is the problem but I'm not sure what to do about it.

 

Without repaint noting gets painted to the JPanel at all.

 

The JPanel / JApplet is specified for the assignment. I've read documentation till I'm blue in the face. So now I need a hint. Obviously I can't publish the code.

Link to comment
Share on other sites

What FuzzyLizard says looks right to me...

 

Just to point one thing out - you sound like you are putting a new JPanel on the JApplet, but are you aware that JApplet extends JPanel?

 

This means that you can use the applet itself as your panel. It doesn't really matter though - just thought I'd point it out - it would make your applet slightly better in regards to memory performance is all - but probably not enough to really worry about...

Link to comment
Share on other sites

Thanks guys thats probably it!

 

I need to override - the ba*** sorry method.

 

Phunni I need to demonstarte the use of a couple of layout managers so I've got a couple of panels in the one applet GridLayout BorderLayout and GridBagLayout.

Obviously performance went out for a coffee.

 

Incidentally using JBuilder (mostly on Windows -als alack) but theres a newish version for Linux as well.

Link to comment
Share on other sites

Hi guys it's still driving me nuts!

 

I've gone back to using Canvas as it's the only way I can get more than one shape on screen at a time - I don't have time to set up a linked list to store sets of co ordinates colours shapes and then do it all polymorpically. So I'll try to get everything in the assignment done bar the JPanel painting area. A pit as it let me do draggable shapes, rubber banding and stuff but only one shape at a time.

Link to comment
Share on other sites

Hello again

 

it looks like the way to do this is actually store each shape in a vector / linked list and paint everything you've done so far to the screen before calling repaint.

 

Strange but perhaps true. Looks like swing really needs a JCanvas.

 

I can do it but I'm not sure it's worth the hassle for what amounts to a very small percentage of my course work marks.

 

 

-----------------------------------------------------------------------------------

-----------------------------------------------------------------------------------

Well there were two acceptable solutions. Use a buffered image was one and the preferrred solution was to dynamically stroe all the necessary information to create a shape in a dynamic data structure.

 

I chose (stumbled on) the second and used an array list as for some strange reason there was apparently no Vector package available tin my JDK installation (Got it sorted now).

 

I had an abstact class of Shapes and then a concrete class for each type of shape I wanted to store each had it's own draw method. I stored each new shape as it was drawn in the list of Shapes and then iterated through my list to draw each shape using it's own draw method before calling repaint - phew! Polymorphism for beginners...

Edited by willisoften
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...