Untitled Document
Untitled Document
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
static int randRed = (rand.nextInt(17)+1)*15; //randomized red green and blue values,
multiples of 15
static int randGrn = (rand.nextInt(17)+1)*15;
static int randBlu = (rand.nextInt(17)+1)*15;
public GuessColor() {
initGUI();
System.out.println("SOLUTION: " + randRed + " " + randGrn + " " + randBlu); // This is just
to show what the RGB values are so you can easily solve
setTitle("Match the color!");
pack();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initGUI() { //sets up the frame and functionality of UI
JLabel title = new JLabel("Match The Color!", JLabel.CENTER);
title.setFont(font);
title.setBackground(Color.BLACK);
title.setForeground(Color.WHITE);
title.setOpaque(true);
add(title, BorderLayout.NORTH);
/**BUTTONS**/
//checks if the color panels are the same and displays winning message if they are
//resets game for when user wins and wants to play again
private static void reset() {
randRed = (rand.nextInt(17)+1)*15;
randGrn = (rand.nextInt(17)+1)*15;
randBlu = (rand.nextInt(17)+1)*15;
userRed = 0;
userGrn = 0;
userBlu = 0;
userColor = new Color(userRed, userGrn, userBlu);
new GuessColor();
}
//main method
public static void main(String[] args) {
try {
String laf = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
}
catch (Exception e) {}
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new GuessColor();
}
});
}
}