Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

super.paintComponent() with an unexpected behavior

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good afternoon,

I'm trying to draw a single shape on different YY positions several times on a JPanel (to mimic an animated shape).

The final result i expected to be was that, after i click on the shape, only one shape should be visible in the screen (moving across as it is being redrawn).

Nevertheless, what i am getting right now is a bunch of shapes drawn in different YY positions, visible in the screen.

I am using the super.paintComponent() method while overriding the paintComponent() class.

I know that this is the correct approach, as super.paintComponent() is responsible for redrawing JPanel's background and thus, only one shape should be visible moving across the screen.

The following code is what i have right now:



Please note that it is extending JApplet (and not JFrame, as it should) only because this is an old project of mine, but i'm pretty sure that this undesirable behavior has nothing to do with it.

Any ideias?

Please check both images i'm sending attached.

Thank you in advance.
J Amorim
greenshot_2019-07-08_15-40-37.png
[Thumbnail for greenshot_2019-07-08_15-40-37.png]
pic 01 - initial state
greenshot_2019-07-08_17-04-47.png
[Thumbnail for greenshot_2019-07-08_17-04-47.png]
pic 02 - end state (unexpected behavior)
 
Marshal
Posts: 77532
372
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(...).
 
Marshal
Posts: 27582
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3291
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please note that it is extending JApplet (and not JFrame, as it should)



It should do neither.

There is absolutely no reason to extend JApplet since you are NOT creating an appliet.

And you should not extend JFrame either since you are NOT adding any new functionality to the JFrame. Adding components to a frame is not adding functionality to the frame.

So your code should simply be:



Then you also get rid of the init() method.

Instead you simply add your DBPanel to the frame directly:


Don't know if this will fix your problem but at least now you should be using Swing properly by adding components to the frame.

If you need more help then post your SSCCE showing the code changes.

We also don't have access to your CustomShapeButton, so we don't know if that might be causing the problem.

 
Joaquim Amorim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

First of all thank you all for your answers.

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.



 
Rob Camick
Rancher
Posts: 3291
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3291
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example that puts most of those above comment into practice.

The main difference is that the class animates an ArrayList of "Ball" objects, not Shape objects, because I wanted to add additional properties for each Ball.

 
Joaquim Amorim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help, Rob.

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.

Cheers!
 
Tell me how it all turns out. Here is a tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic