src/share/classes/javax/swing/plaf/nimbus/SynthPainterImpl.java

Print this page




  32 import javax.swing.*;
  33 import javax.swing.plaf.synth.SynthContext;
  34 import javax.swing.plaf.synth.SynthPainter;
  35 import javax.swing.plaf.synth.SynthConstants;
  36 
  37 import javax.swing.Painter;
  38 
  39 
  40 class SynthPainterImpl extends SynthPainter {
  41     private NimbusStyle style;
  42 
  43     SynthPainterImpl(NimbusStyle style) {
  44         this.style = style;
  45     }
  46 
  47     /**
  48      * Paint the provided painter using the provided transform at the specified
  49      * position and size. Handles if g is a non 2D Graphics by painting via a
  50      * BufferedImage.
  51      */
  52     private void paint(Painter p, SynthContext ctx, Graphics g, int x, int y,
  53                        int w, int h, AffineTransform transform) {
  54         if (p != null) {
  55             if (g instanceof Graphics2D){
  56                 Graphics2D gfx = (Graphics2D)g;
  57                 if (transform!=null){
  58                     gfx.transform(transform);
  59                 }
  60                 gfx.translate(x, y);
  61                 p.paint(gfx, ctx.getComponent(), w, h);
  62                 gfx.translate(-x, -y);
  63                 if (transform!=null){
  64                     try {
  65                         gfx.transform(transform.createInverse());
  66                     } catch (NoninvertibleTransformException e) {
  67                         // this should never happen as we are in control of all
  68                         // calls into this method and only ever pass in simple
  69                         // transforms of rotate, flip and translates
  70                         e.printStackTrace();
  71                     }
  72                 }


  79                 if (transform!=null){
  80                     gfx.transform(transform);
  81                 }
  82                 p.paint(gfx, ctx.getComponent(), w, h);
  83                 gfx.dispose();
  84                 g.drawImage(img,x,y,null);
  85                 img = null;
  86             }
  87         }
  88     }
  89 
  90     private void paintBackground(SynthContext ctx, Graphics g, int x, int y,
  91                                  int w, int h, AffineTransform transform) {
  92         // if the background color of the component is 100% transparent
  93         // then we should not paint any background graphics. This is a solution
  94         // for there being no way of turning off Nimbus background painting as
  95         // basic components are all non-opaque by default.
  96         Component c = ctx.getComponent();
  97         Color bg = (c != null) ? c.getBackground() : null;
  98         if (bg == null || bg.getAlpha() > 0){
  99             Painter backgroundPainter = style.getBackgroundPainter(ctx);

 100             if (backgroundPainter != null) {
 101                 paint(backgroundPainter, ctx, g, x, y, w, h,transform);
 102             }
 103         }
 104     }
 105 
 106     private void paintForeground(SynthContext ctx, Graphics g, int x, int y,
 107                                  int w, int h, AffineTransform transform) {
 108         Painter foregroundPainter = style.getForegroundPainter(ctx);
 109         if (foregroundPainter != null) {
 110             paint(foregroundPainter, ctx, g, x, y, w, h,transform);
 111         }
 112     }
 113 
 114     private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w,
 115                              int h, AffineTransform transform) {
 116         Painter borderPainter = style.getBorderPainter(ctx);
 117         if (borderPainter != null) {
 118             paint(borderPainter, ctx, g, x, y, w, h,transform);
 119         }
 120     }
 121 
 122     private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {
 123         Component c = ctx.getComponent();
 124         boolean ltr = c.getComponentOrientation().isLeftToRight();
 125         // Don't RTL flip JSpliders as they handle it internaly
 126         if (ctx.getComponent() instanceof JSlider) ltr = true;
 127 
 128         if (orientation == SwingConstants.VERTICAL && ltr) {
 129             AffineTransform transform = new AffineTransform();
 130             transform.scale(-1, 1);
 131             transform.rotate(Math.toRadians(90));
 132             paintBackground(ctx, g, y, x, h, w, transform);
 133         } else if (orientation == SwingConstants.VERTICAL) {
 134             AffineTransform transform = new AffineTransform();
 135             transform.rotate(Math.toRadians(90));
 136             transform.translate(0,-(x+w));




  32 import javax.swing.*;
  33 import javax.swing.plaf.synth.SynthContext;
  34 import javax.swing.plaf.synth.SynthPainter;
  35 import javax.swing.plaf.synth.SynthConstants;
  36 
  37 import javax.swing.Painter;
  38 
  39 
  40 class SynthPainterImpl extends SynthPainter {
  41     private NimbusStyle style;
  42 
  43     SynthPainterImpl(NimbusStyle style) {
  44         this.style = style;
  45     }
  46 
  47     /**
  48      * Paint the provided painter using the provided transform at the specified
  49      * position and size. Handles if g is a non 2D Graphics by painting via a
  50      * BufferedImage.
  51      */
  52     private void paint(Painter<Object> p, SynthContext ctx, Graphics g, int x, int y,
  53                        int w, int h, AffineTransform transform) {
  54         if (p != null) {
  55             if (g instanceof Graphics2D){
  56                 Graphics2D gfx = (Graphics2D)g;
  57                 if (transform!=null){
  58                     gfx.transform(transform);
  59                 }
  60                 gfx.translate(x, y);
  61                 p.paint(gfx, ctx.getComponent(), w, h);
  62                 gfx.translate(-x, -y);
  63                 if (transform!=null){
  64                     try {
  65                         gfx.transform(transform.createInverse());
  66                     } catch (NoninvertibleTransformException e) {
  67                         // this should never happen as we are in control of all
  68                         // calls into this method and only ever pass in simple
  69                         // transforms of rotate, flip and translates
  70                         e.printStackTrace();
  71                     }
  72                 }


  79                 if (transform!=null){
  80                     gfx.transform(transform);
  81                 }
  82                 p.paint(gfx, ctx.getComponent(), w, h);
  83                 gfx.dispose();
  84                 g.drawImage(img,x,y,null);
  85                 img = null;
  86             }
  87         }
  88     }
  89 
  90     private void paintBackground(SynthContext ctx, Graphics g, int x, int y,
  91                                  int w, int h, AffineTransform transform) {
  92         // if the background color of the component is 100% transparent
  93         // then we should not paint any background graphics. This is a solution
  94         // for there being no way of turning off Nimbus background painting as
  95         // basic components are all non-opaque by default.
  96         Component c = ctx.getComponent();
  97         Color bg = (c != null) ? c.getBackground() : null;
  98         if (bg == null || bg.getAlpha() > 0){
  99             
 100             Painter<Object> backgroundPainter = style.getBackgroundPainter(ctx);
 101             if (backgroundPainter != null) {
 102                 paint(backgroundPainter, ctx, g, x, y, w, h,transform);
 103             }
 104         }
 105     }
 106 
 107     private void paintForeground(SynthContext ctx, Graphics g, int x, int y,
 108                                  int w, int h, AffineTransform transform) {
 109         Painter<Object> foregroundPainter = style.getForegroundPainter(ctx);
 110         if (foregroundPainter != null) {
 111             paint(foregroundPainter, ctx, g, x, y, w, h,transform);
 112         }
 113     }
 114 
 115     private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w,
 116                              int h, AffineTransform transform) {
 117         Painter<Object> borderPainter = style.getBorderPainter(ctx);
 118         if (borderPainter != null) {
 119             paint(borderPainter, ctx, g, x, y, w, h,transform);
 120         }
 121     }
 122 
 123     private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {
 124         Component c = ctx.getComponent();
 125         boolean ltr = c.getComponentOrientation().isLeftToRight();
 126         // Don't RTL flip JSpliders as they handle it internaly
 127         if (ctx.getComponent() instanceof JSlider) ltr = true;
 128 
 129         if (orientation == SwingConstants.VERTICAL && ltr) {
 130             AffineTransform transform = new AffineTransform();
 131             transform.scale(-1, 1);
 132             transform.rotate(Math.toRadians(90));
 133             paintBackground(ctx, g, y, x, h, w, transform);
 134         } else if (orientation == SwingConstants.VERTICAL) {
 135             AffineTransform transform = new AffineTransform();
 136             transform.rotate(Math.toRadians(90));
 137             transform.translate(0,-(x+w));