Jogo de Degradê (GradientPaint) e Cor simples em Banckground de JPanel

3 respostas
F

Senhores,

Eu tenho um JPanel que originalmente tem um degradê. Quero que ao clicar num botão, este mesmo Jpanel passe a ter a cor Vermelha (cor simples e normal). Como eu faço isso???

O degradê feito da seguinte classe:

public class Degrade extends JPanel {

int a1, a2, w1, w2;
    public Degrade(int a1, int a2, int w1, int w2){
        this.a1 = a1;
        this.a2 = a2;
        this.w1 = w1;
        this.w2 = w2;
    }

    @Override
    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;

        //GradientPaint gp = new GradientPaint(20.0f, 70.0f, new Color(192, 230, 249), 20.0f,350.0f, Color.white);
        GradientPaint gp = new GradientPaint(a1, a2, new Color(192, 230, 249), w1,w2, Color.white);
        g2.setPaint(gp);
        g2.fillRect(this.WIDTH, this.HEIGHT, this.getWidth(), getHeight());
    }
}

O Jpanel foi feito assim:
JPanel panel = new br.edu.sgbi.classes.Degrade(50,115,32,35);

Quero que o panel vire vermelho no evento de um botão. Mas não adianta afazer isso: panel.setBackground(Color.red);

Como fazer então??? Como ficar vermelho ???

3 Respostas

V

Coloque uma variável boolean no seu painel. Se for true, faça o método paint pintar de vermelho. Se for false, faça pintar do degradee.
Adicione um método para alterar o valor desse boolean no seu painel. Altere o valor da variável e chame repaint().

No evento de botão, altere o valor da variável boolean.

Ao postar códigos, use a tag code:
http://www.guj.com.br/java/50115-voce-e-novo-no-guj-vai-criar-um-topico-e-colar-seu-codigo-fonte-leia-aqui-antes-por-favor

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class Degrade extends JPanel {

	private static final long serialVersionUID = 1652536198355981460L;
	
	private int x1, y1, x2, y2;
	private boolean degradee = true;

	public Degrade(int x1, int y1, int x2, int y2) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	
	public void setDegradee(boolean degradee) {
		this.degradee = degradee;
		repaint();
	}
	
	public boolean isDegradee() {
		return degradee;
	}
	
	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g.create();

		if (degradee)
		{
			g2.setPaint(new GradientPaint(x1, y1, new Color(192, 230, 249), 
					x2, y2, Color.white));			
		}
		else
		{
			g2.setColor(Color.RED);			
		}
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.dispose();
	}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class DegradeFrame {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Degradee");
		frame.setSize(800,600);
		frame.setLocationRelativeTo(null);
		final Degrade pnlDegrade = new Degrade(50,115,32,35);
		frame.add(pnlDegrade, BorderLayout.CENTER);
		JButton btnTrocar = new JButton("Trocar");
		btnTrocar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				pnlDegrade.setDegradee(!pnlDegrade.isDegradee());
			}
		});
		frame.add(btnTrocar, BorderLayout.SOUTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
}
F

Obrigado, cara! Funcionou Legal!
Mas quando o degradê muda, aparece uma pequenas bordas (não contínuas e irregulares) no JPanel. Como se algo por detrais desse Panel tivesse sendo feito algo que era salientado. Você sabe o que é isso? É algo pequeno, talvez ninguém perceba.

Obrigado!

V

Pode postar um screenshot?

Criado 6 de janeiro de 2012
Ultima resposta 7 de jan. de 2012
Respostas 3
Participantes 2