Dégradé dans un contentPane
Bonjour,
J'ai un JDialog où je souhaiterais que le fond soit un dégradé.
Code:
1 2 3 4 5 6 7 8 9 10
| public void paint(Graphics g)
{
super.paint(g);
Color backgroundEdge = new Color(74, 91, 159);
Color backgroundCenter = new Color(99, 146, 193);
Graphics2D g2 = (Graphics2D) this.getContentPane().getGraphics();
g2.setPaint(new GradientPaint(0, 0, backgroundCenter, 0, this.getHeight(), backgroundEdge, true));
} |
J'ai aussi essayé :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class LightBox extends JDialog
{
private static final long serialVersionUID = -6631531322633376093L;
public LightBox(JFrame parent, boolean modal, JPanel jPanel)
{
super(parent, "", modal);
this.setSize(jPanel.getWidth() + 2 * 5, jPanel.getHeight() + 2 * 5);
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.setResizable(false);
Color backgroundEdge = new Color(74, 91, 159);
Color backgroundCenter = new Color(99, 146, 193);
this.getContentPane().setBackground(Color.RED);
/*
Graphics2D g2 = (Graphics2D) this.getContentPane().getGraphics();
g2.setPaint(new GradientPaint(0, 0, backgroundEdge, 0, this.getHeight(), backgroundCenter, true));
/**/
this.setVisible(true);
this.repaint();
}
[...]
} |
Le problème est que j'ai remarqué que si je mettais le setBackground après le setVisible, la couleur n'était pas prise en compte. Je le met donc avant, ainsi que pour le Graphics2D.
Mais si je fais appel à Graphics2D avant le setVisible il renvoie un null. Je dois donc le faire avant pour qu'il soit pris en compte, mais dans ce cas l'objet renvoyé est null !
Comment puis je faire pour que l'arrière plan de JDialog soit un dégradé ?
D'avance merci pour votre aide.
xenos