• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code removed while analyzing still works?  RSS feed

 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was examining this code:


I started by removing some of the code with the intent of replacing it with different code utilizing a different approach:


I was surprised to see that the app worked (just as it did before) after the code was removed (Vector, Enumeration, Point; with very few other changes). Now, I'm wondering why some of this original code was necessary at all. Can anyone explain? Thanks!
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I know we have an FAQ telling people off for simply saying, “It doesn't work,” but never thought we would have somebody posting, “It does work.”
And I know there is beginners' code where you can delete 50% of it without noticing any difference, but this looks different.
What exactly happened? Did you recompile the code? Did you suffer any compile‑time errors? Are you using an IDE?

Maybe you still have the old .class files around.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I found the 1st set of code, CanvasX (my name) just poking around. I created another Class, CanvasX2, in the same Eclipse Java Project. I edited CanvasX2 and re-ran it. I believe Eclipse compiles as you write the code. I believe that any problems would be pointed out when the Class is run. I did Clean the Project just to be sure. No Eclipse error flags. No Errors in Console when the Classes are run.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grrr! In reviewing this post I realized that the code that I had changed actually commented out this line:

...as I have since learned this will prevent the CanvasX2 JPanel from repainting it's background. Multiple squares however, will still be painted. Once the background IS repainted, e.g. resize the window, only one square remains. I can kind of get the same effect by just painting 5 squares when paintComponent is overridden:

If I click again super.paintComponent repaints the JPanel's background and provides 5 new squares.
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!