Subpages: 1. JComponent properties, size, and positioning
2. Event handling and dispatching
3. Multithreading
4. Timers
5. AppContext service
6. Inside Timers & the TimerQueue
7. JavaBeans architecture
8. Fonts, Colors, Graphics and text
9. Using the Graphics clipping area
10. Graphics debugging
11. Painting and validation
12. Focus Management
13. Keyboard input, KeyStrokes, and Actions
14. SwingUtilities
2.9 Using the Graphics clipping area
We can use the clipping area to optimize component rendering. This may not noticably improve rendering speed for simple components such as our JCanvas above, but it is important to understand how to implement such functionality, as Swing's whole painting system is based on this concept (we will find out more about this in the next section).
We now modify JCanvas so that each of our shapes, strings, and images is only painted if the clipping area intersects its bounding rectangular region. (These intersections are fairly simple to compute, and it may be helpful for you to work through, and verify each one.) Additionaly, we maintain a local counter that is incremented each time one of our items is painted. At the end of the paintComponent() method we display the total number of items that were painted. Below is our optimized JCanvas paintComponent() method (with counter):
The Code: JCanvas.java
see \Chapter1\3
public void paintComponent(Graphics g) {
super.paintComponent(g);
// counter
int c = 0;
// for use below
int w = 0;
int h = 0;
int d = 0;
// get damaged region
Rectangle r = g.getClipBounds();
int clipx = r.x;
int clipy = r.y;
int clipw = r.width;
int cliph = r.height;
// fill only damaged region only
g.setColor(Color.white);
g.fillRect(clipx,clipy,clipw,cliph);
// filled yellow circle if bounding region has been damaged
if (clipx <= 240 && clipy <= 240) {
g.setColor(Color.yellow);
g.fillOval(0,0,240,240); c++;
}
// filled magenta circle if bounding region has been damaged
if (clipx + clipw >= 160 && clipx <= 400
&& clipy + cliph >= 160 && clipy <= 400) {
g.setColor(Color.magenta);
g.fillOval(160,160,240,240); c++;
}
w = m_flight.getIconWidth();
h = m_flight.getIconHeight();
// paint the icon below blue sqaure if bounding region damaged
if (clipx + clipw >= 280-(w/2) && clipx <= (280+(w/2))
&& clipy + cliph >= 120-(h/2) && clipy <= (120+(h/2))) {
m_flight.paintIcon(this,g,280-(w/2),120-(h/2)); c++;
}
// paint the icon below red sqaure if bounding region damaged
if (clipx + clipw >= 120-(w/2) && clipx <= (120+(w/2))
&& clipy + cliph >= 280-(h/2) && clipy <= (280+(h/2))) {
m_flight.paintIcon(this,g,120-(w/2),280-(h/2)); c++;
}
// filled transparent red square if bounding region damaged
if (clipx + clipw >= 60 && clipx <= 180
&& clipy + cliph >= 220 && clipy <= 340) {
g.setColor(m_tRed);
g.fillRect(60,220,120,120); c++;
}
// filled transparent green circle if bounding region damaged
if (clipx + clipw > 140 && clipx < 260
&& clipy + cliph > 140 && clipy < 260) {
g.setColor(m_tGreen);
g.fillOval(140,140,120,120); c++;
}
// filled transparent blue square if bounding region damaged
if (clipx + clipw > 220 && clipx < 380
&& clipy + cliph > 60 && clipy < 180) {
g.setColor(m_tBlue);
g.fillRect(220,60,120,120); c++;
}
g.setColor(Color.black);
g.setFont(m_biFont);
FontMetrics fm = g.getFontMetrics();
w = fm.stringWidth("Swing");
h = fm.getAscent();
d = fm.getDescent();
// Bold, Italic, 36-point "Swing" if bounding regiondamaged
if (clipx + clipw > 120-(w/2) && clipx < (120+(w/2))
&& clipy + cliph > (120+(h/4))-h && clipy < (120+(h/4))+d)
{
g.drawString("Swing",120-(w/2),120+(h/4)); c++;
}
g.setFont(m_pFont);
fm = g.getFontMetrics();
w = fm.stringWidth("is");
h = fm.getAscent();
d = fm.getDescent();
// Plain, 12-point "is" if bounding region damaged
if (clipx + clipw > 200-(w/2) && clipx < (200+(w/2))
&& clipy + cliph > (200+(h/4))-h && clipy < (200+(h/4))+d)
{
g.drawString("is",200-(w/2),200+(h/4)); c++;
}
g.setFont(m_bFont);
fm = g.getFontMetrics();
w = fm.stringWidth("powerful!!");
h = fm.getAscent();
d = fm.getDescent();
// Bold 24-point "powerful!!" if bounding region damaged
if (clipx + clipw > 280-(w/2) && clipx < (280+(w/2))
&& clipy + cliph > (280+(h/4))-h && clipy < (280+(h/4))+d)
{
g.drawString("powerful!!",280-(w/2),280+(h/4)); c++;
}
System.out.println("# items repainted = " + c + "/10");
}
Try running this example and dragging another window in your desktop over parts of the JCanvas. Keep your console in view so that you can monitor how many items are painted during each repaint. Your output should be displayed something like the following (of course you'll probably see different numbers):
# items repainted = 4/10
# items repainted = 0/10
# items repainted = 2/10
# items repainted = 2/10
# items repainted = 1/10
# items repainted = 2/10
# items repainted = 10/10
# items repainted = 10/10
# items repainted = 8/10
# items repainted = 4/10
Optimizing this canvas wasn't that bad, but imagine how tough it would be to optimize a container with a variable number of children, possibly overlapping, with double-buffering options and transparency. This is what JComponent does, and it does it quite efficiently. We will learn a little more about how this is done in section 2.11. But first we'll finish our high level overview of graphics by introducing a very powerful and well-met feature new to Swing: graphics debugging.



RSS feed Java FAQ News