java中怎样实现渐变色进度条
1个回答
2016-12-09 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
代码如下:
package test;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class test extends JFrame {
int current = 0;
public static void main(String[] args) {
new test().setVisible(true);
}
public test() {
JPanel contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setBounds(0, 0, 400, 400);
final JProgressBar pbMusic = new JProgressBar();
pbMusic.setBounds(16, 38, 332, 4);
pbMusic.setValue(current);
pbMusic.setMaximum(100);
pbMusic.setUI(new GradientProgressBarUI());
pbMusic.setBorderPainted(false);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
pbMusic.setValue(current++);
}
}, 0, 1000);
contentPane.add(pbMusic);
setLocation(400, 200);
setSize(400, 400);
setVisible(true);
}
public class GradientProgressBarUI extends BasicProgressBarUI {
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
Graphics2D graphics2d = (Graphics2D) g;
Insets b = progressBar.getInsets();
// JProgressBar的边界区域
int width = progressBar.getWidth();
int height = progressBar.getHeight();
int barRectWidth = width - (b.right + b.left);
int barRectHeight = height - (b.top + b.bottom);
int arcSize = height / 2 - 1;
int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
//已完成的进度
graphics2d.setColor(Color.WHITE);
graphics2d.fillRoundRect(0, 0, width - 1, height, arcSize,
arcSize);
//绘制JProgressBar的背景
//用GradientPaint类来实现渐变色显示进度
//设置了开始点和终止点,并设置好这两个点的颜色,系统会自动在这两个点中用渐变色来填充
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(amountFull - 1, barRectHeight - 1);
//这里设置的终止点是当前已经完成的进度的那个点
GradientPaint gradientPaint = new GradientPaint(
start, new Color(114,243,233), end, new Color(233,243,123));
graphics2d.setPaint(gradientPaint);
graphics2d.fillRoundRect(b.left, b.top, amountFull - 1,
barRectHeight, arcSize, arcSize);//这里实现的是圆角的效果将arcSize调成0即可实现矩形效果
}
}
}
package test;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class test extends JFrame {
int current = 0;
public static void main(String[] args) {
new test().setVisible(true);
}
public test() {
JPanel contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setBounds(0, 0, 400, 400);
final JProgressBar pbMusic = new JProgressBar();
pbMusic.setBounds(16, 38, 332, 4);
pbMusic.setValue(current);
pbMusic.setMaximum(100);
pbMusic.setUI(new GradientProgressBarUI());
pbMusic.setBorderPainted(false);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
pbMusic.setValue(current++);
}
}, 0, 1000);
contentPane.add(pbMusic);
setLocation(400, 200);
setSize(400, 400);
setVisible(true);
}
public class GradientProgressBarUI extends BasicProgressBarUI {
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
Graphics2D graphics2d = (Graphics2D) g;
Insets b = progressBar.getInsets();
// JProgressBar的边界区域
int width = progressBar.getWidth();
int height = progressBar.getHeight();
int barRectWidth = width - (b.right + b.left);
int barRectHeight = height - (b.top + b.bottom);
int arcSize = height / 2 - 1;
int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
//已完成的进度
graphics2d.setColor(Color.WHITE);
graphics2d.fillRoundRect(0, 0, width - 1, height, arcSize,
arcSize);
//绘制JProgressBar的背景
//用GradientPaint类来实现渐变色显示进度
//设置了开始点和终止点,并设置好这两个点的颜色,系统会自动在这两个点中用渐变色来填充
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(amountFull - 1, barRectHeight - 1);
//这里设置的终止点是当前已经完成的进度的那个点
GradientPaint gradientPaint = new GradientPaint(
start, new Color(114,243,233), end, new Color(233,243,123));
graphics2d.setPaint(gradientPaint);
graphics2d.fillRoundRect(b.left, b.top, amountFull - 1,
barRectHeight, arcSize, arcSize);//这里实现的是圆角的效果将arcSize调成0即可实现矩形效果
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询