Monday, February 13, 2012

Painting Swing components

Painting GUI components

In Java, components are rendered on screen in a process known as painting. Although this is usually handled automatically, there are occasions when you need to trigger repainting, or modify the default painting for a component.



The core painting mechanism is based on the Abstract Windowing Toolkit (AWT). In newer versions of Java, Swing painting mechanisms are based on and extend the functionality of AWT mechanisms.
Components can be heavyweight or lightweight. A heavyweight component has its own native screen peer. For a lightweight component to exist there must be a heavyweight further up the containment hierarchy. So lightweight components are more efficient. Swing components tend to be lightweight.

Painting for lightweight and heavyweight components differs slightly.
Painting is triggered when a GUI element is launched or altered in any way. This can be caused by
  • a system event
  • an application event
a system event
System-triggered painting operations are caused when a system requests a component to be rendered onscreen for the first time, resized, or repaired.

If you open the Save As window in an application, then a system request is triggered.
an application event
Application-triggered painting events occur when the internal state of an application changes and requires components to be updated.

For example, if an item is selected, the application might send a request for the item to be highlighted.
Painting in AWT
When a paint request is triggered, AWT uses a callback mechanism to paint the lightweight and heavyweight components of a GUI.

You should place the code for rendering the component in the relevant overriding paint method of the java.awt.Component class. The method is invoked whenever a system or application request is triggered.
public void paint(Graphics g)
The paint method is not invoked directly. Instead, you call the repaint method to schedule a call to paint correctly.

The Graphics object parameter is pre-configured with the state required to draw and render a component. The Graphic parameter can be reconfigured to customize painting.

public void repaint()
public void repaint(long tm)
public void repaint(int x, int y, int width, int height)
public void repaint(long tm, int x, int y,
  int width, int height)

For complex components, you should specify the region to be rendered using the arguments of the repaint method. The updated region is referred to as the clip rectangle.

The whole component is repainted if no arguments are given.

public void repaint()
public void repaint(long tm)
public void repaint(int x, int y, int width, int height)
public void repaint(long tm, int x, int y,
  int width, int height)

You can use an overridden update method to handle application-triggered painting differently from system-triggered painting.

For application-triggered paint operations, AWT calls the update method. If the component doesn't override the update() method, the default update method paints heavyweight components by clearing the background and calling the paint method.

The process of updating specified areas of a component is known as incremental painting. Incremental painting is not supported by lightweight components.
Consider the code that is used to refresh the applet when a user types text. You first create the ke instance to represent the event of typing a key.

public void keyTyped(keyEvent ke) {
  msg += ke.getKeyChar();
  repaint();
}

You call the ke instance's getKeyChar method to determine the value of the typed key. You assign the value to a variable - msg in this case - using the += overloaded operator.

You then call the repaint method, which in turn calls the overridden paint method. The value of the key typed - msg - is passed to the paint method and the key character is painted onscreen.


Painting in Swing
Painting Swing components is based on the AWT callback method, so it supports the paint and repaint methods. Swing painting also extends the functionality of paint operations with a number of additional features.

The Swing API, RepaintManager, can be used to customize the painting of Swing components. In addition, Swing painting supports Swing structures, such as borders and double-buffering.

Double-buffering is supported by default. Removing double-buffering is not recommended.
Because lightweight components are contained within heavyweight components, the painting of lightweight components is triggered by the paint method of the heavyweight component.
When the paint method is called, it is translated to all lightweight components using the java.awt.Container class's paint method. This causes all defined areas to be repainted.
There are three customized callbacks for Swing components, which factor out a single paint method into three subparts. These are
  • paintComponent()
  • paintBorder()
  • paintChildren()
paintComponent()
You use the paintComponent method to call the UI delegate object's paint method. The paintComponent method passes a copy of the Graphics object to the UI delegate object's paint method. This protects the rest of the paint code from irrevocable changes.

You cannot call the paintComponent method if UI delegate is set to null.
paintBorder()
You use the paintBorder method to paint a component's border.
paintChildren()
You use the paintChildren method to paint a component's child components.
You need to bear several factors in mind when designing a Swing paint operation.

Firstly, application-triggered and system-triggered requests call the paint or repaint method of a Swing component, but never the update method. Also, the paint method is never called directly. You can trigger a future call to it by invoking the repaint method.

As with AWT components, it is good practice to define the clip rectangle using the arguments of the repaint method. Using the clip rectangle to narrow down the area to be painted makes the code more efficient.

You can customize the painting of Swing components using two properties, namely
  • opaque
  • optimizedDrawingEnabled
opaque
You use the opaque property to clear the background of a component and repaint everything contained within the paintComponent method. To do this, opaque must be set to true, which is the default. Setting opaque to true reduces the amount of screen clutter caused by repainting a component's elements.
optimizedDrawingEnabled
The optimizedDrawingEnabled property controls whether components can overlap. The default value of the optimizedDrawingEnabled property is true.
Setting either the opaque or the optimizedDrawingEnabled property to false is not advised unless absolutely necessary, as it causes a large amount of processing.

You use the paintComponent method for Swing component extensions that implement their own paint code. The scope of these component extensions need to be set in the paintComponent method.

The paintComponent method is structured in the same way as the paint method.

// Custom JPanel with overridden paintComponent method
class MyPanel extends JPanel {
 
public MyPanel(LayoutManager layout) {
  super (layout) ;
}
 
public void paintComponent(Graphics g) {
  // Start by clearing the current screen
  g.clearRect( getX(), getY(), getWidth(), getHeight()) ;
  g.setColor (Color.red) ;
  int[] x = {30, 127, 56, 355, 240, 315 } ;
  int[] y = {253, 15, 35, 347, 290, 265} ;
  //Draw complex shape on-screen and fill it
  g.drawPolygon (x, y, 5) ;
  g.fillPolygon (x, y, 5) ;
  }
}

Summary

Painting is the process of rendering components onscreen. Heavyweight components have matching native peers, whereas lightweight components rely on a heavyweight container to do their painting. Painting is triggered by requests. System-triggered requests occur when windows are launched, resized, or repaired. Application-triggered requests occur when the internal state of an application changes.

AWT uses a callback method to invoke an overridden version of the paint method of the java.awt.Component class. You define the area that requires painting, the clip rectangle, using the arguments of the repaint method. You can also override the update method for application-triggered paint operations.

Swing painting is based on AWT painting and then adds further functionality. Heavyweight containers translate the paint method to all the lightweight components contained within them. Swing painting has three customized callbacks and two properties that you use to customize the Swing painting operation.

76 comments:

Hello Frndz....
Your blog has always been a good source for me to get quality tips on blogging. Thanks once again.

Swing handle

Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. Best JAVA Training in Chennai | JAVA Course in Chennai

Oracle DBA Training in Chennai
Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
Websphere Training in Chennai

Data warehousing Training in Chennai
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

Selenium Training in Chennai
Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

Oracle Training in chennai
Thanks for sharing such a great information..Its really nice and informative..

Oracle Training in chennai
Thanks for sharing such a great information..Its really nice and informative..

SAP Training in Chennai
This post is really nice and informative. The explanation given is really comprehensive and informative..

This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic
Android Training In Chennai In Chennai

Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
Unix Training In Chennai

I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
SalesForce Training in Chennai

There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

Best SQL Query Tuning Training Center In Chennai This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

Thanks for sharing information about pedicure cleaning and the meeting. Your blog has always been a source of great beauty tips Dash Inspectorate

Java is programing language which is used in almost all the applications and games which are on the web. Java is being used extensively and it will be used extensively in near future. So getting trained in Java will surely be helpful.
Java training in Chennai | Java training institute in Chennai | Java course in Chennai

This post is really useful.Your blog gives more ideas.Thanks for sharing this information.
Regards

Hadoop Training in chennai


It is good article.Our hadoop training course is more effective and real time environment.
Regards,
Hadoop course in Chennai | Hadoop Training institutes in Chennai

Thanks admin,You have posted the java concepts clearly and it is quite simple and impressive.It helps me a lot during my interview.Keep sharing like this.
Regards,
Hadoop Training in Chennai

Excellent Post, I welcome your interest about to post blogs. It will help many of them to update their skills in their interesting field.
Regards,
DOT NET Training in Chennai|DOT NET Training Institutes in Chennai|Best DOT NET Training in Chennai

Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info! , Regards, servicenow training in hyderabad

My Arcus offer java training with 100% placement. Our java training course that includes fundamentals and advance java training program with high priority jobs. java j2ee training with placement having more exposure in most of the industry nowadays in depth manner of java

java training in chennai

Powell Painting is not your run-of-the-mill painting company. Not only do we provide stellar painting services executed with skill, we also offer savvy interior design advice and colour consultations. Every client is provided a quote along with a custom concept that works with your unique space.

Great post, very helpful to understand. I really thankful to you for this kind of information shared. Keep sharing your knowledge. Java training institutes in pune

This comment has been removed by the author.
This comment has been removed by the author.

looking for dot net training & placement in Chennai? visit us at The best Dot Net training in Chennai

This comment has been removed by the author.
This comment has been removed by the author.
This comment has been removed by the author.

100% Job Oriented Oracle Training in Chennai For more details visit to The Best Oracle Training In Chennai
Try it Yourself »

Java is programing language which is used in almost all the applications and games which are on the web. Java is being used extensively and it will be used extensively in near future. So getting trained in Java will surely be helpful.
the best java training institute


Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

dot net training in chennai

php training in chennai

java training in chennai

This comment has been removed by the author.

looking for the best SAS training In Chennai? come & join us at Greens Technology, the leading software training & Placement in Chennai, for more information click to The Best SAS Training In Chennai

looking for the best Share Point training In Chennai? click to The Best Share Point In Chennai

This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training Java Online Training Java Online Training

Greens Technology's the leading software Training & placement centre Chennai & ( Adyar)
J2EE training in chennai

Great article Glad to find your blog. Thanks for sharing.
SCM training in chennai

Glad to have visit this blog. very informative articles

Best Informatica Training in Chennai

Excellant content thanks for sharing the unique information and keep posting.
ssas training in chennai

I have read your blog its very attractive and impressive. I like it your blog.
Java Training institute in velachery

Awesome post.
I read your blog everything is helpful and effective.
Thanks for sharing with us.
custom painters omaha

You post explain everything in detail and it was very interesting to read. Thank you. nata coaching centres in chennai

Very Useful Blog I really Like this blog and i will refer this blog...
And i found a some usefull content for Online Java training check It out .

Eduslab knowledge solutions provide some services as Project Management, Quality Management, Agile Management, IT Service Management etc...Our Mission is to become the foremost Essential, Respected, and skilled Development Company worldwide adhering to our values and attribute. Visit: project management training in gurgaon

I have read your blog its very attractive and impressive. I like it your blog. Java Training in Chennai

This comment has been removed by the author.

I just see the post i am so happy to the communication science post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be replay for your great thinks and I hope you post again soon...
Java Training in Chennai

Informative article, just what I was looking for.seo services chennai

You made some good points there. I did a search on the topic and found most people will agree with your blog. Java Training in Chennai

This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

That was a nice and interesting article and the writer has explained logically and systematically the procedure of painting swing components. I will not mind following this blog to read future articles especially on emerging and current issues in our society. Find time and visit our proofreading site by clicking on Format my Paper.

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
Dot Net Training in Chennai

Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
SAS Training in Chennai

Post a Comment