Preciso construir um applet que solicite três números para as configurações RGB e, em seguida, mostre a cor como pano de fundo.
Fiz esse código mas na hora em que peço para configurar está dando erro. Alguém saberia onde está o erro?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Cores extends Applet implements ActionListener {
Label lr, lg, lb, pr;
TextField tr, tg, tb;
Button b1;
Graphics gr;
int r,g,b; //r = red g = green b = blue //
Cores c;
public void init()
{
lr = new Label("Vermelho");
add(lr);
tr = new TextField(" ");
add(tr);
lg = new Label("Verde");
add(lg);
tg = new TextField(" ");
add(tg);
lb = new Label ("Azul");
add(lb);
tb = new TextField(" ");
add(tb);
b1 = new Button("Configure a cor");
add(b1);
pr = new Label(" ");
add(pr);
b1.addActionListener(this);
}
public void paint(Graphics gr) {
gr.setColor(c);
gr.fillRect(200, 200 , 200, 200);
}
public void actionPerformed(ActionEvent e) {
r = Integer.parseInt(tr.getText());
g = Integer.parseInt(tg.getText());
b = Integer.parseInt(tb.getText());
if((r > 255) || (g > 255) || (b > 255)) {
pr.setLocation(200,50);
pr.setText("Entre com os valores de 0 a 255");
}
else {
pr.setText(" ");
c = new Cores(r,g,b);
}
repaint();
}
}