Olá! Uma vez eu usei degrade só pra brincar.
Achei um um site de uns indianos o seguinte código mas não estou achando o link.
Segue o código para um JPanel. É só mudar as cores.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* CurvedGradientePanel.java
*
* Created on 18/03/2009, 13:03:10
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import javax.swing.*;
/**
*
* @author Marcilio
*/
public class CurvedGradientePanel extends JPanel {
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
private int roundRectSize = 15;
/**
* The start color of the gradient
*/
private Color startColor = new Color(114, 166, 252);
/**
* The end color of the gradient
*/
private Color endColor = new Color(255, 255, 255);
/**
* The direction of the gradient
*/
private int direction = VERTICAL;
// ------------------------------------------------------------------------------------------------------------------
// Constructors and getter/setter methods
// ------------------------------------------------------------------------------------------------------------------
/**
* Create a default SimpleGradientPanel instance.
*/
public CurvedGradientePanel() {
super();
initComponents();
startColor = Color.BLUE;
endColor = Color.white;
direction = 3;
roundRectSize = 0;
}
/**
* Create a SimpleGradientPanel with the given start and end colors.
*
* @param aStart
* The start color for the gradient.
* @param aEnd
* The end color for the gradient.
*/
public CurvedGradientePanel(Color aStart, Color aEnd) {
super();
startColor = aStart;
endColor = aEnd;
}
/**
* Create a SimpleGradientPanel with the given start and end colors.
*
* @param aStart
* The start color for the gradient.
* @param aEnd
* The end color for the gradient.
* @param aDirection
* The direction of the gradient.
*/
public CurvedGradientePanel(Color aStart, Color aEnd, int aDirection) {
super();
startColor = aStart;
endColor = aEnd;
direction = aDirection;
}
/**
* Create a SimpleGradientPanel with the given start and end colors.
*
* @param aStart
* The start color for the gradient.
* @param aEnd
* The end color for the gradient.
* @param aDirection
* The direction of the gradient.
* @param aRoundRectSise
* The rounded size of the Rectangle.
*/
public CurvedGradientePanel(Color aStart, Color aEnd, int aDirection,
int aRoundRectSise) {
super();
startColor = aStart;
endColor = aEnd;
direction = aDirection;
roundRectSize = aRoundRectSise;
}
/**
* Get the ending color of the gradient.
*/
public Color getEndColor() {
return endColor;
}
/**
* Set the ending color to use.
*
* @param aColor
* The color to use.
*/
public void setEndColor(Color aColor) {
Color oldEndColor = endColor;
endColor = aColor;
super.firePropertyChange("endColor", oldEndColor, endColor);
repaint();
}
/**
* Get the start color of the gradient
*/
public Color getStartColor() {
return startColor;
}
/**
* Set the starting color.
*
* @param aColor
* The color to use
*/
public void setStartColor(Color aColor) {
Color oldStartColor = endColor;
startColor = aColor;
super.firePropertyChange("startColor", oldStartColor, startColor);
repaint();
}
/**
* Get the direction (vertical or horizontal) of the gradient.
*/
public int getDirection() {
return direction;
}
/**
* Set the direction
*
* @param aDirection
* The direction of the gradient
*/
public void setDirection(int aDirection) {
int oldDirection = direction;
direction = aDirection;
super.firePropertyChange("direction", oldDirection, aDirection);
repaint();
}
/**
* Reposiciona os botoes de controle toda vez que alterar o tamanho do Form
*/
public void reposition(){
this.repaint();
}
// ------------------------------------------------------------------------------------------------------------------
// Custom painting methods
// ------------------------------------------------------------------------------------------------------------------
/**
* Override the default paintComponent method to paint the gradient in the
* panel.
*
* @param g
*/
public void paintComponent(Graphics g) {
Dimension dim = getSize();
Graphics2D g2 = (Graphics2D) g;
Insets inset = getInsets();
int vWidth = dim.width - (inset.left + inset.right);
int vHeight = dim.height - (inset.top + inset.bottom);
if (direction == HORIZONTAL) {
paintHorizontalGradient(g2, inset.left, inset.top, vWidth, vHeight,
dim.width);
} else {
paintVerticalGradient(g2, inset.left, inset.top, vWidth, vHeight,
dim.height);
}
}
/**
* Paints a vertical gradient background from the start color to the end
* color.
*/
private void paintVerticalGradient(Graphics2D g2d, int x, int y, int w,
int h, int height) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint p1;
GradientPaint p2;
GradientPaint GP = new GradientPaint(0, 0, startColor, 0, h, endColor,
true);
g2d.setPaint(GP);
g2d.setPaint(Color.BLACK);
g2d.drawRoundRect(0, 0, w - 1, h - 1, roundRectSize, roundRectSize);
g2d.setPaint(GP);
g2d.fillRoundRect(1, 1, w - 2, h - 2, roundRectSize, roundRectSize);
g2d.setPaint(Color.WHITE);
g2d.drawRoundRect(1, 1, w - 3, h - 3, roundRectSize, roundRectSize);
g2d.setPaint(new Color(255, 255, 255, 100));
GeneralPath path = new GeneralPath();
path.moveTo(-5, 1);
path.quadTo(w / 2, h / 2, w, 0);
path.closePath();
g2d.fill(path);
}
/**
* Paints a horizontal gradient background from the start color to the end
* color.
*/
private void paintHorizontalGradient(Graphics2D g2d, int x, int y, int w,
int h, int width) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint p1;
GradientPaint p2;
GradientPaint GP = new GradientPaint(0, 0, startColor, w, 0, endColor,
true);
g2d.setPaint(GP);
g2d.setPaint(Color.BLACK);
g2d.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
g2d.setPaint(GP);
g2d.fillRoundRect(1, 1, w - 2, h - 2, 10, 10);
g2d.setPaint(new Color(255, 255, 255, 100));
GeneralPath path = new GeneralPath();
path.moveTo(-5, 1);
path.quadTo(w / 2, h / 2, w, 0);
path.closePath();
g2d.fill(path);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
gradienteRoundRecButton1 = new br.orquestra.testes.GradienteRoundRecButton();
gradienteRoundRecButton3 = new br.orquestra.testes.GradienteRoundRecButton();
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
setLayout(null);
gradienteRoundRecButton1.setText("-");
add(gradienteRoundRecButton1);
gradienteRoundRecButton1.setBounds(440, 40, 40, 25);
gradienteRoundRecButton3.setText("x");
add(gradienteRoundRecButton3);
gradienteRoundRecButton3.setBounds(480, 40, 50, 25);
}// </editor-fold>
private void formComponentResized(java.awt.event.ComponentEvent evt) {
reposition();
System.out.println("resize");
}
public static void main(String [] args)
{
JFrame jf = new JFrame();
jf.add(new CurvedGradientePanel());
jf.setVisible(true);
}
// Variables declaration - do not modify
private br.orquestra.testes.GradienteRoundRecButton gradienteRoundRecButton1;
private br.orquestra.testes.GradienteRoundRecButton gradienteRoundRecButton3;
// End of variables declaration
}
Botão Gradiente:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* GradienteRoundRecButton.java
*
* Created on 18/03/2009, 14:00:35
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author JDhilsukh
* Copiado em http://jroller.com/DhilshukReddy/resource/CustomButtons/GradientRoundRectButton.java
*
* Arredondar os JButton's
*/
public class GradienteRoundRecButton extends JButton {
private static boolean REPAINT_SHADOW = true;
private Color COLOR1 = new Color(255, 255, 255);
private Color COLOR2 = new Color(115, 201, 29);
private int outerRoundRectSize = 8;
private int innerRoundRectSize = 6;
private Color unselectedLow; // Lowlight for unselected tab
private Color unselectedHigh; // Highlight for unselected tab
private Color unselectedMid;
private Color selectedLow = new Color(204, 67, 0);// Lowlight for selected tab
private Color selectedHigh = Color.WHITE; // Highlight for selected tab
public GradienteRoundRecButton() {
super();
setContentAreaFilled(false);
setText("");
setBorderPainted(false);
unselectedLow = Color.BLACK;
unselectedMid = new Color(0x330099);
unselectedHigh = Color.WHITE;
setForeground(Color.WHITE);
setFont(new Font("Thoma", Font.BOLD, 12));
setFocusable(false);
setContentAreaFilled(false);
}
public GradienteRoundRecButton(String text) {
super();
setContentAreaFilled(false);
setText(text);
setBorderPainted(false);
unselectedLow = Color.BLACK;
unselectedMid = new Color(0x330099);
unselectedHigh = Color.WHITE;
setForeground(Color.WHITE);
setFont(new Font("Thoma", Font.BOLD, 12));
setFocusable(false);
setContentAreaFilled(false);
}
public GradienteRoundRecButton(Color color1, Color color2,
int outerRoundRectSize, int innerRoundRectSize) {
super();
COLOR1 = color1;
COLOR2 = color2;
this.outerRoundRectSize = outerRoundRectSize;
this.innerRoundRectSize = innerRoundRectSize;
setFont(new Font("Thoma", Font.BOLD, 12));
setForeground(Color.BLACK);
setFocusable(false);
setContentAreaFilled(false);
setBorderPainted(false);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
int h = getHeight();
int w = getWidth();
float tran = 0.1f + 1 * 0.9f;
GradientPaint GP = new GradientPaint(0, 0, COLOR1, 0, h, COLOR2, true);
g2d.setPaint(GP);
ButtonModel model = getModel();
if (!model.isEnabled()) {
GP = new GradientPaint(0, 0, COLOR1, 0, h, COLOR2, true);
} else if (model.isRollover()) {
GP = new GradientPaint(0, 0, COLOR1, 0, h, COLOR2, true);
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint p1;
GradientPaint p2;
if (getModel().isPressed()) {
p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, h - 1,
new Color(100, 100, 100));
p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, h - 3,
new Color(255, 255, 255, 100));
} else {
p1 = new GradientPaint(0, 0, new Color(100, 100, 100), 0, h - 1,
new Color(0, 0, 0));
p2 = new GradientPaint(0, 1, new Color(255, 255, 255, 100), 0,
h - 3, new Color(0, 0, 0, 50));
}
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tran));
RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1,
h - 1, outerRoundRectSize, outerRoundRectSize);
Shape clip = g2d.getClip();
g2d.clip(r2d);
g2d.fillRect(0, 0, w, h);
g2d.setClip(clip);
g2d.setPaint(p1);
g2d.drawRoundRect(0, 0, w - 1, h - 1, outerRoundRectSize,
outerRoundRectSize);
g2d.setPaint(p2);
g2d.drawRoundRect(1, 1, w - 3, h - 3, innerRoundRectSize,
innerRoundRectSize);
// g2d.dispose();
_drawRect(g2d, 0, w, unselectedHigh, unselectedMid, unselectedLow);
g2d.setPaint(null);
super.paintComponent(g);
}
private void _drawRect(Graphics2D g2, int x, int w, Color hiCol,
Color mdCol, Color loCol) {
Dimension d = getSize();
// Divide height into quarters
int h = (int) d.getHeight();
int th = h / 4;
GradientPaint p1;
GradientPaint p2;
ButtonModel model = getModel();
// Paint shaded area
if (!model.isEnabled()) {
p1 = new GradientPaint(0, 0, new Color(192, 192, 192), 0, th,
new Color(192, 192, 192));
p2 = new GradientPaint(0, 4, new Color(192, 192, 192), 0, h - th
- 4, new Color(192, 192, 192));
} else {
if (model.isRollover()) {
p1 = new GradientPaint(0, 0, new Color(255, 143, 89), 0, th,
selectedHigh);
p2 = new GradientPaint(0, 4, selectedHigh, 0, h - th - 4,
new Color(255, 143, 89));
} else {
p1 = new GradientPaint(0, 0, loCol, 0, th, hiCol);
p2 = new GradientPaint(0, 4, hiCol, 0, h - th - 4, loCol);
}
}
g2.setPaint(p1);
g2.fillRoundRect(1, 1, w - 2, th, 6, 6);
g2.setPaint(p2);
g2.fillRect(1, th, w - 2, h - th - 1);
if (model.isPressed()) {
p1 = new GradientPaint(0, 0, selectedLow, 0, th, selectedHigh);
p2 = new GradientPaint(0, 4, selectedHigh, 0, h - th - 4,
selectedLow);
g2.setPaint(p1);
g2.fillRoundRect(1, 1, w - 2, th, 6, 6);
g2.setPaint(p2);
g2.fillRect(1, th, w - 2, h - th - 1);
}
//g2.fillRect(1,th,w-2,h-th-1);
}
/**
* This method sets the Actual Background Color of the Button
*/
public void setStartColor(Color color) {
COLOR1 = color;
}
/**
* This method sets the Pressed Color of the Button
*/
public void setEndColor(Color pressedColor) {
COLOR2 = pressedColor;
}
/**
* This method sets the OuterRoundRect Size of the Button
*/
public void setOuterRoundRectSize(int outerRoundRectSize) {
this.outerRoundRectSize = outerRoundRectSize;
}
/**
* This method sets the InnerRoundRect Size of the Button
*/
public void setInnerRoundRectSize(int innerRoundRectSize) {
this.innerRoundRectSize = innerRoundRectSize;
}
/**
* @return Starting Color of the Button
*/
public Color getStartColor() {
return COLOR1;
}
/**
* @return Ending Color of the Button
*/
public Color getEndColor() {
return COLOR2;
}
/**
* @return the OuterRoundRect Size of the Button
*/
public int getOuterRoundRectSize() {
return this.outerRoundRectSize;
}
/**
* @return the InnerRoundRect Size of the Button
*/
public int getInnerRoundRectSize() {
return this.innerRoundRectSize;
}
public static void main(String args[]) {
try {
//UIManager.setLookAndFeel(new NimbusLookAndFeel());
//UIManager.setLookAndFeel(new SubstanceLookAndFeel());
//SubstanceLookAndFeel.setSkin(new NebulaSkin());
} catch (Exception e) {
// TODO: handle exception
}
JFrame frame = new JFrame("Custom Buttons Demo");
frame.setLayout(new FlowLayout());
GradienteRoundRecButton standardButton = new GradienteRoundRecButton(
"Standard Button");
frame.add(standardButton.getButtonsPanel());
frame.getContentPane().setBackground(Color.WHITE);
frame.setBackground(Color.WHITE);
frame.setSize(700, 85);
frame.setVisible(true);
}
public JPanel getButtonsPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
GradienteRoundRecButton standardButton = new GradienteRoundRecButton(
"Standard Button");
standardButton.setPreferredSize(new Dimension(130, 28));
GradienteRoundRecButton rollOverButton = new GradienteRoundRecButton(
"RollOver Button");
rollOverButton.setPreferredSize(new Dimension(130, 28));
GradienteRoundRecButton disabledButton = new GradienteRoundRecButton(
"Disable Button");
disabledButton.setPreferredSize(new Dimension(130, 28));
disabledButton.setEnabled(false);
GradienteRoundRecButton pressedButton = new GradienteRoundRecButton(
"Pressed Button");
pressedButton.setPreferredSize(new Dimension(130, 28));
panel.add(standardButton);
panel.add(rollOverButton);
panel.add(disabledButton);
panel.add(pressedButton);
return panel;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
…
acabo de ver que o link da fonte está no código
http://jroller.com/DhilshukReddy/resource/CustomButtons/GradientRoundRectButton.java