• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Repainting /clipping area?

 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://mindprod.com/jgloss/repaint.html

Faster Repaints

One simple technique to speed up repaint is to use


When you know that only a portion of the component has changed. You can further speed things up, in your paint or paintComponent method by paying attention to the clip bounds. Anything you paint outside the clipping region will just be ignored. For efficiency, you should make some effort to avoid rendering large amounts of screen real estate outside it.



This is what is said in this link so I want to know should I be using a clipping area for repainting 1 rectangle after moving it? I tried to do but all I get is it repainting the new one, without getting rid of the original spot...








Now if I'm going to use the I would need to do but how do I incorporate that with the repaint? There is no clipping method in the rectangle class itself it seems, but it would have to do with the Graphics class? So I would have to set the currentFace in the paint method to "getClipBounds" and in turn only have the currentFace repaint? would I call normally or would I have to do anything extra?


Thanks for any help, much appreciated,

~JO
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way would be to get the bounds of the rectangle before the move, move the rectangle, repaint both the original and the new area.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:One way would be to get the bounds of the rectangle before the move, move the rectangle, repaint both the original and the new area.



So to do this I need to be able to call Graphics g as a parameter in my mouseDragged method? Set the bounds before and after, and do a normal repaint();? Or do I need to do something with the repaint as well?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually a Swing/AWT question, so I'll move it to the appropriate forum.

I'd say it should be possible to invalidate both areas - both old and new position of the rectangle - just call the repaint( int x, int y, int width, int height ) method twice with proper parameters. I'd expect that Swing/AWT will create a bounding rectangle to cover all invalidated areas, which will then be used in the paint method. I haven't tried this, though, I just remember similar mechanism from the old days of my Windows programming.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never use getGraphics() of a Component.

Get the Rectangle before the move, probably in mousePressed(...)
Get the Rectangle after teh move, probably in mouseReleased(...) or maybe in mouseDragged(...)
Add one Rectangle to the other (see the API for java.awt.Rectangle)
Repaint the union of the two Rectangles.

But don't do any of that if you don't actually have a performance problem. Premature optimization is evil. Simple code that performs adequately is always more readable and maintainable.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Never use getGraphics() of a Component.

Get the Rectangle before the move, probably in mousePressed(...)
Get the Rectangle after teh move, probably in mouseReleased(...) or maybe in mouseDragged(...)
Add one Rectangle to the other (see the API for java.awt.Rectangle)
Repaint the union of the two Rectangles.

But don't do any of that if you don't actually have a performance problem. Premature optimization is evil. Simple code that performs adequately is always more readable and maintainable.



Well the issue is that I'm trying to just get the 1 rectangle I move to repaint. With the or it wont get rid of the old rectangle.

What exactly is adding/union going to accomplish? When adding the 2 rectangles all it does when dragged is make the dragged face bigger, and union doesn't do anything. Will it be that "adding" the 2 together and then creating a new rectangle and repainting that "rectangle area" will just show the 1 rectangle afterwards? What will the union accomplish? It seems like adding already creates a "union" or am I wrong?


void add(Rectangle r)
Adds a Rectangle to this Rectangle.

Rectangle union(Rectangle r)
Computes the union of this Rectangle with the specified Rectangle.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post an SSCCE as maybe we're not talking about the same thing.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Post an SSCCE as maybe we're not talking about the same thing.



sorry I thought the code I had was enough, I'll post more.
















This should be all you need, thanks for the help sir!
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay, did you read the SSCCE page? You've posted code that includes setting the LaF to Nimbus and who knows what else that isn't relevant to the problem. If there is a problem at all -- are you really experiencing a visible, noticeable performance hit?

edit And you're still using getGraphics(). I already told you that's not correct.

Please go through the Lesson: Performing Custom Painting.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Jay, did you read the SSCCE page? You've posted code that includes setting the LaF to Nimbus and who knows what else that isn't relevant to the problem. If there is a problem at all -- are you really experiencing a visible, noticeable performance hit?

edit And you're still using getGraphics(). I already told you that's not correct.

Please go through the Lesson: Performing Custom Painting.



Yes I did, I added the main sinceI wasn't sure if it was needed or not, it was something Netbeans auto-created with a Jframe Form... Sorry.... The code should work regardless. I already said what my issue was, trying to repaint the currentFace, without repainting the entire screen... I checked out the tutorial again, thanks for the link, I tihkn I might try doing the painting in the Face class itself(like on #4) and see ow that goes (it repaints the class itself, so this is probably what I am looking for).

I didn't realize that I had (I thought you were talking about the from my first post. I believe I need to use each time I move my rectangle(instead of repainting each time) so I wanted to put Graphics g it into my mouseDragged parameter, but it's giving me an issue with it being an "abstract" class, and wanting mouseDragged(MouseEvent e) only, so I guess I'll have to set up the mouse motion listener like in the example(without implementing it) and put it as a parameter that way. Is there a reason I shouldn't use XORMode and just redraw as I move vs repainting the rectangle each time? What exactly is the issue with getGraphics();? Is there too much overhead associated with calling it, or is it bad practice?
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Jay, did you read the SSCCE page? You've posted code that includes setting the LaF to Nimbus and who knows what else that isn't relevant to the problem. If there is a problem at all -- are you really experiencing a visible, noticeable performance hit?

edit And you're still using getGraphics(). I already told you that's not correct.

Please go through the Lesson: Performing Custom Painting.



Ok so after switching to using repaint() with multi-arg my rectangle changes to the background color(gray) which causes flickerings all over... It also repaints EVERY rectangle, which I have 150 currently and can have way more... That isn't very effective and IMO isn't very practical when you have so many which can cause a big performance hit. I also notice it has to go through the entire paint method to see what I need to repaint.... Next I am trying to drag an outline while having the original stay in place (which is what I had originally). I know you didn't like the getGraphics(); so I tried to use a parameter of the mouseDragged for Graphics g, but that wasn't working for some reason(is it because the only argument it should have is (MouseEvent)? I am going to try using the paint routine in my Face class, and see if using another method like the moveSquare(Graphics g) and see if I can use the XOR like I wanted to and g.draw like I originally had. Everything was fine except for the last repaint, which I believe I understand what happened before( although there was still flickering for some reason).

I learned some things from reading this tutorial again, but I know why I didn't use the repaint in the first place, lots of issues.... Is there a reason why it would flicker?






 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, if you went through the tutorial you should know by now that the method to override is paintComponent(...) not paint(...). And it does rather look like you're trying to do custom painting ins a top level window, which isn't the right way to go about it -- which you would also have learned form the tutorial.

In absence of an SSCCE, it's difficult to know the exact cause, but any and all flickering is on account of your program. Drawing 150 rectangles certainly doesn't impose any noticeable/visible performance hit, and doesn't require clipping the repainting rectangle.

Try this code I posted back in 2008; draw as many faces as you like and drag a few around. Do you see any flicker?

 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I'm posting again because I'm still having issues... I recently came back to this and I tried to do the code exactly like in the Oracle trail, just modified.




The face class is just an upgrade of a Rectangle. I extend Rectangle2D.Float to be able to use Contains(For the next class I will post) and originally was used for painting faces before I was told/saw in the Oracle trail to paint in the "Face" class itself.




I'm having an issue with the drag... I'm stuck... I originally had it as currentFace.setX(e.getX()); and same for Y, but realized e.getX() is the "Zoomed" value... I have to keep the original values the same and keep everything on the screen "Zoomed." This entire time though the dragged rectangle is ALWAYS gray when using paint and a JFrame, and invisible when it's a JPanel and paintComponent...... WHY?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic