jdk/src/share/classes/javax/swing/Painter.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 23010 6dadb192ad81
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21982
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     4
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    10
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    15
 * accompanied this code).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    16
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    24
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    25
package javax.swing;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    26
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    27
import java.awt.Graphics2D;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    28
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    29
/**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    30
 * <p>A painting delegate. The Painter interface defines exactly one method,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    31
 * <code>paint</code>. It is used in situations where the developer can change
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    32
 * the painting routine of a component without having to resort to subclassing
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    33
 * the component. It is also generically useful when doing any form of painting
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    34
 * delegation.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    35
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    36
 * <p><code>Painter</code>s are simply encapsulations of Java2D code and make
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    37
 * it fairly trivial to reuse existing <code>Painter</code>s or to combine
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    38
 * them together. Implementations of this interface are also trivial to write,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    39
 * such that if you can't find a <code>Painter</code> that does what you need,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    40
 * you can write one with minimal effort. Writing a <code>Painter</code> requires
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    41
 * knowledge of Java2D.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    42
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    43
 * <p>A <code>Painter</code> may be created with a type parameter. This type will be
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    44
 * expected in the <code>paint</code> method. For example, you may wish to write a
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    45
 * <code>Painter</code> that only works with subclasses of {@link java.awt.Component}.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    46
 * In that case, when the <code>Painter</code> is declared, you may declare that
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    47
 * it requires a <code>Component</code>, allowing the paint method to be type safe. Ex:
9222
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    48
 * <pre>
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    49
 * {@code
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    50
 * Painter<Component> p = new Painter<Component>() {
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    51
 *     public void paint(Graphics2D g, Component c, int width, int height) {
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    52
 *         g.setColor(c.getBackground());
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    53
 *         //and so forth
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    54
 *     }
9222
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    55
 * }
7d19f2c40915 7032376: A type parameter isn't seen by using an web browser
rupashka
parents: 5506
diff changeset
    56
 * }
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 9222
diff changeset
    57
 * </pre>
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    58
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    59
 * <p>This interface makes no guarantees of threadsafety.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    60
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    61
 * @author rbair
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    62
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    63
public interface Painter<T> {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    64
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    65
     * <p>Renders to the given {@link java.awt.Graphics2D} object. Implementations
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    66
     * of this method <em>may</em> modify state on the <code>Graphics2D</code>, and are not
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    67
     * required to restore that state upon completion. In most cases, it is recommended
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    68
     * that the caller pass in a scratch graphics object. The <code>Graphics2D</code>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    69
     * must never be null.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    70
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    71
     * <p>State on the graphics object may be honored by the <code>paint</code> method,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    72
     * but may not be. For instance, setting the antialiasing rendering hint on the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    73
     * graphics may or may not be respected by the <code>Painter</code> implementation.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    74
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    75
     * <p>The supplied object parameter acts as an optional configuration argument.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    76
     * For example, it could be of type <code>Component</code>. A <code>Painter</code>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    77
     * that expected it could then read state from that <code>Component</code> and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    78
     * use the state for painting. For example, an implementation may read the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    79
     * backgroundColor and use that.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    80
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    81
     * <p>Generally, to enhance reusability, most standard <code>Painter</code>s ignore
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    82
     * this parameter. They can thus be reused in any context. The <code>object</code>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    83
     * may be null. Implementations must not throw a NullPointerException if the object
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    84
     * parameter is null.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    85
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    86
     * <p>Finally, the <code>width</code> and <code>height</code> arguments specify the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    87
     * width and height that the <code>Painter</code> should paint into. More
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    88
     * specifically, the specified width and height instruct the painter that it should
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    89
     * paint fully within this width and height. Any specified clip on the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    90
     * <code>g</code> param will further constrain the region.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    91
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    92
     * <p>For example, suppose I have a <code>Painter</code> implementation that draws
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    93
     * a gradient. The gradient goes from white to black. It "stretches" to fill the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    94
     * painted region. Thus, if I use this <code>Painter</code> to paint a 500 x 500
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    95
     * region, the far left would be black, the far right would be white, and a smooth
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    96
     * gradient would be painted between. I could then, without modification, reuse the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    97
     * <code>Painter</code> to paint a region that is 20x20 in size. This region would
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    98
     * also be black on the left, white on the right, and a smooth gradient painted
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    99
     * between.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   100
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   101
     * @param g The Graphics2D to render to. This must not be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   102
     * @param object an optional configuration parameter. This may be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   103
     * @param width width of the area to paint.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   104
     * @param height height of the area to paint.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   105
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   106
    public void paint(Graphics2D g, T object, int width, int height);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   107
}