Path: blob/master/src/java.desktop/share/classes/sun/awt/SunGraphicsCallback.java
41152 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.awt.*;2829import sun.util.logging.PlatformLogger;3031public abstract class SunGraphicsCallback {32public static final int HEAVYWEIGHTS = 0x1;33public static final int LIGHTWEIGHTS = 0x2;34public static final int TWO_PASSES = 0x4;3536private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.SunGraphicsCallback");3738public abstract void run(Component comp, Graphics cg);3940protected void constrainGraphics(Graphics g, Rectangle bounds) {41if (g instanceof ConstrainableGraphics) {42((ConstrainableGraphics)g).constrain(bounds.x, bounds.y, bounds.width, bounds.height);43} else {44g.translate(bounds.x, bounds.y);45}46g.clipRect(0, 0, bounds.width, bounds.height);47}4849public final void runOneComponent(Component comp, Rectangle bounds,50Graphics g, Shape clip,51int weightFlags) {52if (comp == null || !comp.isDisplayable() || !comp.isVisible()) {53return;54}55boolean lightweight = comp.isLightweight();56if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0) ||57(!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) {58return;59}6061if (bounds == null) {62bounds = comp.getBounds();63}6465if (clip == null || clip.intersects(bounds)) {66Graphics cg = g.create();67try {68constrainGraphics(cg, bounds);69cg.setFont(comp.getFont());70cg.setColor(comp.getForeground());71if (cg instanceof Graphics2D) {72((Graphics2D)cg).setBackground(comp.getBackground());73}74run(comp, cg);75} finally {76cg.dispose();77}78}79}8081public final void runComponents(Component[] comps, Graphics g,82int weightFlags) {83int ncomponents = comps.length;84Shape clip = g.getClip();8586if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) {87Rectangle newrect = clip.getBounds();88log.finer("x = " + newrect.x + ", y = " + newrect.y +89", width = " + newrect.width +90", height = " + newrect.height);91}9293// A seriously sad hack--94// Lightweight components always paint behind peered components,95// even if they are at the top of the Z order. We emulate this96// behavior by making two printing passes: the first for lightweights;97// the second for heavyweights.98//99// ToDo(dpm): Either build a list of heavyweights during the100// lightweight pass, or redesign the components array to keep101// lightweights and heavyweights separate.102if ((weightFlags & TWO_PASSES) != 0) {103for (int i = ncomponents - 1; i >= 0; i--) {104runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS);105}106for (int i = ncomponents - 1; i >= 0; i--) {107runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS);108}109} else {110for (int i = ncomponents - 1; i >= 0; i--) {111runOneComponent(comps[i], null, g, clip, weightFlags);112}113}114}115116public static final class PaintHeavyweightComponentsCallback117extends SunGraphicsCallback118{119private static PaintHeavyweightComponentsCallback instance =120new PaintHeavyweightComponentsCallback();121122private PaintHeavyweightComponentsCallback() {}123public void run(Component comp, Graphics cg) {124if (!comp.isLightweight()) {125comp.paintAll(cg);126} else if (comp instanceof Container) {127runComponents(((Container)comp).getComponents(), cg,128LIGHTWEIGHTS | HEAVYWEIGHTS);129}130}131public static PaintHeavyweightComponentsCallback getInstance() {132return instance;133}134}135public static final class PrintHeavyweightComponentsCallback136extends SunGraphicsCallback137{138private static PrintHeavyweightComponentsCallback instance =139new PrintHeavyweightComponentsCallback();140141private PrintHeavyweightComponentsCallback() {}142public void run(Component comp, Graphics cg) {143if (!comp.isLightweight()) {144comp.printAll(cg);145} else if (comp instanceof Container) {146runComponents(((Container)comp).getComponents(), cg,147LIGHTWEIGHTS | HEAVYWEIGHTS);148}149}150public static PrintHeavyweightComponentsCallback getInstance() {151return instance;152}153}154}155156157