Eae pessoal.
Meu problema é o seguinte: estou implementando um novo look and feel.
Sobrescrevi o método paint do meu delegate e ficou ok.
O “desenho” do botão fica meio diferente quando o botão esta “clickado”, com o “mouseover”, ou desabilitado (só para dar um efeito heheeh)
Por exemplo, quando o mouse está em cima do botão eu coloco uma bordinha amarela. Quando o mouse sai de cima volta ao normal.
package example;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import com.sun.java.swing.plaf.windows.WindowsButtonUI;
public class MyButtonUI extends WindowsButtonUI {
private boolean isClicked = false;
private boolean isOver = false;
private final static MyButtonUI buttonUI = new MyButtonUI();
public MyButtonUI() {
}
public static ComponentUI createUI(JComponent c) {
return buttonUI;
}
// BasicButtonUI overrides
// ----------------------------------------------------------------------
public void installUI(JComponent c) {
super.installUI(c);
// toda vez que chama um button ele vem alterado por esta cofigurações
// abaixo
final AbstractButton button;
button = (AbstractButton) c;
button.setOpaque(false);
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
button.repaint();
}
public void mouseEntered(MouseEvent e) {
isOver = true;
button.repaint();
}
public void mouseExited(MouseEvent e) {
isOver = false;
button.repaint();
}
public void mousePressed(MouseEvent e) {
isClicked = true;
isOver = false;
button.repaint();
}
public void mouseReleased(MouseEvent e) {
isClicked = false;
isOver = true;
button.repaint();
}
});
}
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c); // Declaracoes
int textWidth = 0;
int textHeight = 0;
int panelWidth = 0;
int panelHeight = 0;
int xDR = 0;
int yDR = 0;
AbstractButton button = null;
button = (AbstractButton) c;
Font fontVerdana = new Font("Verdana", Font.BOLD, 10);
int w = button.getWidth();
int h = button.getHeight();
Graphics2D g2 = (Graphics2D) g;
// Coloca um retangulo para ficar como fundo
Rectangle2D rectFundo = new Rectangle2D.Double(0, 0, w, h);
g2.setPaint(Color.black);
g2.draw(rectFundo);
g2.setPaint(g2.getBackground());
g2.fill(rectFundo);
// Faz um retangulo com os cantos aredondados
RoundRectangle2D rr2d = new RoundRectangle2D.Double(1.0, 1.0, w - 1,
h - 1, 10.0, 10.0);
Map<Object, Object> mapa = new HashMap<Object, Object>();
mapa.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(mapa);
g2.draw(rr2d);
if (button.isEnabled()){
if (isClicked) {
g2.setPaint(Color.black);
g2.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
// paint inner border
g2.setPaint(Color.lightGray);
g2.drawRoundRect(1, 1, w - 3, h - 3, 10, 10);
// Pinta o gradiente
g2.setPaint(new GradientPaint(1, 1, new Color(117, 142, 206), 1,
h - 1, new Color(145, 191, 251), false));
g2.fillRoundRect(2, 2, w - 4, h - 4, 10, 10);
}
if (isOver) {
g2.setPaint(Color.black);
g2.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
// paint inner border
g2.setPaint(Color.orange);
g2.drawRoundRect(1, 1, w - 3, h - 3, 10, 10);
// Pinta o gradiente
g2.setPaint(new GradientPaint(1, h - 1, new Color(117, 142, 206),
1, 1, new Color(145, 191, 251), false));
g2.fillRoundRect(2, 2, w - 4, h - 4, 10, 10);
}
if (!isClicked && !isOver) {
g2.setPaint(Color.black);
g2.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
// Pinta o gradiente
g2.setPaint(new GradientPaint(1, h - 1, new Color(117, 142, 206),
1, 1, new Color(145, 191, 251), false));
g2.fillRoundRect(1, 1, w - 2, h - 2, 10, 10);
}
} else {
g2.setPaint(Color.black);
g2.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
g2.setPaint(new Color(186, 208, 252));
g2.fillRoundRect(1, 1, w - 2, h - 2, 10, 10);
}
// Acha o tamanho da fonte do componente
FontMetrics fm = g2.getFontMetrics(fontVerdana);
java.awt.geom.Rectangle2D rectText = fm.getStringBounds(button
.getText(), g2);
textWidth = (int) (rectText.getWidth());
textHeight = (int) (rectText.getHeight());
panelWidth = button.getWidth();
panelHeight = button.getHeight();
// Faz os calculos para centralizar a string no componentes
xDR = (panelWidth - textWidth) / 2;
if (button.getHeight() == 25) {
yDR = 16;
} else {
yDR = ((panelHeight - textHeight) / 3) + (textHeight);
}
g2.setFont(fontVerdana);
g2.setPaint(Color.WHITE);
g2.drawString(button.getText(), xDR, yDR);
}
}
Mas quando existem mais botões na tela e eu passo o mouse por cima de um botão, o efeito acontece em todos os botões!! Não somente para o botão que eu to com o mouse em cima. Alguém sabe como resolver?
Obs.: Eu estou sobrescrevendo o Look and Feel do windows.
Valeu!