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.
// Graphics g is passed to your paint as a parameter. // get x, y, width and height of region we are actually painting Rectangle r = g.getClipBounds();
Container.paint() only deals with painting the background of the Container. Container. paintComponents() paints each of the contents, and Container. paintAll() paint everything. Container. paintComponents() calls each contained flyweight component’s paint routine in turn. For efficiency, it bypasses the call if the component is definitely outside the clipregion. For heavyweight contained components, the native GUI handles repainting without AWT (Advanced Windowing Toolkit) involvement. Container.update() behaves similarly. If a Component has zero size, paint won’t be called.
You can’t control whether components get painted by overriding paintComponents or paintAll. The components have a repainting life of their own separate from the Container. To hide them you must either remove or setVisible( false );
You might wonder why paint gets passed a Graphics object. Often paint is asked to restore only a small fraction of the entire image, since for example some obscuring window has been removed from a corner of the Component. When scrolling, the paint method needs to paint only a 4-pixel tall sliver of newly emerging material. The rest of the display is generated by bitblt of previously rendered material. The Graphics object has a built-in clip region, describing just the portion that needs to be redrawn. paint also might be called on the to do duty in printing. The AWT needed some easy way to redirect the output of paint from the screen to the printer. The Graphics object parameter does that. Similarly a paint routine might be asked to create an in-RAM non-displayed image that can later be rapidly blasted onto the screen with
g.drawImage( img, x, y, this );
In animation, there are two kinds of repaint requests, ones you create to ask paint to do the next animation frame, which may require only a minor touch up of the image, or perhaps scrolling it a pixel, and ones from the operating system, required because your component that was occluded has just been uncovered, and require a complete paint of the component from scratch. How do you tell them apart? You can set some boolean in the component for paint to examine. Further paint can check that g.getClipBounds().equals(new Rectangle(this.getSize())) indicating a request to paint the entire image. The alternative for animation is to abandon the repaint mechanism and paint yourself directly with Component.getGraphics.
JPanel.paintComponent() just paints the JPanel’s content, the JPanel component itself. JPanel.paint() paints the panel, the panel’s children and the border.
Whenever you override paint or paintComponent, chances are you also need to implement getMinimumSize, getMaximumSize and getPreferredSize. I have found this more reliable than using the new setMinimumSize etc. methods. If you don’t, you will usually find your Component squeezed down to 0 × 0 pixels.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[184.108.40.206]
|Feedback||You are visitor number 36,150.|