CS51 - Spring 2010 - Lecture 11
Exercises 11.4.2 and 11.4.3
show
ColorSlider demo
- what's in the center?
- canvas with a FilledRect
- we can adjust the sliders and in response to that, our program updates the color
- Another interface ChangeListener
- one method: public void stateChanged(ChangeEvent evt)
- similar to our other listeners, we have an: addChangeListener(this) method
- this pattern should start to look familiar:
- implements ChangeListener
- add the stateChange method
- tell the program that "this" will listen/respond to any changes made by the object (in this case, the sliders)
- why do we have two different interfaces ActionListener and ChangeListener
- what does our layout look like at the bottom?
- compare 3 by 1 grids with nested 1 by 3 grids and one 3 by 3 grid
look at
ColorSlider code
- what information do you think we need to construct a new JSlider (i.e. what's going to go in the constructor)?
- need to know whether it's a vertical or horizonal slider
- range, specified by a minimum value and a maximum value
- initial, starting value
- what information for JLabels?
- the text
- the alignment
- JLabel.RIGHT
- JLabel.LEFT
- JLabel.CENTER
- to handle the changing sliders:
- implements ChangeListener
- stateChanged method: going to be called everytime any of the sliders changes values
- addChangeListener(this) for the three sliders
- what do we want to do when a slider changes value?
- get the value from each of the sliders
- getValue() method returns the value
- set the color of our FilledRect to be the new color indicated by the sliders
- update the text for all of the sliders
- setText(...) (similar to setText for Text)
- why do we do "" + redValue? what does this accomplish?
- we could try and figure out which of the sliders changed using evt.getSource(), but it would just make the code more complicated
show
ColorMixer demo
- rather than using a JSlider, we can use a JTextField to allow the user to enter data
- getText() method allows us to get the text entered in the JTextField
- how do we know when to check the data?
- when the user hits "return" it triggers an event
- what type of listener do you think we would use?
- ActionListener (discrete, user generate event)
- we can addActionListener to respond to user inputs
- what does the layout look like?
- 3, 2 GridLayout
- each row has a JLabel and a JTextField
- when we're allowing a user to enter data, what do we need to be careful of?
- need to make sure the the user enters valid data
- in this case, it has to be a number and that number has to be between 0 and 255
look at
ColorMixer code
- when we're creating a GridLayout layout manager, there are two additional parameters we can specify (if we want) that modify the amount of space in between the rows and columns
- new GridLayout(3, 2, 10, 5)
- 3 rows
- 2 columns
- 10 pixel horizontal gap (i.e. between each column)
- 5 pixel vertical gap (i.e. between each row)
- addActionListener(this) for all of the JTextFields
- actionPerformed method
- get the text values
- create a new color and set the color of the FilledRect
- what does the getColorMix method do?
- it's a private helper method
- why is it private?
- only used internally to the class
- gets the value from the JTextField and checks to see if it's the appropriate range
- what if it's not in the range?
- if it's less than the minimum value, it's set to the minimum value
- if it's larger than the maximum value, it's set to the maximum value
- why use a helper method?
- whenever you find yourself doing repeated work, make a method!
what if we want multiple lines of text, like a pararaph, etc?
- JTextArea
- same basic premise as JTextField, but multiple lines
- look at the TextApplet example in the book
show
Interesting demo
midterm review
- worth 15% of your grade
- note also that TP1 is worth 15% of your grade
- make sure you put in the time for both of these
- close book, closed notes, etc.
- we will give you copies of:
- Objectdraw quick reference
- GUI cheat sheet
- types of questions
- 1-2 code this up problems
- 1-2 what does this code do problems
- a few short answer questions
- will cover all the material up through now (including GUIs)
- topics (roughly)
- basic programming
- variables
- instance
- local
- constants
- methods
- classes
- parameters
- commenting
- objectdraw components
- booleans
- if-else
- ints and doubles
- while loops
- active objects
- defining and manipulating multiple classes
- GUIs
- randomness
- colors
- images
- audio
- interfaces