需要帮助来创建一个自定义按钮

时间:2012-10-27 09:12:28

标签: java swing jbutton custom-component graphics2d

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

enter image description here

现在我已经做了直到用渐变颜料看起来但我应该怎样做才能使按钮文字出现。尽管我称之为'super(s)'但它并没有出现,因为我已经用GradientPaint绘制了它。我该怎么办?确保文本显示在paint.My代码如下所示: -

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);
}
}

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

3 个答案:

答案 0 :(得分: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();
}

enter image description here

其他很好的淡入淡出示例(根据要求)。我用了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();       

}

enter image description here

最后我们可以动态地使用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);
}
}

闪烁,从.0到.9的透明睡眠时间为300毫秒,从.9返回到.0:

enter image description here - &gt; enter image description here

答案 1 :(得分:2)

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

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

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

答案 2 :(得分:1)

要fadeIn和fadeOut,您需要实现自己的Animation Sequencer,该TimerTask在不同的线程中运行,并且会不断地使用0改变alpha值。一旦alpha的值达到100%,它就应该递增回Filthy Rich Java Clients

另请查看Romain Guy的书:{{1}}