4343import java .awt .Graphics ;
4444import java .awt .Graphics2D ;
4545import java .awt .Insets ;
46+ import java .awt .Paint ;
47+ import java .awt .PaintContext ;
4648import java .awt .Point ;
4749import java .awt .Rectangle ;
4850import java .awt .RenderingHints ;
4951import java .awt .Shape ;
5052import java .awt .geom .AffineTransform ;
5153import java .awt .geom .Rectangle2D ;
5254import java .awt .geom .RoundRectangle2D ;
55+ import java .awt .image .ColorModel ;
5356
5457/**
5558 * The actual progress bar implementation. Heavily inspired by {@link com.intellij.ide.ui.laf.darcula.ui.DarculaProgressBarUI}
@@ -95,6 +98,7 @@ protected void paintIndeterminate(Graphics graphics, JComponent component) {
9598 Color endColor ;
9699 Color foreground = progressBar .getForeground ();
97100 Object statusProperty = progressBar .getClientProperty (ProgressBarUtil .STATUS_KEY );
101+
98102 if (ProgressBarUtil .FAILED_VALUE .equals (statusProperty ) || foreground == JBUI .CurrentTheme .ProgressBar .FAILED ) {
99103 startColor = JBUI .CurrentTheme .ProgressBar .FAILED ;
100104 endColor = JBUI .CurrentTheme .ProgressBar .FAILED_END ;
@@ -169,8 +173,9 @@ protected void paintDeterminate(Graphics graphics, JComponent component) {
169173
170174 Shape fullShape ;
171175 Shape coloredShape ;
172- int orientation = progressBar .getOrientation ();
173- if (orientation == SwingConstants .HORIZONTAL ) {
176+ boolean horizontalOrientation = progressBar .getOrientation () == SwingConstants .HORIZONTAL ;
177+
178+ if (horizontalOrientation ) {
174179 int pHeight = getStripeWidth ();
175180 int yOffset = r .y + (r .height - pHeight ) / 2 ;
176181
@@ -188,19 +193,23 @@ protected void paintDeterminate(Graphics graphics, JComponent component) {
188193
189194 Color foreground = progressBar .getForeground ();
190195 Object statusProperty = progressBar .getClientProperty (ProgressBarUtil .STATUS_KEY );
191- if (ProgressBarUtil .FAILED_VALUE .equals (statusProperty ) || foreground == JBUI .CurrentTheme .ProgressBar .FAILED ) {
192- graphics .setColor (JBUI .CurrentTheme .ProgressBar .FAILED );
196+
197+ if (progressBar .getClientProperty (ProgressBarUtil .PROGRESS_PAINT_KEY ) instanceof Paint progressPaint ) {
198+ PaintTransformer paint = new PaintTransformer (progressPaint , horizontalOrientation , horizontalOrientation ? r .x : r .y , amountFull );
199+ graphics2D .setPaint (paint );
200+ } else if (ProgressBarUtil .FAILED_VALUE .equals (statusProperty ) || foreground == JBUI .CurrentTheme .ProgressBar .FAILED ) {
201+ graphics2D .setColor (JBUI .CurrentTheme .ProgressBar .FAILED );
193202 } else if (ProgressBarUtil .PASSED_VALUE .equals (statusProperty ) || foreground == JBUI .CurrentTheme .ProgressBar .PASSED ) {
194- graphics .setColor (JBUI .CurrentTheme .ProgressBar .PASSED );
203+ graphics2D .setColor (JBUI .CurrentTheme .ProgressBar .PASSED );
195204 } else if (ProgressBarUtil .WARNING_VALUE .equals (statusProperty ) || foreground == JBUI .CurrentTheme .ProgressBar .WARNING ) {
196- graphics .setColor (JBUI .CurrentTheme .ProgressBar .WARNING );
205+ graphics2D .setColor (JBUI .CurrentTheme .ProgressBar .WARNING );
197206 } else {
198207 graphics2D .setColor (JBUI .CurrentTheme .ProgressBar .PROGRESS );
199208 }
200209 graphics2D .fill (coloredShape );
201210
202211 ImageIcon icon = PedroIcons .getScaledIcon ();
203- if (orientation == SwingConstants . HORIZONTAL ) {
212+ if (horizontalOrientation ) {
204213 icon .paintIcon (progressBar , graphics2D , amountFull , r .y );
205214 } else {
206215 icon .paintIcon (progressBar , graphics2D , r .x , amountFull );
@@ -280,4 +289,35 @@ private void paintString(Graphics2D graphics2D, int x, int y, int w, int h, int
280289 }
281290 graphics2D .setClip (oldClip );
282291 }
283- }
292+
293+ private record PaintTransformer (Paint originalPaint , boolean isHorizontal , int start , int size ) implements Paint {
294+
295+ @ Override
296+ public PaintContext createContext (ColorModel cm , Rectangle deviceBounds , Rectangle2D userBounds , AffineTransform xform , RenderingHints hints ) {
297+ AffineTransform transform = xform == null ? new AffineTransform () : (AffineTransform ) xform .clone ();
298+
299+ if (start != 0 ) {
300+ if (isHorizontal ) {
301+ transform .translate (start , 0 );
302+ } else {
303+ transform .translate (0 , start );
304+ }
305+ }
306+
307+ if (size > 0 ) {
308+ transform .scale (size , size );
309+ }
310+
311+ if (!isHorizontal ) {
312+ transform .rotate (Math .toRadians (90 ), 0 , 0 );
313+ }
314+
315+ return originalPaint .createContext (cm , deviceBounds , userBounds , transform , hints );
316+ }
317+
318+ @ Override
319+ public int getTransparency () {
320+ return originalPaint .getTransparency ();
321+ }
322+ }
323+ }
0 commit comments