MySQL Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  MySQL Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python MySQL Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python MySQL Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python MySQL Application Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
MySQL Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python MySQL Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
MySQL Application Hosting Web Hosting Sign-Up MySQL Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  MySQL Application Hosting Resellers web hosting, website hosting, web site hosting MySQL Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
MySQL Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
MySQL Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

MySQL Application Hosting Welcome MySQL Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting MySQL Application Hosting Fund Raising, Fundraising, web hosting MySQL Application Hosting Resellers, web Hosting MySQL Application Hosting Web Design, web Hosting MySQL Application Hosting Extra Services,  web Hosting MySQL Application Hosting Traffic Booster, web hosting MySQL Application Hosting Traffic Booster, web hosting MySQL Application Hosting Technical Support,  web Hosting MySQL Application Hosting webmaster tips,  web Hosting MySQL Application Hosting 30 Day Money Back, web hosting MySQL Application Hosting Legal Notices for Web Hosting MySQL Application Hosting Glossary Computer Terms for web Hosting MySQL Application Hosting Contact Information - web hosting

Site Map
MySQL Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP MySQL Application Hosting MySQL Application Hosting MySQL Application Hosting MySQL Application Hosting MySQL Application Hosting Solving Common Painting Problems (The Java™ Tutorials > Creating a GUI with JFC/Swing > Performing Custom Painting)
Trail: Creating a GUI with JFC/Swing
Lesson: Performing Custom Painting
Home Page > Creating a GUI with JFC/Swing > Performing Custom Painting
Solving Common Painting Problems
Problem: I don't know where to put my painting code.
  • Painting code belongs in the paintComponent method of any component descended from JComponent.
Problem: The stuff I paint doesn't show up.
  • Check whether your component is showing up at all. Solving Common Component Problems should help you with this.

  • Check whether repaint is invoked on your component whenever its appearance needs to be updated.
Problem: My component's foreground shows up, but its background is invisible. The result is that one or more components directly behind my component are unexpectedly visible.
  • Make sure your component is opaque. JPanels, for example, are opaque by default in many but not all look and feels. To make components such as JLabels and GTK+ JPanels opaque, you must invoke setOpaque(true) on them.

  • If your custom component extends JPanel or a more specialized JComponent descendant, then you can paint the background by invoking super.paintComponent before painting the contents of your component.

  • You can paint the background yourself using this code at the top of a custom component's paintComponent method:
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(getForeground());
    

Problem: I used setBackground to set my component's background color, but it seemed to have no effect.

  • Most likely, your component isn't painting its background, either because it's not opaque or your custom painting code doesn't paint the background. If you set the background color for a JLabel, for example, you must also invoke setOpaque(true) on the label to make the label's background be painted.

Problem: I'm using the exact same code as a tutorial example, but it doesn't work. Why?

  • Is the code executed in the exact same method as the tutorial example? For example, if the tutorial example has the code in the example's paintComponent method, then this method might be the only place where the code is guaranteed to work.
Problem: How do I paint thick lines? patterns?
  • The JavaTM 2D API provides extensive support for implementing line widths and styles, as well as patterns for use in filling and stroking shapes. See the 2D Graphics trail for more information on using the Java 2D API.
Problem: The edges of a particular component look odd.
  • Because components often update their borders to reflect component state, you generally should avoid invoking setBorder except on JPanels and custom subclasses of JComponent.

  • Is the component painted by a look and feel such as GTK+ or Windows XP that uses UI-painted borders instead of Border objects? If so, don't invoke setBorder on the component.

  • Does the component have custom painting code? If so, does the painting code take the component's insets into account?
Problem: Visual artifacts appear in my GUI.
  • If you set the background color of a component, be sure the color has no transparency if the component is supposed to be opaque.

  • Use the setOpaque method to set component opacity if necessary. For example, the content pane must be opaque, but components with transparent backgrounds must not be opaque.

  • Make sure your custom component fills its painting area completely if it's opaque.
Problem: The performance of my custom painting code is poor.
  • If you can paint part of your component, use the getClip or getClipBounds method of Graphics to determine which area you need to paint. The less you paint, the faster it will be.

  • If only part of your component needs to be updated, make paint requests using a version of repaint that specifies the painting region.

  • For help on choosing efficient painting techniques, look for the string "performance" in the Java 2D API home page.
Problem: The same transforms applied to seemingly identical Graphics objects sometimes have slightly different effects.
  • Because the Swing painting code sets the transform (using the Graphics method translate) before invoking paintComponent, any transforms that you apply have a cumulative effect. This doesn't matter when doing a simple translation, but a more complex AffineTransform, for example, might have unexpected results.

If you don't see your problem in this list, see Solving Common Component Problems and Solving Common Layout Problems.

Previous page: Summary
Next page: End of Trail
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting