vendredi 5 février 2021

Color generator in java swing using Math.random

How do you make a random color generator in java swing using Math.random()? Here how far I got:

import javax.swing.*;

import java.awt.; import java.awt.event.;

public class GUI implements ActionListener {

//window
JFrame window = new JFrame("Colors");
JPanel panel = new JPanel();
JButton button = new JButton("New Color");


//constructor
public GUI () {

    button.addActionListener(this);
    panel.add(button);
    window.add(panel);
    window.setSize(300,300);
    window.setVisible(true);

}


public void actionPerformed (ActionEvent e) {
int num1;
num1 = (Math.random()*255);
  
    panel.setBackground(Color.num1);
    //see if you can use Math.random() to choose a random color each time
    
}

}




Aucun commentaire:

Enregistrer un commentaire