Site Overlay

How to design a software train simulator program that enables the user to enter data by a screen drawing process

Hits: 85

Computer software programs have been written in many years for industrial management and systems control. Most hardware devices and intelligent machines are succumbed to some software programs either for simulation purposes, monitoring or control. Control programs are developed and transferred onto devices such as the microprocessor chip, which is then embedded onto a hardware system.

In many years manufacturing industries have depend on computer programs. Fuel supply and burner flame monitoring in power stations’ boilers, temperature control in nuclear power stations, automatic piloting in aeroplanes and so forth are all software controlled. 

Likewise, train simulator programs have been written by different organizations to simulate different aspects of the train performance. The Microsoft Company for example has designed a graphical software program that enables the user to drive different trains on different tracks and different places. The Microsoft Train Simulator program can be used both for teaching and for fun.

The Single Run Train Simulator software program is designed to simulate the performance of a single train running from a given station to a destination station. During the processing of the run, train data is manually entered into tables. The data includes traction force, distance covered, breaking rate, gradients, speed, line current, and other parameters not mentioned here. The output is preferably in the form of speed/distance or speed/time graphs or tabulation of the same data. Subsidiary information on gradients and speed restrictions can also be plotted. There is a significant number of data that needs to be entered into tables and plotted onto graphs.

 The whole data entry process could be tedious and time consuming, especially when dealing with the gradient profile entries for long distance train journeys.

 This project is therefore designed to reduce the workload of data entry process, by introducing graphics that enables graphical data input by a screen drawing processes. Data such as motor characteristics, track profiles etc could be entered easily using this graphical program. This report discusses the design and implementation of this graphical program.

The aims of this project are:

  • To design a software train simulator program that enables the user to enter data by a screen drawing process.
  • The design is to be implemented on a graphics basis using programming language that can create graphical user interfaces.

The objectives are:

  • The final design should be user friendly.
  • The data produced by the program should be accurate for reliable results, and transferable to other similar train simulator programs.

This project program is designed using Java, anobject oriented software development language. The programmer has studied this programming language to greater lengths and has more experience in its use compared to other languages such as C++ and mat lab.

The Java programming language was developed for the Internet and is well known for its simplicity compared to its closest rival, C++. Java has a rich library that makes it possible to write portable programs. This means that programs can be written on one computer and moved to a different one without rewriting them.

Java has two important attributes, safety and portability that make it user friendly. Some websites do contain applets that display animated information or images that automatically start running. An applet is a graphical program that executes inside a web browser or applet viewer. It is important that these self-executable applets are safe to run on any computer that is connected to the Internet. Today people view web pages containing applets and this does not damage the data in their personal computers otherwise it would obviously be dangerous to surf the Internet.  

Frame Windows

A frame window is a graphical rectangular frame with a border and a title bar. To show a frame, the JFrame class in the javax.swing package is used. The javax package name denotes a standard extension of java.  The JFrame class is a fundamental class in the Swing toolkit, the most advanced user interface toolkit that Sun Microsystems has created for Java -Cay S Horstmann. Figure 1.1 shows a simple frame created in Java.

A Frame Window, showing the title bar and the border. Using the mouse can operate the buttons by ‘clicking’ on the surface of the buttons.

In the event of executing the Frame Window above, a DOS (Disk Operating System) program called a Console window is also executed. This is shown in the Figure 1.2 on page 13. A console window displays messages on the events taking place in the main program, (the frame window). Output Strings can also be printed on this frame such as the words “Starting MyFrame…”using the command line: System.out.println (“Starting MyFrame…”); as described in the Example 5 below.

The Example 5 below shows a piece of code that describes a Frame Window.

Example A code describing a frame window in Java.

Note: in this code the new class MyFrame extends – meaning it inherits the characteristics of the class JFrame. Comments appear in green.  The statement terminator marks the end of a program statement or instruction the instructions that come after the terminator are totally new statements, this acts just like the full stop mark in text a paragraph.

A Console window

The anatomy of the Frame Window

The surface of the frame window is covered with four panes:

  • The Content pane, called from the class “Container”, which holds the components that are to be displayed in the window.
  • The Glass pane, which is transparent, and captures mouse events. A mouse event occurs when the mouse is, clicked, clicked and dragged, when the mouse button is pressed or when it is released, keeping in mind that depending on the design of the mouse, there can be more than one button. Commonly known are the right and the left mouse buttons. See Figure 1.3.
  • The Layered pane holds the Menu bar and the content pane together.
  • The Root pane holds the glass pane and the layered pane together.
The anatomy of a Swing Frame (The Frame Window)

Graphical User Interface Components

A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface.

After a Frame window has been created, user interface components such as buttons, scroll bars, menus and tables can then be added onto the frame. Drawing straight onto the frame window interferes with the display of these components and hence the need to use a JPanel. A JPanel is a special component provided by the Swing User Interface toolkit.  It is completely blank and any thing can be drawn onto it in terms of graphics. The user interface components are added onto the JPanel, which is then added onto the frame window (see the anatomy of the Frame Window).


Buttons are user interface components that help the user take control the program when by clicking on them. When a button is clicked, it sends an action event. This action event can be captured using an action listener. An action listener simply listens to the events or state of the button (if clicked or not clicked), and executes a command that changes the state of the program accordingly.  A click on a button on the screen for example would mean to zoom into a view if viewing a graph in the train simulator program.  The buttons carry labels on them so that the user can know which button to click in order to perform a particular action, while using the program.

A simple button containing the label ‘Click Me’.


A menu is a collection of menu items and more menus. Menus items and sub menus are added using the add method. Menus are like buttons only that they can ‘pull down’ to allow the user to select on the menu the item of interest. The container for the top-level menu items is called the menu bar. A menu bar is created and attached to the frame window.

A menu (‘File’) and menu items (‘New’, ‘Save’) attached onto the frame window

Scroll Pane

A scroll pane in java is like the content pane only that it has a scrolling facility that allows the user to scroll text or tables, when viewing the data in the program. A scroll pane can contain either a text field, a text area or a data table for it to function normally.

A text field is a plain white graphical surface that allows the user to, edit, delete or type text characters. A text area contains more than one row of text entries, while a text field is restricted to only one line of entry. See the Figures 1.7 and 1.8 below. Figure 1.9 shows two scroll panes, one with a data table and the other with a text area.

A Text Field
A Text area
A scroll pane containing a data table(a) and a scroll pane containing a  text field (b).

Adding User Interface Components to a Frame Window

Consider the Frame Window in section 3.1.6, Figure 1.1. If the title is changed to “TRAIN SIMULATOR PROGRAM” and the code slightly altered to look like the one in Example 6. below, then we’ll have a simple train simulator program frame with nothing on it. This will not probably work as a train simulator but it provides a basement layout of the main program. See the Figure 2.0 on page 20.

Example A Simple Frame Window for the Train Simulator Program

Note: the comments are in green (/*—–*/) and do not affect the program whatsoever, they are just guidelines explaining the main code.

Note: The figure has been re scaled to fit the page

Now the frame window is ready to start accommodating user interface components.

To add a button, first we import the class JButton from the javax.swing package by putting the command line   import javax.swing.JButton; on top of the program code. A JPanel and a Container also need to be imported in the same way. See the figure below. A return type method needs to be created and this normally carries the same name as the main class, in this case, ’TRAINSIMULATORPROGRAM’ will be the name of the return type method. A return type method is one that computes values and executes most of the active commands within the program software. For example the return type method can take an argument and returns a value. An Argument is an actual parameter in a method call or one of the values combined by an operator.

Example Adding a single button onto the Train Simulator Program frame window.

Train Simulator Frame Window with a button added to it

Below shows a part of the code derived from Example 7 

Shows that the classes Jframe, Jbutton and Jpanel all come from the same package javax.swing. This package is therefore called three times. Many other classes can be imported from the same package and this could be a lengthy code with the same package name repeated several times for different classes. For convenience purposes and to shorten the code, the above code can be replaced by the code below:

This is just the same code as the one in Example 8 only that it is shortened for convenience purposes. The  (. *) parameter stands for all the classes within the package named before it. This code will therefore import all the classes from the two packages javax.swing and java.awt and the programmer will not have to worry about typing all the class names when using the packages.

By adding the code in Example 10 below to the one in Example 7, a menu bar and its items can be added onto the frame window to create a simple user interface with a button and a menu item.

Adding a menu bar, a menu and a menu item onto the frame window

A menu (‘File’) and menu items (‘New’, ‘Save’) and a button added onto the frame window.


When the user clicks on the menu item or on the button, the menu item or the button sends an action event. After implementing the buttons and the menu items, there is need to implement a listener, which captures the action event. Associating a listener with each button and each menu item does this. 

Attaching a button listener to a button

Attaching a menu listener to the menu items

Implementing a button listener

Implementing a menu listener

Keyboard and Mouse Event Handling

For programs that are user controlled, the user communicates with the program by pressing the keys on the computer keyboard and the mouse. The state of the keys and mouse at a given time is called an event i.e., the keys can be at a state ‘pressed’ or at a state ‘released’. Likewise the mouse events would be ‘button pressed’ or ‘button released’, ‘mouse dragged’ or mouse button pressed while mouse is dragged over the mat.

The User Friendly Train Simulator Program employs a number of keyboard and mouse events, as a major form of data input for the program. This means that data can be entered onto tables by typing or by using the mouse to draw on the screen.

Array Data Tabling

Data tables are created using method call approach, by calling columns and rows from the table model class.  The table model class already exists in the java class library and this creates a table containing the values given in arrays.  Using the following code

This creates a table set containing the arrays, data and parameters described in the above section. See the comments in green for detail.

Editing a cell and the activity window display. The activity window displays information on the data that has been recently changed.

Editing a Data Table Cell

If the user selects a cell in column number 3, and types in 555540, then the display will appear as:  End of Gradient is 555540 m. The ‘m’ standing for meters. See Figure 2.4 below

Editing a cell and the activity window display. The activity window displays information on the data that has been recently changed.


Layout Management

This is the planning and positioning done when adding components such as buttons, text fields, the graphs and data tables onto one panel in order to view and use them. There are three types of layout used in this program: the Border Layout, the Flow Layout and the Grid Layout.

The Border Layout

The content pane uses a border layout to arrange its components. The locations to place the components are specified as “Center”, ”North”, ”South”. ”West”, or ”East”. See Figure 2.5 below.

Component areas of a Border Layout.
A border layout for the Train simulator program’s Frame window

The Flow Layout

The panel uses a flow layout. This arranges the components in a row and starts a new row when there is no more room in the current row.

A Flow Layout of user interface components on a panel

A panel, named as a button panel in the program with a flow layout, with two text fields and three buttons. The two text fields are used here to display a summary of the contents of the data tables for example the information for the train journey as “ Northern Line, Camden Town to High Barnet”, is a simple reference to what the data represents.

The Grid Layout

This is another form of layout, which arranges the components in a grid format with a fixed number of rows and columns. Example 19 shows a code for a grid layout with 1 column and 8 rows. Also see Figure 2.8. for the resulting graphics.

Example 19. A Grid Layout statement for a button panel

A Grid Layout of user interface components (buttons) on a panel

The Paint Component

The Java Paint Component is a method that allows us to draw and display images on the screen (on the content pane). In the train simulator program this has been used to create two types of graphs, the editable graph and a normal graph.  The normal graph plots the values straight from the data table; while the editable graph allows the user to draw graphs onto it and at a later stage, map the graph data onto a table.

When declaring the paint component a sequence of code is used such as the one shown in Example 20 below.

Example 20. Implementing the Paint Component for drawing shapes onto the screen.

Declaring the following code will result in the lines, text and shape shown below it and considering the color changes as well. A string in java is a set of characters that can be typed into a program such as the Microsoft word using a standard keyboard.

Example 21. A set of instructions used to draw a blue vertical line, a horizontal line, a red rectangle and a string of characters onto the screen. The comments in green explain each step.

Note: this code gets the size of the height of the graphics window frame and divides it by two, to approximately put the horizontal line in the middle of the screen. This is only for demonstration as there is a more efficient and technical way of doing this, as shown in the appendices (code).

Figure 2.9. An image displayed on the graphics area drawn using the code in Example 21. This shows a rectangle, a horizontal line and a vertical line.

With these achievements, a graph can easily be plotted using similar instructions, this time replacing the coordinates x and y with the values given in the data table containing the information we want to plot.

Figure 2.9 also shows the relationship between the x values and the y values of the graphics window, where x is the width and the y is the height of the window. To draw a graph for example, one needs to establish a mid point, where the y-axis meets the x-axis and then plot from there. Therefore, for a start we have drawn a horizontal line and placed it at the centre of the graphics screen by getting the height of the graphics window, (Y maximum), and using a for-loop to make it run across the width of the graphics frame. In this case the end of the for-loop condition, is the width of the frame, so the horizontal line is drawn from the position X=0 to X= width of the frame.

 Figure 2.9 also shows the relationship between the x values and the y values of the graphics window, where x is the width and the y is the height of the window. To draw a graph for example, one needs to establish a mid point, where the y-axis meets the x-axis and then plot from there. Therefore, for a start we have drawn a horizontal line and placed it at the centre of the graphics screen by getting the height of the graphics window, (Y maximum), and using a for-loop to make it run across the width of the graphics frame. In this case the end of the for-loop condition, is the width of the frame, so the horizontal line is drawn from the position X=0 to X= width of the frame.


In the Train Simulator Program, the data can be used, in the first instance to plot a graph of the Gradient Profile and the Motor Characteristics. The user can edit the current data, making changes to it then plotting or can use the given data directly. The following are the step-by-step testing method used to demonstrate the functioning of the program.

Editing the data

 The user can use the current data as given or change it accordingly. This data includes, the Gradient Profile and the Motor Characteristics.

. Running the program. The user can scroll the tables up or down and make changes to the table or use the default data.

Displaying the graph

The Gradient Profile

When the data is ready, the user clicks on the “Show Graph” button indicated on Figure 3.0, which sets a new frame window running, with a graph drawn on it as shown below.

Figure 3.1. The Graph Frame Window showing the gradient profile. This Frame Window contains buttons that can be used to change the view of the graph. The buttons are labeled as  + or – for example the “Shift +” button and next to it is a ‘-’ button. The Sift button moves the graph to the left (Shift +) or to the right (-).  The buttons to display information are found on the left of the graph

frame (the Rise + and Rise- buttons, D/S/T (+/-) buttons. For displaying, Distance or Speed or Time, depending on the graph being viewed, and the gradient information display buttons (Grad+/-).

At this point, the user can choose to use the buttons available on the graph frame window, to zoom in for example and also to show more detail on the graph, see Figure 3.2

Figure 3.2. The Graph Frame Window showing the rise or fall in 1000s of meters, the distance covered in meters and the gradients at particular points on the curve. Clicking on the “Rise +” button shows the rise the “D/S/T +”(Distance/Speed/Time), displays the distance, and the “Grad +” button shows the gradient. The graph has also been stretched to the right by using the “Horizontal +” button and zoomed in using the “Zoom +” button. The graph also displays the position of stations by station names or towns. All these information has been entered in the data tables prior to plotting. The reset button will automatically change this graph to look like the one on Figure 3.1, which is the default graph. Of course the D/S/T-, the Grad-, etc buttons will reverse the above actions.

The Motor Characteristics

Selecting the ‘View’ menu on the graph frame window shown in Figure 3.2 above, and selecting the item Tractive Effort on the Speed Graphs menu item, changes the view to the motor characteristics curve, plotted using the motor characteristics data available in the main program. The user can easily switch between the gradient profile graph and the motor characteristic curves.

Figure 3.3.Selecting from the menu item when changing from viewing the Gradient Profile to viewing the Motor Characteristics
Figure 3.4. The Motor Characteristics curve plotted from the data in the program.
Figure 3.5. The Motor Characteristics curve showing the Tractive Effort and the Speed information at the plotted points. This detail is added by using the buttons on the left of the graph, see Figure 3.1 and Figure 3.2 on how to use the buttons.

The Editable Graph

The editable graph is called by selecting on the menu, the menu item “Edit Graph”. This opens a new frame window.

Figure 3.6. Selecting the editable graph menu item in order to start drawing the graph.

Editing the Gradient Profile

Editing the graph is done by adding plotting points and moving these points to the correct location within the graph window.  See Figure 3.7. To add points, the user clicks on the ‘Add Point’ button, likewise the point can be removed using the ‘Remove Point’ button. A point is indicated by a red rectangle and a black line automatically joins the points as they are added. Clicking on the ‘Guide’ button puts a blue rectangle that can be dragged by the mouse to guide the user on where to place the next point. If however the points are not placed at the correct positions, the user can still click on the red rectangles and drag them using the mouse to change the positions of the points. The gradient value shown in red is calculated by getting the x-y location of the points and using the gradient equation shown below. The program does this calculation within the code and displays the result (see the appendices for the program code).

Gradient =     next point’s y position – current point y position

                      next point’s x position – current point x position

The Gradient equation

Figure 3.7.The Editable Graph Showing how to edit the gradient profile graph.

Editing the Motor Characteristics

To edit the motor characteristics, the user changes the view to ‘Tractive Effort’ against speed plot; see Figure 3.3 on how to change the view. The user can then start editing the graph by adding or removing points and dragging them to position using the mouse.

Figure 3.8. Editing the Motor Characteristics curve, by adding points and moving them to position using the mouse to click and drag.

Obtaining a Data Table From the Graph

To obtain the data from the graph, the  “Map To Table” button is used; see Figure 3.8.  Figure 3.9 and 4.0 show some sample results obtained using this program.

Figure 3.9. A data table of results obtained from a graph drawn on a graphics area for the Motor Characteristics.
Figure 4.0. A data table of results obtained from a graph drawn on a graphics area for the Track Gradient Profile.

Getting User Help

User help is made available by clicking on the help menu on the Frame Windows. The user can get help on using the program and also get a brief explanation on what the program is all about.


This program can be designed further to capture information drawn on the graphical window to a higher level of accuracy that the one already achieved.  More user interface components could be added to suit the needs of the user and also give the user control over the components for own interest and level of understanding. Printing files, opening and saving files could also be implemented. This program does not demonstrate much on printing and saving or opening of new files, but these could be done if more time is allocated to the project.

Risk Assessment

This is entirely a software program and there is no known risk involved in using this software program.


This project has demonstrated successfully the use of graphics to input data by a screen drawing process.

The editing processes involved in the data manipulation of the Train Simulator Program are easy to understand and use.

The outputs achieved by using the gradient profile and the motor characteristic curves data indicate that it is possible to extend the design further in order to accommodate other types of data that are not explored in this program.


Cay S. Horstmann (1999), Computing Concepts with Java essentials 2. Chapters 3, 4,10,11 and 12.

Mike Smith’s program for the Advanced Railway Research Center, University of Sheffield, 27th October 1999. visited on 16th January 2003

Leave a Reply

Your email address will not be published. Required fields are marked *

Please support the site
By clicking any of these buttons you help our site to get better
Social PopUP by Timersys