I can not seem to get my background color to change. I am programing in java. Th
ID: 3847190 • Letter: I
Question
I can not seem to get my background color to change. I am programing in java. The code in question is the code in my class SliderListener.
Here is the problem given to me:
"Inside of the panel, create a label that says "Choose Color." Right below it, make a slider that changes the background color of the panel when the slider is moved to a particular color. The slider values should be RGB color values from 0 to 255 (or black to white). Assume that the RGB values for red, green, and blue are the same value - for instance: (240, 240, 240). The slider label's text should be replaced with the RGB value that the slider is currently on."
Here is my code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TrainFrame extends JFrame {
/* Declare your UI variables and other class level variables at the class level (outside of the main method). These include GUI controls, such as a panel, text fields, combo
box, slider, labels, and buttons. */
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JSlider jSlider1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private JPanel panel1;
private Time time;
/* Place all of your UI initialization code inside the initComponents() method. Also, add UI components to the panel. (For example: panel = new JPanel(); ) */
public void initComponents() {
panel1 = new JPanel();
panel1.setLayout(null);
add(panel1);
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider(0, 255);
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox<>();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Train Route Reservation");
setMaximumSize(new java.awt.Dimension(475, 500));
setMinimumSize(new java.awt.Dimension(475, 500));
jLabel1.setText("Choose Color");
jLabel2.setText("Train Type:");
jLabel3.setText("Departure Train Station:");
jLabel4.setText("Destination Train Station:");
jLabel5.setText("Departure Time:");
jLabel6.setText("Arrival Time:");
jLabel7.setText("Select Itinerary Object:");
jTextField2.setText("jTextField2");
jTextField3.setText("jTextField3");
jTextField4.setText("jTextField4");
jTextField5.setText("jTextField5");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
jButton2.setText("Search");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("View");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(244, 244, 244)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(6, 6, 6))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(244, 244, 244)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(10, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel7)
.addComponent(jLabel4))
.addGap(92, 92, 92)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addComponent(jTextField4)
.addComponent(jTextField5)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel8)))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]{jButton2, jButton3});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]{jButton2, jButton3});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// Create listeners for each button/control and add action listeners to each UI control
ActionListener comboListener = new ComboListener();
ActionListener viewListener = new ViewListener();
ActionListener searchListener = new SearchListener();
jButton2.addActionListener(searchListener);
ChangeListener sliderListener = new SliderListener();
jSlider1.addChangeListener(sliderListener);
}
public TrainFrame() {
initComponents();
}
/**
* Converts the time String to a Time object. Uncomment this method and
* implement it
*
*/
private Time convertTime() {
// Modify this method so that it returns the correct time
Time time = new Time(hours, minutes);
return time;
}
/**
* SliderListener class that changes the color of the panel to a grayscale
* color when the slider is moved
*
*/
class SliderListener implements ChangeListener {
public void stateChanged(ChangeEvent event) {
int gray = jSlider1.getValue();
Color newBackground = new Color(gray, gray, gray);
panel1.setOpaque(true);
panel1.setBackground(Color.gray);
panel1.repaint();
System.out.println(gray);
jLabel8.setText(Integer.toString(gray));
}
}
/**
* ComboListener class used to get the selected option in the itineraries
* combobox
*
*/
class ComboListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
/**
* SearchListener class is used to populate the itineraries combobox
* (validating text input is not required, but recommended)
*
*/
class SearchListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String strTrainType = jTextField1.getText();
String strDepatureStation = jTextField2.getText();
String strDestinationStation = jTextField3.getText();
String strDepartureTime = jTextField4.getText();
String srtArrivalTime = jTextField5.getText();
checkValidTrainType();
}
}
/**
* ViewListener class validates train type, source and destination train
* stations, departure and arrival times, creates an instance of RouteFrame
* and opens the route frame that shows the results of the train route
* search
*
*/
class ViewListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
/**
* Checks if the train type entered in the train type text field can be
* converted to a TrainType object
*
* @return true if entered train type is valid and exists in TrainType enum
*
*/
private boolean checkValidTrainType() {
if (strTrainType.equals("yes")) {
return true;
}
return false;
}
/**
* Checks if source and destination train stations entered in the text
* fields exist in TrainStation enum
*
* @param src The entered source (departure) train station
* @param dest The entered destination train station
* @return true if they are both valid train station
*
*/
private boolean checkValidTrainStations(String src, String dest) {
return false;
}
/**
* Checks if the time is in valid format (hh:mm a)
*
* @param depTime The departure time text field
* @param arriveTime The arrival time text field
*
*/
private boolean checkValidTime(String depTime, String arriveTime) {
return false;
}
// Initialize TrainFrame and set its properties in the main method (size, whether it is resizable, title, default close operation, and visibility)
public static void main(String[] args) {
TrainFrame frame = new TrainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorValueSliderControl extends JFrame {
public ColorValueSliderControl() {
getContentPane().add(new TColor());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new ColorValueSliderControl();
}
}
class TColor extends JPanel {
DrawingCanvas canvas = new DrawingCanvas();
JLabel rgbValue = new JLabel("");
JSlider sliderR, sliderG, sliderB, sliderH, sliderS, sliderBr,
sliderAlpha;
public TColor() {
sliderR = getSlider(0, 255, 0, 40, 5);
sliderG = getSlider(0, 255, 0, 40, 5);
sliderB = getSlider(0, 255, 0, 40, 5);
sliderH = getSlider(0, 10, 0, 5, 1);
sliderS = getSlider(0, 10, 0, 5, 1);
sliderBr = getSlider(0, 10, 0, 5, 1);
sliderAlpha = getSlider(0, 255, 255, 40, 5);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 2, 15, 0));
panel.add(new JLabel("R-G-B Sliders (0 - 255)"));
panel.add(new JLabel("H-S-B Sliders (ex-1)"));
panel.add(sliderR);
panel.add(sliderH);
panel.add(sliderG);
panel.add(sliderS);
panel.add(sliderB);
panel.add(sliderBr);
panel.add(new JLabel("Alpha Adjustment (0 - 255): ", JLabel.RIGHT));
panel.add(sliderAlpha);
panel.add(new JLabel("RGB Value: ", JLabel.RIGHT));
rgbValue.setBackground(Color.white);
rgbValue.setForeground(Color.black);
rgbValue.setOpaque(true);
panel.add(rgbValue);
add(panel, BorderLayout.SOUTH);
add(canvas);
}
public JSlider getSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) {
JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(mjrTkSp);
slider.setMinorTickSpacing(mnrTkSp);
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());
return slider;
}
class DrawingCanvas extends Canvas {
Color color;
int redValue, greenValue, blueValue;
int alphaValue = 255;
float[] hsbValues = new float[2];
public DrawingCanvas() {
setSize(240, 240);
color = new Color(0, 0, 0);
setBackgroundColor();
}
public void setBackgroundColor() {
color = new Color(redValue, greenValue, blueValue, alphaValue);
setBackground(color);
}
}
class SliderListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
if (slider == sliderAlpha) {
canvas.alphaValue = slider.getValue();
canvas.setBackgroundColor();
} else if (slider == sliderR) {
canvas.redValue = slider.getValue();
displayRGBColor();
} else if (slider == sliderG) {
canvas.greenValue = slider.getValue();
displayRGBColor();
} else if (slider == sliderB) {
canvas.blueValue = slider.getValue();
displayRGBColor();
} else if (slider == sliderH) {
canvas.hsbValues[0] = (float) (slider.getValue() * 0.1);
displayHSBColor();
} else if (slider == sliderS) {
canvas.hsbValues[1] = (float) (slider.getValue() * 0.1);
displayHSBColor();
}
canvas.repaint();
}
public void displayRGBColor() {
canvas.setBackgroundColor();
Color.RGBtoHSB(canvas.redValue, canvas.greenValue, canvas.blueValue,canvas.hsbValues);
sliderH.setValue((int) (canvas.hsbValues[0] * 10));
sliderS.setValue((int) (canvas.hsbValues[1] * 10));
rgbValue.setText(Integer.toString(canvas.color.getRGB() & 0xffffff, 16));
}
public void displayHSBColor() {
canvas.color = Color.getHSBColor(canvas.hsbValues[0],
canvas.hsbValues[1], canvas.hsbValues[2]);
canvas.redValue = canvas.color.getRed();
canvas.greenValue = canvas.color.getGreen();
canvas.blueValue = canvas.color.getBlue();
sliderR.setValue(canvas.redValue);
sliderG.setValue(canvas.greenValue);
sliderB.setValue(canvas.blueValue);
canvas.color = new Color(canvas.redValue, canvas.greenValue,
canvas.blueValue, canvas.alphaValue);
canvas.setBackground(canvas.color);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.