1

我正在尝试创建一个如下所示的按钮,并不断淡入和淡出。它看起来像:-

在此处输入图像描述

现在我已经完成了渐变油漆的外观,但是我应该怎么做才能使按钮文本出现。尽管调用了“super(s)”,但它并没有出现,因为我用 GradientPaint 对其进行了绘制。我应该怎么做文字出现在油漆上。我的代码如下所示:-

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class Fader extends JFrame{
Fader()
{
    super("A fading button");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    setSize(400,400);

    add(new CustomButton("Submit"));
    setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){public void run(){new Fader();}});
}
}

class CustomButton extends JButton
{
public CustomButton(String s) {
    super(s);
    // TODO Auto-generated constructor stub
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g.create();
    GradientPaint gp=new GradientPaint(0, 0, Color.RED, 200, 100, Color.YELLOW);
    g2.setPaint(gp);
    g2.fillRect(0, 0, getWidth(), getHeight());
}
public Dimension getPreferredSize()
{
    return new Dimension(200,100);
}
}

其次,还要求提供实现淡入淡出效果的建议。

4

3 回答 3

3

您可以使用此选项,在组件上绘制透明颜色渐变:

@Override
public void paintComponent(Graphics g){
    super.paintComponent( g );

    Graphics2D g2=(Graphics2D)g.create();
    int h = getHeight();
    int w = getWidth();

    g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, .5f));
    g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red));
    g2.fillRect(0, 0, w, h);

    g2.dispose();
}

在此处输入图像描述

其他很好的淡入示例(根据要求)。我用过RadialGradientPaint。你可以玩AlphaComposite

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));

其中4f代表透明级别 40%

@Override
public void paintComponent(Graphics g){
    super.paintComponent( g );

    Graphics2D g2=(Graphics2D)g.create();
    int h = getHeight();
    int w = getWidth();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));


    Point2D center = new Point2D.Float(100, 50);
    float radius = 150;
    float[] dist = {0.0f, 1.0f};

    Color[] colors = {Color.yellow, Color.red};
    RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors);
    g2.setPaint(p);     

    g2.fillRect(0, 0, w, h);

    g2.dispose();       

}

在此处输入图像描述

最后我们可以动态地使用 alpha。她是完整的代码。我创建了简单的线程,将我的 alpha 从 0 更改为 9,反之亦然。开始了:

public class Fader extends JFrame{
private static final long serialVersionUID = 1L;
static JButton button;

public static float mTransparent = .0f;

Fader(){
    super("A fading button");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    setSize(400,400);

    JButton button = new CustomButton("Submit");

    add(button);
    setVisible(true);

    Blink blink = new Blink(this);
    blink.start();
}

public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable(){public void run(){new Fader();}});
}

public static float getTransparentLevel() {
    return mTransparent;
}

public  void setTransparentLevel(float newVal) {
    mTransparent = newVal;

    if(button != null){
        button.repaint();

    }
    repaint();


}
}


class Blink extends Thread{

Fader fader;

public Blink(Fader fader) {
    this.fader = fader;
}

@Override
public void run(){

    while(true){        

        if(Fader.getTransparentLevel() == 0.0f){                
            //increase to 1f
            for(int i=1; i<10; i++){
                fader.setTransparentLevel((float)i/10);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }               
        }
        else if(Fader.getTransparentLevel() == 0.9f){
            //increase to 1f
            for(int i=10; i>=0; i--){
                fader.setTransparentLevel((float)i/10);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }       
    }       
}
}


class CustomButton extends JButton {

private static final long serialVersionUID = 1L;
public CustomButton(String s) {
    super(s);       
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent( g );

    Graphics2D g2=(Graphics2D)g.create();
    int h = getHeight();
    int w = getWidth();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Fader.getTransparentLevel()));


    Point2D center = new Point2D.Float(100, 50);
    float radius = 150;
    float[] dist = {0.0f, 1.0f};

    Color[] colors = {Color.yellow, Color.red};
    RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors);
    g2.setPaint(p);     
    g2.fillRect(0, 0, w, h);
    g2.dispose();       

}

public Dimension getPreferredSize(){
    return new Dimension(200,100);
}
}

它在睡眠 300 毫秒时闪烁,从 .0 到 .9 透明,然后从 0.9 到 .0:

在此处输入图像描述--> 在此处输入图像描述

于 2012-10-27T09:45:59.467 回答
2

覆盖该paintComponent()方法后,您就可以自己绘制按钮了。因此,您必须自己绘制文本。这样的事情会有所帮助:

g2.setColor(Color.GREEN);
g2.drawString(getText(), 0, 10);

上面的代码必须加在fillRect方法后面。但是,您必须使用FontMetrics才能根据文本对齐首选项定位文本。

于 2012-10-27T09:18:21.677 回答
1

要淡入和淡出,您需要实现Animation Sequencer在不同线程中运行的自己的,这将不断改变 alpha 值TimerTask。一旦 alpha 的值达到0,它应该递增回100%

还可以看看 Romain Guy 的书:Filthy Rich Java Clients

于 2012-10-27T09:35:55.850 回答