Java 如何根据百分比使用抽绳方法更改范围像素的颜色

Java 如何根据百分比使用抽绳方法更改范围像素的颜色,java,swing,Java,Swing,我目前在我的程序中使用了一个splashscreen,然后我在上面显示信息,就像文件的下载百分比一样。我喜欢JProgressBarstring,我的意思是,当蓝色进度经过字符串上方时,字符串的颜色会改变 这里是我目前正在使用的代码 private static void splashProgress(float percent, String str) { if (mySplash != null && mySplash.isVisible()) { //

我目前在我的程序中使用了一个splashscreen,然后我在上面显示信息,就像文件的下载百分比一样。我喜欢
JProgressBar
string,我的意思是,当蓝色进度经过字符串上方时,字符串的颜色会改变

这里是我目前正在使用的代码

private static void splashProgress(float percent, String str)
{
    if (mySplash != null && mySplash.isVisible())
    {   // important to check here so no other methods need to know if there
        // really is a Splash being displayed
        float width = (186*percent)/100;
        splashProgress = new Rectangle2D.Double(4., 211., width, 24.);
        splashTextArea = new Rectangle2D.Double(width+4., 211., 186.-width, 24.);

        splashGraphics.setColor(new Color(255,177,100,255));
        splashGraphics.fill(splashProgress);

        splashGraphics.setColor(new Color(175,25,25,255));
        splashGraphics.fill(splashTextArea);
        splashGraphics.setColor(new Color(25,25,25,255));
        splashGraphics.drawString(str, 4,226);


        // make sure it's displayed
        mySplash.update();
    }
}
进度条如下所示:

如您所见,字符串的颜色与进度条的颜色相同,我希望将隐藏部分的颜色设置为不同的颜色


谢谢你的帮助,很抱歉我的英语不好,这不是我的母语。

你使用的是
Graphics2D\setXORMode


这是我尝试过的,但正如你在下面看到的,结果并不是我所期望的:D

字符串消失,progressbar无法正确重新加载自身。 这是我的代码,如果你知道什么是错的

private static void splashProgress(float percent, String str)
{
    if (mySplash != null && mySplash.isVisible())
    {   // important to check here so no other methods need to know if there
        // really is a Splash being displayed
        float width = (186*percent)/100;
        splashProgress = new Rectangle2D.Double(4., 211., width, 24.);
        splashTextArea = new Rectangle2D.Double(width+4., 211., 186.-width, 24.);

        splashGraphics.setColor(new Color(255,177,100,255));
        splashGraphics.fill(splashProgress);

        splashGraphics.setColor(new Color(175,25,25,255));
        splashGraphics.fill(splashTextArea);

        splashGraphics.setXORMode(new Color(175,25,25,255));
        splashGraphics.drawString(str, 4,226);

        // make sure it's displayed
        mySplash.update();
    }
}
这个方法就是从那里调用的

while ((read = input.read(buffer)) > 0){
            writeFile.write(buffer, 0, read);
            writeFile.flush();
            percent = (percent + read);
            percentText = (int) ((percent*100)/length);
            splashProgress((percent*100)/length, "Téléchargement : "+percentText+" %");
        }
不过,非常感谢您的帮助:)

编辑:我试图
setXorMode
在我的while循环之外,现在我得到的就是这个

编辑2:好的,我解决了我的问题,事实上,由于我的循环出现了冲突,所以我只添加了
splashGraphics.setPaintMode()用于绘制矩形,然后
splashGraphics.setXORMode(c)为我的文字,它的作品完美


非常感谢您的帮助和您的反应。

我相信您需要设置
Graphics
context的
XORMode
。感谢您的快速回答,XORMode看起来不错,但我不知道如何使用它。我只是在谷歌上搜索了一下:p
while ((read = input.read(buffer)) > 0){
            writeFile.write(buffer, 0, read);
            writeFile.flush();
            percent = (percent + read);
            percentText = (int) ((percent*100)/length);
            splashProgress((percent*100)/length, "Téléchargement : "+percentText+" %");
        }