Don't use applets. Nobody has used them for the best part of ten years.
Please port your app to a JPanel with its paintComponent() method, which should have protected access. Tell us what the exact problem is then.
Please explain why you are using @SuppressWarnings(...).
Those lines from 93 to 113 in your posted code. You meant them to represent an animation where the display changes every 35 milliseconds. But it doesn't work that way. All of those display changes get put into a queue called the "Event Dispatch Thread", which processes them at its own speed. So you may see jerky changes happening but not every 35 milliseconds and not what you expected.
But the Swing designers knew that people wanted to do that kind of thing and so they provided a mechanism to support it. Tutorial here: How to Use Swing Timers.
I've just modified the whole code so now it is much more simple.
As you all guys mentioned a different approach, i have tried several things, with no luck at all (in all of them i always get the trace of the shape being drawn across the screen or i simply get no shape drawn at all), including:
- Adding/commenting in several different ways the above code lines (42; 64; 65 and 67);
- The Timer approach;
- Getting rid of JApplet extension and substitute it with a JFrame one;
The weird thing is that this is such a simple thing to do, a shape moving across the screen with no trace of it's own shape (I've done this multiple times in the past before, but i'm a little rusty with java language at the moment as i am working with other stuff for the last 12 years now (this is why i had the JApplet implemented as this was an old unfinished project i had started a long time ago).
So, with this being said and looking at the much simpler/cleaner code above, what am i doing wrong here? :S Thank you.
PS - I am sending you the CustomShapeButton class as well.
Well, I specifically stated you should NOT be extending JFrame.
There is no need to extend any class. All you need is the class with the main() method that creates the frame and adds the panel to the frame.
I gave you the code for this.
Not only did you change the code but you changed the order of the statements in the code.
Swing components should be added to the frame BEFORE the frame is made visible.
Regarding your custom painting I am not sure why you are extending JButton. There is no need to extend a Swing component to draw a custom Shape.
All you need to do is create your Polygon and paint the polygon in your paintComponent() method.
See Playing With Shapes for a simple code example that shows how to create a "triangle" Shape and paint the triangle in paintCompoennt.
in your case you have a "shapes" ArrayList. So you need to just create your custom shape and add the shape to the ArrayList. Then in the paintComponent() method you can iterate through the ArrayList to paint all the shapes.
So basically your "drawShape(...)" method should just be a stand alone method that returns the Polygon instance so you can add the instance of the Polygon to the "shapes" ArrayList.
For an example of doing custom painting with a dynamic number of shapes, using an ArrayList see the "Draw On Componnt" example in Custom Painting Approaches
And has already been suggested you should be using a Swing Timer for animation. Swing components need to be updated on the Event Dispatch Thread (EDT). The Swing Timer executes on the EDT.
Rob Camick wrote:Not only did you change the code but you changed the order of the statements in the code.
I did this because i was trying to figure out if the behavior had anything related to the order of the instructions.
Rob Camick wrote:Regarding your custom painting I am not sure why you are extending JButton. There is no need to extend a Swing component to draw a custom Shape.
The code you are seeing here is barely 20% of the whole project. This will be a launching window, having images and shapes that i need them to behave as animated buttons thus the use of an extended JButton.
As the complete code of the project is unnecessary to debug this simple problem, i have removed almost all of it just to simplify things, but i didn't remove the whole thing, so a few original leafs still remain here and there.
I will definitely try your approach and, most important of all, i will try to understand it.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop