Last active
March 10, 2019 12:20
-
-
Save ezhov-da/d8bf745c3518a591f9a476fa2a42323b to your computer and use it in GitHub Desktop.
java прогресс бар в виде радуги
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/240546/remove-html-tags-from-a-string | |
[code:]java[:code] | |
import java.awt.BasicStroke; | |
import java.awt.Color; | |
import java.awt.GradientPaint; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Insets; | |
import java.awt.RenderingHints; | |
import javax.swing.BorderFactory; | |
import javax.swing.JComponent; | |
import javax.swing.JProgressBar; | |
import javax.swing.border.Border; | |
import javax.swing.plaf.basic.BasicProgressBarUI; | |
/** | |
* Отображение прогресс бара с помощью градиента двух цветов | |
* <p> | |
* @author ezhov_da | |
*/ | |
public class RainbowProgressBarUI extends BasicProgressBarUI | |
{ | |
protected GradientPaint gradPaint; | |
protected Color defaultColorMin = new Color(50, 205, 50); | |
protected Color defaultColorMax = new Color(178, 34, 34); | |
protected Color userColorMin; | |
protected Color userColorMax; | |
protected Border defaultBorder = BorderFactory.createLineBorder(Color.GRAY); | |
protected Border userBorder; | |
public RainbowProgressBarUI() | |
{ | |
} | |
public RainbowProgressBarUI(Color userColorMin, Color userColorMax) | |
{ | |
this.userColorMin = userColorMin; | |
this.userColorMax = userColorMax; | |
} | |
public RainbowProgressBarUI(Border userBorder) | |
{ | |
this.userBorder = userBorder; | |
} | |
public RainbowProgressBarUI(Color userColorMin, Color userColorMax, Border userBorder) | |
{ | |
this.userColorMin = userColorMin; | |
this.userColorMax = userColorMax; | |
this.userBorder = userBorder; | |
} | |
protected void paintDeterminate(Graphics g, JComponent c) | |
{ | |
if (!(g instanceof Graphics2D)) | |
{ | |
return; | |
} | |
Insets b = progressBar.getInsets(); // area for border | |
int barRectWidth = progressBar.getWidth() - (b.right + b.left); | |
int barRectHeight = progressBar.getHeight() - (b.top + b.bottom); | |
if (barRectWidth <= 0 || barRectHeight <= 0) | |
{ | |
return; | |
} | |
int cellLength = getCellLength(); | |
int cellSpacing = getCellSpacing(); | |
// amount of progress to draw | |
int amountFull = getAmountFull(b, barRectWidth, barRectHeight); | |
//СОБСТВЕННАЯ ВСТАВКА------------------------------------------- | |
Graphics2D g2 = (Graphics2D) g; | |
paintComponentUserSettings(g2, c); | |
//-------------------------------------------------------------- | |
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) | |
{ | |
// draw the cells | |
if (cellSpacing == 0 && amountFull > 0) | |
{ | |
// draw one big Rect because there is no space between cells | |
g2.setStroke(new BasicStroke((float) barRectHeight, | |
BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); | |
} else | |
{ | |
// draw each individual cell | |
g2.setStroke(new BasicStroke((float) barRectHeight, | |
BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, | |
0.f, new float[] | |
{ | |
cellLength, cellSpacing | |
}, 0.f)); | |
} | |
if (true) | |
{ | |
g2.drawLine(b.left, (barRectHeight / 2) + b.top, | |
amountFull + b.left, (barRectHeight / 2) + b.top); | |
}//УБРАНА ELSE ИЗ ОРИГИНАЛЬНОГО КОДА | |
} | |
// Deal with possible text painting | |
if (progressBar.isStringPainted()) | |
{ | |
paintString(g, b.left, b.top, | |
barRectWidth, barRectHeight, | |
amountFull, b); | |
} | |
} | |
protected void paintComponentUserSettings(Graphics2D g2, JComponent c) | |
{ | |
if (userColorMin != null && userColorMax != null) | |
{ | |
paintColor(g2, userColorMin, userColorMax, c); | |
} else | |
{ | |
paintColor(g2, defaultColorMin, defaultColorMax, c); | |
} | |
if (defaultBorder != null) | |
{ | |
progressBar.setBorder(userBorder); | |
} else | |
{ | |
progressBar.setBorder(defaultBorder); | |
} | |
} | |
private void paintColor(Graphics2D g2, Color min, Color max, JComponent c) | |
{ | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
gradPaint = new GradientPaint(0, 0, min, (c.getWidth()), 0, max, true); | |
g2.setPaint(gradPaint); | |
} | |
} | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment