willisoften Posted March 12, 2004 Report Share Posted March 12, 2004 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. Quote Link to comment Share on other sites More sharing options...
fuzzylizard Posted March 13, 2004 Report Share Posted March 13, 2004 You have to implement the update() method. The default update() method clears the screen everytime you issue a repaint() method call. This is why you are only getting one shape on screen. Quote Link to comment Share on other sites More sharing options...
phunni Posted March 13, 2004 Report Share Posted March 13, 2004 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... Quote Link to comment Share on other sites More sharing options...
willisoften Posted March 13, 2004 Author Report Share Posted March 13, 2004 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. Quote Link to comment Share on other sites More sharing options...
phunni Posted March 13, 2004 Report Share Posted March 13, 2004 OK - makes sense to use more than one panel then. BTW - don't know if you have to use JBuilder, but eclipse is worth taking a look at - runs on both windows and Linux Quote Link to comment Share on other sites More sharing options...
phunni Posted March 13, 2004 Report Share Posted March 13, 2004 BTW - not certain you need to override update() - try just calling it insteadof repaint(). I could be wrong though - that's after just a quick glance at the docs Quote Link to comment Share on other sites More sharing options...
willisoften Posted March 13, 2004 Author Report Share Posted March 13, 2004 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. Quote Link to comment Share on other sites More sharing options...
willisoften Posted March 16, 2004 Author Report Share Posted March 16, 2004 (edited) 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 March 26, 2004 by willisoften Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.