synthpainterimpl.java

来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 1,571 行 · 第 1/5 页

JAVA
1,571
字号
/* * @(#)SynthPainterImpl.java	1.8 08/05/29 * * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.nimbus;import java.awt.*;import java.awt.geom.AffineTransform;import java.awt.geom.NoninvertibleTransformException;import java.awt.image.BufferedImage;import java.util.*;import javax.swing.*;import javax.swing.plaf.synth.SynthContext;import javax.swing.plaf.synth.SynthPainter;import javax.swing.plaf.synth.SynthConstants;import com.sun.java.swing.Painter;/** * * @author rbair */class SynthPainterImpl extends SynthPainter {    private NimbusStyle style;    SynthPainterImpl(NimbusStyle style) {        this.style = style;    }    /**     * Paint the provided painter using the provided transform at the specified     * position and size. Handles if g is a non 2D Graphics by painting via a     * BufferedImage.     */    private void paint(Painter p, SynthContext ctx, Graphics g, int x, int y,                       int w, int h, AffineTransform transform) {        if (p != null) {            if (g instanceof Graphics2D){                Graphics2D gfx = (Graphics2D)g;                if (transform!=null){                    gfx.transform(transform);                }                gfx.translate(x, y);                p.paint(gfx, ctx.getComponent(), w, h);                gfx.translate(-x, -y);                if (transform!=null){                    try {                        gfx.transform(transform.createInverse());                    } catch (NoninvertibleTransformException e) {                        // this should never happen as we are in control of all                        // calls into this method and only ever pass in simple                        // transforms of rotate, flip and translates                        e.printStackTrace();                    }                }            } else {                // use image if we are printing to a Java 1.1 PrintGraphics as                // it is not a instance of Graphics2D                BufferedImage img = new BufferedImage(w,h,                        BufferedImage.TYPE_INT_ARGB);                Graphics2D gfx = img.createGraphics();                if (transform!=null){                    gfx.transform(transform);                }                p.paint(gfx, ctx.getComponent(), w, h);                gfx.dispose();                g.drawImage(img,x,y,null);                img = null;            }        }    }    private void paintBackground(SynthContext ctx, Graphics g, int x, int y,                                 int w, int h, AffineTransform transform) {        // if the background color of the component is 100% transparent        // then we should not paint any background graphics. This is a solution        // for there being no way of turning off Nimbus background painting as        // basic components are all non-opaque by default.        Component c = ctx.getComponent();        Color bg = (c != null) ? c.getBackground() : null;        if (bg == null || bg.getAlpha() > 0){            Painter backgroundPainter = style.getBackgroundPainter(ctx);            if (backgroundPainter != null) {                paint(backgroundPainter, ctx, g, x, y, w, h,transform);            }        }    }    private void paintForeground(SynthContext ctx, Graphics g, int x, int y,                                 int w, int h, AffineTransform transform) {        Painter foregroundPainter = style.getForegroundPainter(ctx);        if (foregroundPainter != null) {            paint(foregroundPainter, ctx, g, x, y, w, h,transform);        }    }    private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w,                             int h, AffineTransform transform) {        Painter borderPainter = style.getBorderPainter(ctx);        if (borderPainter != null) {            paint(borderPainter, ctx, g, x, y, w, h,transform);        }    }    private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        // Don't RTL flip JSpliders as they handle it internaly        if (ctx.getComponent() instanceof JSlider) ltr = true;        if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintBackground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0,-(x+w));            paintBackground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintBackground(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            AffineTransform transform = new AffineTransform();            transform.translate(x,y);            transform.scale(-1, 1);            transform.translate(-w,0);            paintBackground(ctx, g, 0, 0, w, h, transform);        }    }    private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintBorder(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0, -(x + w));            paintBorder(ctx, g, y, 0, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintBorder(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            paintBorder(ctx, g, x, y, w, h, null);        }    }    private void paintForeground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintForeground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0, -(x + w));            paintForeground(ctx, g, y, 0, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintForeground(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            paintForeground(ctx, g, x, y, w, h, null);        }    }    /**     * Paints the background of an arrow button. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintArrowButtonBackground(SynthContext context,                                           Graphics g, int x, int y,                                           int w, int h) {        if (context.getComponent().getComponentOrientation().isLeftToRight()){            paintBackground(context, g, x, y, w, h, null);        } else {            AffineTransform transform = new AffineTransform();            transform.translate(x,y);            transform.scale(-1, 1);            transform.translate(-w,0);            paintBackground(context, g, 0, 0, w, h, transform);        }    }    /**     * Paints the border of an arrow button. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintArrowButtonBorder(SynthContext context,                                       Graphics g, int x, int y,                                       int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }    /**     * Paints the foreground of an arrow button. This method is responsible     * for drawing a graphical representation of a direction, typically     * an arrow. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     * @param direction One of SwingConstants.NORTH, SwingConstants.SOUTH     *                  SwingConstants.EAST or SwingConstants.WEST     */    public void paintArrowButtonForeground(SynthContext context,                                           Graphics g, int x, int y,                                           int w, int h,                                           int direction) {        //assume that the painter is arranged with the arrow pointing... LEFT?        String compName = context.getComponent().getName();        boolean ltr = context.getComponent().                getComponentOrientation().isLeftToRight();        // The hard coding for spinners here needs to be replaced by a more        // general method for disabling rotation        if ("Spinner.nextButton".equals(compName) ||                "Spinner.previousButton".equals(compName)) {            if (ltr){                paintForeground(context, g, x, y, w, h, null);            } else {                AffineTransform transform = new AffineTransform();                transform.translate(w, 0);                transform.scale(-1, 1);                paintForeground(context, g, x, y, w, h, transform);            }        } else if (direction == SwingConstants.WEST) {            paintForeground(context, g, x, y, w, h, null);        } else if (direction == SwingConstants.NORTH) {            if (ltr){                AffineTransform transform = new AffineTransform();                transform.scale(-1, 1);                transform.rotate(Math.toRadians(90));                paintForeground(context, g, y, 0, h, w, transform);            } else {                AffineTransform transform = new AffineTransform();                transform.rotate(Math.toRadians(90));                transform.translate(0, -(x + w));                paintForeground(context, g, y, 0, h, w, transform);            }        } else if (direction == SwingConstants.EAST) {            AffineTransform transform = new AffineTransform();            transform.translate(w, 0);            transform.scale(-1, 1);            paintForeground(context, g, x, y, w, h, transform);        } else if (direction == SwingConstants.SOUTH) {            if (ltr){                AffineTransform transform = new AffineTransform();                transform.rotate(Math.toRadians(-90));                transform.translate(-h, 0);                paintForeground(context, g, y, x, h, w, transform);            } else {                AffineTransform transform = new AffineTransform();                transform.scale(-1, 1);                transform.rotate(Math.toRadians(-90));                transform.translate(-(h+y), -(w+x));                paintForeground(context, g, y, x, h, w, transform);            }        }    }    /**     * Paints the background of a button.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintButtonBackground(SynthContext context,                                      Graphics g, int x, int y,                                      int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }    /**     * Paints the border of a button.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?