Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will continue the Train Route Search Engine mini-project

ID: 3848640 • Letter: I

Question

In this assignment you will continue the Train Route Search Engine mini-project by creating a small user interface application. The classes you create must be compatible with either the classes for the past two assignments (the ones you submitted last week and the week before), or our posted solution. Please indicate which in a comment at the top of the classes. Hint: If you have NetBeans installed, then you can approach this homework by using the Design tab. With the Design tab, you can create your UI by dragging and dropping the swing controls. Once you are done with the UI, you can copy/paste your source code into a new Java file to work on for the remaining tasks. Please see the "Creating Simple GUI in NetBeans" video under Week 4 for a quick example on how to create a user interface in NetBeans using drag and drop Q1: The first part of the assignment involves creating very basic GUI controls: a frame, panel, and slider o Create a JFrame. Add a title to the frame called "Train Route Reservation". Set the frame size to 475 by 500 pixels. Create a JPanel inside of the frame. You will be adding all of your Ul components to this JPanel. 16 ptsl o 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. [8 pts] Q2: Add functionality to search for an Itinerary o Create labels and text fields that allow the user to enter a train type, a source train station, and a destination train station, as well as the departure time and the arrival time to search for one or more available train routes in an Itinerary. Also, create a ComboBox (see Chapter 11) that will allow the user to select from a list of train route Itineraries Each item in the ComboBox represents an Itinerary element, e.g. the first comboBox element is the first Itinerary in the array. [15 pts] o Methods: o Your TrainFrame class should include the following methods The methods that check for validation must have message dialogs indicating to the user if the information entered in each text field is nvalid

Explanation / Answer

Here is the code for Ques 1:

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TrainReservation {

   public static void main(String s[]) {

       JFrame frame = new JFrame("Train Route Reservation");
       JPanel panel = new JPanel();
       panel.setLayout(new FlowLayout());
       JLabel label = new JLabel("Choose color");
       Color color = new Color(240,240,240);
      
       JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
       slider.setPaintTicks(true);
   slider.setMajorTickSpacing(50);
   slider.setMinorTickSpacing(5);
   slider.setPaintLabels(true);
   slider.addChangeListener(new ChangeListener(){
       public void stateChanged(ChangeEvent e) {
           JSlider slider = (JSlider) e.getSource();
           label.setText(String.valueOf(slider.getValue()));
           panel.setBackground(new Color(slider.getValue(),slider.getValue(),slider.getValue()));
           };
   });
       panel.add(label);
       panel.add(slider);
       panel.setBackground(color);
      
       frame.add(panel);
       frame.setSize(475, 500);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);

   }
  
}