[java]classe GradientPaint

john_matrix Messages postés 443 Statut Membre -  
 Utilisateur anonyme -
bonjour voila je voudrai faire un degradé dans un JPanel et j'utilise pour cela la classe GradientPaint.
Mais apres je sais pas du tou comment faire

Color c1 = new Color(0.807F,1F,0F);
Color c2 = new Color(255,204,102);
float a = 0;
float b = 100;
GradientPaint gp = new GradientPaint(a,a,c1,b,b,c2,true);

Voila si vous avez une idee a partir de ca comment metre le Jpanel avec le GRadientPaint merci

2 réponses

  1. Utilisateur anonyme
     
    Re-salut!

    Le code ci-dessus ne fonctionne pas correctement.

    Crée plutôt les 3 classes suivantesdans le même package:
    - MainView
    - GradientPanel
    - ColorChoicePanel

    et copies-y le code ci-dessous.

    Démarre la démo à partir de MainView.

    MainView
    package panelDegrade;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.HeadlessException;
    
    import javax.swing.JFrame;
    
    public class MainView extends JFrame {
    	private Container c;
    
    	private GradientPanel gradientPanel;
    
    	public MainView() throws HeadlessException {
    		super("Démo de dégradé");
    		initialize();
    	}
    
    	private void initialize() {
    		c = getContentPane();
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		c.setLayout(new BorderLayout());
    	}
    
    	public void setColorChoicePanel(ColorChoicePanel colorchoicePanel) {
    		c.add(colorchoicePanel, BorderLayout.NORTH);
    	}
    
    	public void setGradientPanel(GradientPanel gradientPanel) {
    		this.gradientPanel = gradientPanel;
    		c.add(gradientPanel, BorderLayout.CENTER);
    	}
    
    	public void setColor1(Color c1) {
    		gradientPanel.setColor1(c1);
    	}
    
    	public void setColor2(Color c2) {
    		gradientPanel.setColor2(c2);
    	}
    
    	public static void main(String[] args) {
    		MainView demo = new MainView();
    		ColorChoicePanel colorChoicePanel = new ColorChoicePanel(demo);
    		GradientPanel gradientPanel = new GradientPanel();
    		gradientPanel.setPreferredSize(new Dimension(640, 480));
    		demo.setColorChoicePanel(colorChoicePanel);
    		demo.setGradientPanel(gradientPanel);
    		demo.pack();
    		demo.setVisible(true);
    	}
    }
    
    


    GradientPanel
    package panelDegrade;
    
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class GradientPanel extends JPanel {
    	private Color color1;
    	private Color color2;
    	
    
    	public GradientPanel( ) {
    		this.color1 = Color.black;
    		this.color2 = Color.black;
    		this.add(new JLabel("Hello Gradient Panel!"));
    		this.add(new JTextField("Un champ de texte"));
    	}
    
    	public void setColor1(Color c1) {
    		this.color1 = c1;
    		repaint();
    	}
    
    	public void setColor2(Color c2) {
    		this.color2 = c2;
    		repaint();
    	}
    	
    	public void paint(Graphics g){
    		super.paint(g);
    		//
    	}
    
    	protected void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D) g;
    		super.paintComponent(g);
    		int w = getWidth();
    		int h = getHeight();
    
    		GradientPaint gradient = new GradientPaint(0, 0, color1, w, h, color2, true);
    		g2.setPaint(gradient);
    		g2.fillRect(0, 0, w, h);
    		repaint();
    	}
    }
    
    


    ColorChoicePanel
    package panelDegrade;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    
    public class ColorChoicePanel extends JPanel implements ListCellRenderer {
    	private MainView view;
    
    	private JComboBox jcb_color_1;
    
    	private JComboBox jcb_color_2;
    
    	public ColorChoicePanel(MainView view) {
    		super();
    		this.view = view;
    		initialize();
    	}
    
    	private void initialize() {
    		GradientColor[] colors = { new GradientColor("Noir", 0, 0, 0), new GradientColor("Blanc", 255, 255, 255),
    				new GradientColor("Rouge", 255, 0, 0), new GradientColor("Vert", 0, 255, 0), new GradientColor("Bleu", 0, 0, 255) };
    		DefaultComboBoxModel dcbm_colors_1 = new DefaultComboBoxModel(colors);
    		DefaultComboBoxModel dcbm_colors_2 = new DefaultComboBoxModel(colors);
    		jcb_color_1 = new JComboBox(dcbm_colors_1);
    		jcb_color_1.setRenderer(this);
    		jcb_color_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JComboBox combo = (JComboBox) e.getSource();
    				Color c = (Color) combo.getSelectedItem();
    				view.setColor1(c);
    			}
    		});
    
    		jcb_color_2 = new JComboBox(dcbm_colors_2);
    		jcb_color_2.setRenderer(this);
    		jcb_color_2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JComboBox combo = (JComboBox) e.getSource();
    				Color c = (Color) combo.getSelectedItem();
    				view.setColor2(c);
    			}
    		});
    		
    
    		add(jcb_color_1);
    		add(jcb_color_2);
    	}
    
    	class GradientColor extends Color {
    		private String colorName;
    
    		public GradientColor(String colorName, int r, int g, int b) {
    			super(r, g, b);
    			this.colorName = colorName;
    		}
    
    		public String toString() {
    			return colorName;
    		}
    	}
    
    	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    		JLabel label = null;
    		if (value instanceof GradientColor) {
    			GradientColor gradientColor = (GradientColor) value;
    			label = new JLabel();
    			label.setText(gradientColor.toString());
    			label.setForeground(gradientColor);
    			list.add(label);
    		}
    		return label;
    	}
    }
    
    


    ;-)
    HackTrack
    1
  2. Utilisateur anonyme
     
    Salut!

    package panelDegrade;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class GradientPanel extends JPanel {
    
    	private Color color1;
    
    	private Color color2;
    
    	public GradientPanel() {
    		this(Color.blue, Color.green);
    	}
    
    	public GradientPanel(Color c1, Color c2) {
    		super();
    		this.color1 = c1;
    		this.color2 = c2;
    	}
    
    	public void setColor1(Color c1) {
    		this.color1 = c1;
    		repaint();
    	}
    
    	public void setColor2(Color c2) {
    		this.color2 = c2;
    		repaint();
    	}
    
    	// Overloaded in order to paint the background
    	protected void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D) g;
    
    		int w = getWidth();
    		int h = getHeight();
    
    		GradientPaint gradient = new GradientPaint(0, 0, color1, w, h, color2, true);
    		g2.setPaint(gradient);
    		g2.fillRect(0, 0, w, h);
    	}
    
    	public static void main(String[] args) {
    
    		final GradientPanel pGradient = new GradientPanel();
    
    		Color[] colors = { createColor("Black", Color.black), createColor("Blue", Color.blue), createColor("Green", Color.green),
    				createColor("yellow", Color.yellow), createColor("orange", Color.orange), createColor("red", Color.red),
    				createColor("white", Color.white) };
    
    		JComboBox c1 = new JComboBox(colors);
    		c1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JComboBox combo = (JComboBox) e.getSource();
    				Color c = (Color) combo.getSelectedItem();
    				pGradient.setColor1(c);
    			}
    		});
    
    		JComboBox c2 = new JComboBox(colors);
    		c2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JComboBox combo = (JComboBox) e.getSource();
    				Color c = (Color) combo.getSelectedItem();
    				pGradient.setColor2(c);
    			}
    		});
    
    		JPanel pColors = new JPanel(new GridLayout(0, 2));
    		pColors.add(c1);
    		pColors.add(c2);
    
    		pGradient.add(new JButton("A button"));
    		pGradient.add(new JTextField("A text field"));
    
    		c1.setSelectedItem(colors[1]);
    		c2.setSelectedItem(colors[2]);
    
    		JFrame f = new JFrame("Gradient test");
    		f.setSize(300, 200);
    		f.getContentPane().add(pColors, BorderLayout.NORTH);
    		f.getContentPane().add(pGradient, BorderLayout.CENTER);
    		f.setVisible(true);
    	}
    
    	private static Color createColor(String name, Color c) {
    
    		final String colorname = name;
    		Color color = new Color(c.getRed(), c.getGreen(), c.getBlue()) {
    			private String name = colorname;
    
    			public String toString() {
    				return name;
    			}
    		};
    		return color;
    	}
    }
    


    ;-)
    HackTrack
    0