© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

31
©The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

Page 1: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Chapter 18

Advanced graphics programming

Page 2: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Making choices

With graphical applications, there are a number of ways of providing choices to the user:

– pull-down menus;– pop-up menus– dialogue windows;– radio buttons;– combo boxes.

Page 3: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Pull-down menus

• the program below displays a flag consisting of three horizontal stripes;

• the colour of each stripe can be changed by means of the pull-down menus on the top bar.

Page 4: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

There are three aspects to creating a menu:– the menu bar at the top;– the different menus;– the list of menu items associated with each menu.

Page 5: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Page 6: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The constructor • the initial colour is assigned to each stripe;• the individual menu items are then added to the

menus:

Page 7: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• these menus are then added to the menu bar:

• the setJMenuBar method of JFrame is used to add the menu bar to the frame:

• note that a grid layout has been used to get the stripes where they are wanted.

Page 8: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The actionPerformed method

Page 9: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Pop-up menus

• a pop-up menu is normally not available all the time, but pops up only when it is necessary, and then disappears;

• in the application below the menu is used simply to change the background colour of a frame, and is invoked by pressing a button.

Page 10: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• note the line of code that declares and creates a new pop-up menu:

• the menu items are added to this menu in the constructor:

• in the actionPerformed method, the menu is made visible when the button is pressed, and then is hidden once the background colour has been selected.

Page 11: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

An aside

• note the use of the setFocusPainted method;

• calling this method with a parameter of false removes the highlighting around the text of the button when it is in focus:

Page 12: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The JDialog class and the JRadioButton class

• a dialogue window is an alternative to a pop-up menu;• the Swing class that we use to produce such a window is JDialog;• it is useful for those occasions when we do not want a part of a

frame or window permanently devoted to a particular communication because it is only needed at certain times;

• a JDialog object allows us to add any components we wish to it;• below we use radio buttons to change the background colour of a

frame, just as we did with the pop-up menu;

Page 13: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• the JDialog class is very similar to JFrame, both being derived from the AWT Window class;

• we have created the JDialog object with the following constructor:

• as with a JFrame, the JDialog has a default border layout policy;

• we have changed to a flow layout.;• the components are added to the dialogue box as follows.

Page 14: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Note

• we have used the container method pack;

• when there is a flow layout policy, this makes the container adjust its size in order to lay the components out in the most compact manner:

Page 15: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• it is possible for radio buttons to act independently, or, alternatively, as a group;

• in this case we require them to behave as a group, because we want only one to be able to be selected at any one time;

• to do this, we have created an object ButtonGroup:

• we add each button to the group as follows, so that they act together:

Page 16: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Modal and non-modal dialogues • an object of the JDialog class can be modal or non-

modal;• using the empty constructor creates a non-modal dialogue;• this means that any listening components on the originating

frame are still enabled and we can therefore interact with the frame even while the dialogue is visible.

• a modal dialogue works in such a way as to "freeze" any interaction with the parent frame until the dialogue is disposed of;

• to create a modal dialogue you can use a different constructor, which takes two parameters;

• the first is a reference to the originating frame;• the second is a boolean parameter;• if this parameter is true a modal dialogue will be created,• if false a non-modal dialogue will be created.

Page 17: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• in the above example we could have created a modal dialogue with the following line:

• this would make it impossible to close the frame while the dialogue window is visible.

Note• there is a constructor of JDialog which takes just one

parameter, a reference to the originating frame;• this constructor is useful in the case where the code for

the frame resides in a different class to that of the dialogue.

Page 18: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The JComboBox class

• a combo box provides a choice from a list of options;

• below is a simple class (ComboBoxDemo) in which a combo box provides the choice of background colour for the frame.

Page 19: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The choices are revealed when the down arrow is clicked:

Page 20: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• the JComboBox is created with a constructor that accepts an array of objects - normally Strings - that defines the choices:

• the first option, "Select colour" is not actually an option, but is there simply to direct the user.

• an ActionListener is added to the combo box in the constructor:

Page 21: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• in the actionPerformed method we use the getSelectedItem method of JComboBox to determine which item was selected;

• this method returns the selected item, an Object, which in this case must be type cast back to a String.

• the setSelectedIndex method of JComboBox sets the selected item to the one indicated;

• in this case we require the selected item to be "Select colour", so that after the selection is made the user sees this displayed again;

• this is the first item in the list - index 0 of the array.

Page 22: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The JFileChooser class

• a JFileChooser object interacts with the computer's operating system to enable you to search directories and select files;

• the class below produces a frame containing a menu bar consisting of a couple of menu options;

• as well as the menu bar, it provides a text area:

Page 23: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

Choosing the Select option from the File menu will cause a dialogue window to appear:

Page 24: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• below we have chosen the file called calc.exe;• once we select this file a message appears in the

text area, telling us the name of the file chosen.

• if the file chosen is an executable file, then selecting the Run option of the File menu will run the program, in this case the Windows calculator.

Page 25: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• we have declared the following attribute:

• an object of the File class (which resides in the java.io package) will hold a representation of a file;

• once we have selected a file, its details will be held in this attribute.

Page 26: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The actionPerformed method

This is the option associated with choosing the menu item that lets us select a file:

Page 27: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• after creating the JFileChooser object, we call the showDialog method to make the dialogue window visible;

• this requires two parameters:– a reference to the originating component (in our case this

frame);– the text required on the accept button, in this case "Select

File". • once the user has selected the file, the getSelectedFile method is called to return the file selected;

• this is then assigned to the chosenFile attribute;• after this we use the getName method of the File

class to display the name of the file in the text area;• if we had wanted the full path name to be displayed

we could have used getPath instead of getName.

Page 28: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The runChoice option

This loads and runs the selected file.

Page 29: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• we create an object of the Runtime class and then assign it the return value of the getRuntime method, which is a static method of that class;

• the object returned by this method contains information about the Java application that is currently running;

• the Runtime object is then able to execute a command - specific to the particular operating system - as a separate process in the computer's memory;

• it does this with its exec method, which executes the command that is sent in as a parameter;

• in the above example we are sending in the full path name of the selected file (by using the getPath method of File);

• if the file is an executable file then it will be loaded and run.• two exceptions have been handled:

– a NullPointerException, which would be thrown if the file name sent into the exec method were null - that is if no file had been selected;

– an IOException - this would occur it the file were not an executable

Page 30: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

The JSlider class• a JSlider allows us to control the value of a variable by moving a

sliding bar - a slider -which is used to vary a value of an integer within a particular range;

• the JSlider class has a method called getValue that returns an integer representing the distance that the bar has been moved;

• below we have placed a slider at the top of a frame, and added a label to show the current value returned by getValue;

• when you run the program you will see that the default value of the range is 0 to 100

Page 31: © The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

©The McGraw-Hill Companies, 2006

• a constructor of JSlider allows us to decide upon the orientation of the slider with a predefined integer parameter that can be:– JSlider.HORIZONTAL;– JSlider.VERTICAL;

• the interface that we need to implement is ChangeListener (provided in the javax.swing.event library).

• the method that handles the event is stateChanged, which receives a ChangeEvent object;

• we report on the movement of the bar by using the getValue method:

• the minimum and maximum values of the slider default to 0 to 100;• if you require alternative limits there is an appropriate constructor

provided - this takes two integer parameters, representing the minimum and maximum respectively.