1173N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
1173N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1173N/A *
1173N/A * This code is free software; you can redistribute it and/or modify it
1173N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1173N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1173N/A *
1173N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1173N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1173N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1173N/A * version 2 for more details (a copy is included in the LICENSE file that
1173N/A * accompanied this code).
1173N/A *
1173N/A * You should have received a copy of the GNU General Public License version
1173N/A * 2 along with this work; if not, write to the Free Software Foundation,
1173N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1173N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1173N/A */
1173N/Apackage javax.swing;
1173N/A
1173N/Aimport java.awt.Graphics2D;
1173N/A
1173N/A/**
1173N/A * <p>A painting delegate. The Painter interface defines exactly one method,
1173N/A * <code>paint</code>. It is used in situations where the developer can change
1173N/A * the painting routine of a component without having to resort to subclassing
1173N/A * the component. It is also generically useful when doing any form of painting
1173N/A * delegation.</p>
1173N/A *
1173N/A * <p><code>Painter</code>s are simply encapsulations of Java2D code and make
1173N/A * it fairly trivial to reuse existing <code>Painter</code>s or to combine
1173N/A * them together. Implementations of this interface are also trivial to write,
1173N/A * such that if you can't find a <code>Painter</code> that does what you need,
1173N/A * you can write one with minimal effort. Writing a <code>Painter</code> requires
1173N/A * knowledge of Java2D.</p>
1173N/A *
1173N/A * <p>A <code>Painter</code> may be created with a type parameter. This type will be
1173N/A * expected in the <code>paint</code> method. For example, you may wish to write a
1173N/A * <code>Painter</code> that only works with subclasses of {@link java.awt.Component}.
1173N/A * In that case, when the <code>Painter</code> is declared, you may declare that
1173N/A * it requires a <code>Component</code>, allowing the paint method to be type safe. Ex:
3964N/A * <pre>
3964N/A * {@code
3964N/A * Painter<Component> p = new Painter<Component>() {
3964N/A * public void paint(Graphics2D g, Component c, int width, int height) {
3964N/A * g.setColor(c.getBackground());
3964N/A * //and so forth
1173N/A * }
3964N/A * }
3964N/A * }
3964N/A * </pre></p>
1173N/A *
1173N/A * <p>This interface makes no guarantees of threadsafety.</p>
1173N/A *
1173N/A * @author rbair
1173N/A */
1173N/Apublic interface Painter<T> {
1173N/A /**
1173N/A * <p>Renders to the given {@link java.awt.Graphics2D} object. Implementations
1173N/A * of this method <em>may</em> modify state on the <code>Graphics2D</code>, and are not
1173N/A * required to restore that state upon completion. In most cases, it is recommended
1173N/A * that the caller pass in a scratch graphics object. The <code>Graphics2D</code>
1173N/A * must never be null.</p>
1173N/A *
1173N/A * <p>State on the graphics object may be honored by the <code>paint</code> method,
1173N/A * but may not be. For instance, setting the antialiasing rendering hint on the
1173N/A * graphics may or may not be respected by the <code>Painter</code> implementation.</p>
1173N/A *
1173N/A * <p>The supplied object parameter acts as an optional configuration argument.
1173N/A * For example, it could be of type <code>Component</code>. A <code>Painter</code>
1173N/A * that expected it could then read state from that <code>Component</code> and
1173N/A * use the state for painting. For example, an implementation may read the
1173N/A * backgroundColor and use that.</p>
1173N/A *
1173N/A * <p>Generally, to enhance reusability, most standard <code>Painter</code>s ignore
1173N/A * this parameter. They can thus be reused in any context. The <code>object</code>
1173N/A * may be null. Implementations must not throw a NullPointerException if the object
1173N/A * parameter is null.</p>
1173N/A *
1173N/A * <p>Finally, the <code>width</code> and <code>height</code> arguments specify the
1173N/A * width and height that the <code>Painter</code> should paint into. More
1173N/A * specifically, the specified width and height instruct the painter that it should
1173N/A * paint fully within this width and height. Any specified clip on the
1173N/A * <code>g</code> param will further constrain the region.</p>
1173N/A *
1173N/A * <p>For example, suppose I have a <code>Painter</code> implementation that draws
1173N/A * a gradient. The gradient goes from white to black. It "stretches" to fill the
1173N/A * painted region. Thus, if I use this <code>Painter</code> to paint a 500 x 500
1173N/A * region, the far left would be black, the far right would be white, and a smooth
1173N/A * gradient would be painted between. I could then, without modification, reuse the
1173N/A * <code>Painter</code> to paint a region that is 20x20 in size. This region would
1173N/A * also be black on the left, white on the right, and a smooth gradient painted
1173N/A * between.</p>
1173N/A *
1173N/A * @param g The Graphics2D to render to. This must not be null.
1173N/A * @param object an optional configuration parameter. This may be null.
1173N/A * @param width width of the area to paint.
1173N/A * @param height height of the area to paint.
1173N/A */
1173N/A public void paint(Graphics2D g, T object, int width, int height);
1173N/A}