Question

Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the corresponding

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ColorRange {
  
private static JFrame mainFrame;
private static JPanel mainPanel, inputPanel, buttonPanel;
private static JLabel codeLabel, resLabel;
private static JTextField codeField, resField;
private static JButton submitButton;
  
public static void main(String[] args)
{
mainFrame = new JFrame("Color Range");
mainPanel = new JPanel(new GridLayout(2, 0));
  
inputPanel = new JPanel(new GridLayout(2, 2));
codeLabel = new JLabel("Enter a color code: ");
codeField = new JTextField(10);
resLabel = new JLabel("Color: ");
resField = new JTextField(10);
resField.setEditable(false);
inputPanel.add(codeLabel);
inputPanel.add(codeField);
inputPanel.add(resLabel);
inputPanel.add(resField);
  
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
submitButton = new JButton("Submit");
buttonPanel.add(submitButton);
  
mainPanel.add(inputPanel);
mainPanel.add(buttonPanel);
  
mainFrame.add(mainPanel);
mainFrame.setSize(300, 150);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
  
// listener for submit button
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(codeField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please enter a color code");
return;
}
  
String color;
int code = Integer.parseInt(codeField.getText().trim());
if(code >= 380 && code < 450)
color = "Violet";
else if(code >= 451 && code < 495)
color = "Blue";
else if(code >= 496 && code < 570)
color = "Green";
else if(code >= 571 && code < 590)
color = "Yellow";
else if(code >= 591 && code < 620)
color = "Orange";
else if(code >= 621 && code < 750)
color = "Red";
else
color = "Unknown";
  
resField.setText(color);
}
});
}
}

***************************************************************** SCREENSHOT **********************************************************

Color Range - O X Enter a color code: 630 Color: Red Submit

Add a comment
Know the answer?
Add Answer to:
Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.. As the wavelength of light increases, the frequency of light Group of answer choices a....

    1.. As the wavelength of light increases, the frequency of light Group of answer choices a. Stays the same b. increases c. decreases 2.. A typical AM radio station broadcasts at a frequency of 1070. MHz. What is the wavelength of the electromagnetic radiation generated by this radio station? 1 Hz = 1 s-1 1 MHz = 106 Hz Group of answer choices a. 3.21 x 10^11 m b. 2.804 X 10 ^5 m c. 0.2804 m 3.. Use this...

  • Write in coral please QUESTION 7 5 points Save Answer Complete the program below to convert...

    Write in coral please QUESTION 7 5 points Save Answer Complete the program below to convert water temperatures from degrees Celsius to degrees Fahrenheit. Then, warn the swimmers if the water temperature is too cold or too hot. If the water temperature is 77 degrees Fahrenheit or less, it is too cold to swim. If the water is 104 degrees Fahrenheit or more, it is too hot to swim. Example 1: input: 55 output: Water temperature is 131.0 degrees Fahrenheit....

  • Absorption and transmission: When a photon of light hits a molecule of gas or a particle...

    Absorption and transmission: When a photon of light hits a molecule of gas or a particle in the atmosphere, there are three possibilities: 1) it can be absorbed (the energy is converted to heat), 2) it can be reflected (it continues on but in a different direction), 3) or it can be transmitted. These processes determine our ability to see distant objects (such as the mountains around Los Angeles and Irvine) and the colors in the sky (such as blue...

  • need help to complete this java program // add appropriate import statements here. // These imports...

    need help to complete this java program // add appropriate import statements here. // These imports you can leave as is. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.stage.Stage; /** @author yourAccountNameHere */ public class ConnectTheDots extends Application {            /*     * Do not add code to main(). Add it below in connectTheDots instead.     */     public static void main(String[] args) {         launch(args);     }         /*...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT