More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on...
Transcript of More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on...
![Page 1: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/1.jpg)
More aboutGUIs
COMP163
![Page 2: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/2.jpg)
“Learning is not attained by chance,
it must be sought for with ardor and
attended to with diligence.”
Abigail Adams
2nd First Lady of the United States
![Page 3: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/3.jpg)
Teaching Evaluation
• The official University teaching evaluation survey is on Blackboard
• Complete the survey for ALL classes
• 19% of the students in COMP163 have completed the evaluation
![Page 4: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/4.jpg)
COMP163 Schedule
Mon, Nov 12
More on GUIsWed, Nov 14
Secure ProgrammingFri, Nov 16
review
Mon, Nov 19
Exam 3Wed, Nov 21
Thanksgiving Holiday(no classes)
Fri, Nov 23
Thanksgiving Holiday(no classes)
Mon, Nov 26
Software engineeringWed, Nov 28
final reviewThursday, Nov 29
Lab Final
Tuesday, Dec 4
Final10:30 – 12:30
Exam
![Page 5: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/5.jpg)
![Page 6: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/6.jpg)
![Page 7: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/7.jpg)
Interesting Objects
Many GUI program can be created with a few simple components
• JLabel – Displays text in the GUI
• JButton – Creates a button you can press
• JTextField – Creates an input text box
• All of these objects are from the package javax.swing
![Page 8: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/8.jpg)
How do you make an array of 5 buttons for a GUI?
A. JButton[5] gazelle = new JButton[];
B. JButton gazelle = new JButton(5);
C. JButton[] gazelle = new JButton[5];
D. JButton[] gazelle = { JButton(), JButton(),JButton(), JButton(), JButton() };
E. You cannot make an array of GUI objects
![Page 9: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/9.jpg)
Array of Objects
• If you create an array of objects, you need to create the objects in the array
for (int cow = 0; cow < 5; cow++) {
gazelle[cow] = new JButton("Go");
pane.add( gazelle[cow] );
gazelle[cow].addActionListener( this );
}
![Page 10: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/10.jpg)
Implementing ActionListener
• Implementing the ActionListener interface makes the program respond to GUI input
• Programs implementing ActionListener must have an actionPerformed method
public class MyAppletextends javax.swing.JFrameimplements java.awt.event.ActionListener {
![Page 11: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/11.jpg)
Interfaces
• A Java interface has a list of method headers that a program must implement
• ActionListener is an interface that requires a program to include an actionPerformed method
• Programmers can create their own interfaces
– This is a topic for COMP167
![Page 12: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/12.jpg)
Possible Interface for Bouncing Circles
public interface Bouncable {public double getX();public void setX( double horz );public double getY();public void setY( double vert );public boolean touches( Circle other );
}
The Circle class could bepublic class Circle implements Bouncable { … }
![Page 13: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/13.jpg)
Other Interfaces
• There are many interfaces in the standard Java libraries
• The Comparable interface requires implementing classes to have a compareTomethod
public int compareTo( MyClass other )
• Returns a negative, zero, or positive integer as this object is less than, equal to, or greater than the other object
![Page 14: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/14.jpg)
The Comparable interface is implemented by which class?
A. Math
B. Scanner
C. String
D. Only classes that you write
![Page 15: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/15.jpg)
Generics
• Sometime when you create an object or interface in Java, you must specify the class it will work with
• When you used a HashTable you had to specify the type of the key and data
Hashtable<String, WordData> dict =
new Hashtable<>(18000);
• You must specify the type for Comparablepublic class Mine implements Comparable<Mine>
![Page 16: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/16.jpg)
Adding a Listener
• The method addActionListener should be called on each GUI component that can do something
• Do not call addActionListener on components that do not do anything, like JLabels
javax.swing.JButton frog = newjavax.swing.JButton("Go");
frog.addActionListener( this );
![Page 17: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/17.jpg)
Taking Action
• When a user clicks an object with an ActionListener, Java calls the method
public void actionPerformed(java.awt.event.ActionEvent rabbit) {
// do something here
}
• This method should do what the user expects to happen when the button is pressed
![Page 18: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/18.jpg)
Getting the Text
• You can get the text from a GUI component with the getText() method
• This is useful to get the text a person entered in the JTextField
javax.swing.JTextField llama = newjavax.swing.JTextField();
// in the actionPerformed method
String answer = llama.getText();
![Page 19: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/19.jpg)
Changing a Component’s Text
• A useful method is setText It can be used to change the text of any object that has text, such as JButtons, JLabels and JTextfields
javax.swing.JLabel msg =new javax.swing.JLabel("Do something");
msg.setText("Don’t do something");
![Page 20: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/20.jpg)
Who Dunit?
• Many GUIs have more than one active object
• You can determine where the action was taken by using the getSource() method of the event
• The result of getSource() should be compared to each GUI object
public void actionPerformed( java.awt.event.ActionEvent frog ) {
if ( frog.getSource() == GUIobject ) {
![Page 21: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/21.jpg)
Example Use of getSource()
JButton delete = new JButton(“kill”);
JButton save = new JButton(“allow”);
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
public void actionPerformed( java.awt.event.ActionEvent toad) {
if (toad.getSource() == delete ) {
// delete something
} else {
// don’t delete anything
}
![Page 22: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/22.jpg)
Looping in GUI Programs
• When you want a user to enter multiple
values in a GUI program, you probably do notneed a for loop or a while loop
• Each time the user enters a name, it will call actionPerformed
![Page 23: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/23.jpg)
What will select the pressed button?
A. if( bear == deer )
B. if( bear.getSource() == JButton )
C. if( bear.getSource() == deer )
D. if( deer == JButton.getSource() )
E. if( this == bear.getSource() )
JButton deer = new JButton();public void actionPerformed(
java.awt.event.ActionEvent bear)
![Page 24: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/24.jpg)
setVisible
• Most GUI components have a setVisiblemethod
setVisible( boolean )
• If the boolean parameter is true, the object shows, if false it does not show
• Calling setVisible(true) makes the frame appear onscreen
![Page 25: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/25.jpg)
Disabling GUI Components
• Individual GUI components (i.e. JButtons) can be made invisible or disabled
• A disabled component is gray and cannot be selected
JButton myButton = new JButton("OK");
myButton.setEnabled(false);
• This will make the button gray and unusable
![Page 26: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/26.jpg)
setTitle
• The setTitle method of a JFrame sets the text in the upper border of the window
setTitle("My GUI program");
![Page 27: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/27.jpg)
pack
• The pack method sizes the frame so that all its contents are at their preferred sizes
• When you use the pack method, you do not have to call setSize for the frame
• If you use absolute positioning of components instead of a layout manager, you do not need to call pack
![Page 28: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/28.jpg)
pack() Issues
• Using setSize and not pack()
• Using pack()
• Using pack() with spaces in JTextfields
![Page 29: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/29.jpg)
Sample GUI ConstructorJTextField inStuff = new JTextField( 8 ); // space for 8 letters
JButton theButton = new JButton("OK");
public MyGUI( ) {
java.awt.Container pane = getContentPane();
BoxLayout where = new BoxLayout(pane, BoxLayout.Y_AXIS);
setLayout(where);
pane.add(inStuff);
pane.add(theButton);
theButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
![Page 30: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/30.jpg)
Method Review
• Follow the flow of execution. Execution starts in the main method
• Constructors can be used to initialize objects
![Page 31: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/31.jpg)
What is displayed?public class Colors {
static void aqua(int fish) {
grass(fish + 2);System.out.println("Blue "+ fish);
}static void grass(int snake){
System.out.println(“Green "+ snake);}public static void main(String[] args) {
System.out.println("Gray");aqua( 1 );
}static void rouge(int bird) {
System.out.println("Scarlet"+ bird);}
}
A. Blue 2Green 1Gray Scarlet 3
B. GrayBlue 3
C. GrayGreen 1Blue 3
D. GrayGreen 3Blue 1
E. none of the above
![Page 32: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/32.jpg)
Creating Arrays of Objects
• Often you will create an array and put the objects in the array as you create them
• Consider a program that will create an array of WordData objects using data from a file
WordData[] centaur = new WordData[1000];
• The array can hold 1000 objects, but it is initially empty with all elements equal to null
![Page 33: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/33.jpg)
Example Classpublic class WordData {
private String sphinx;private int count = 1;public WordData( String aWord ) {
sphinx = aWord;}public String getWord() {
return sphinx;}
public boolean match( String mermaid ) {
return sphinx.equals( mermaid );
}
}
![Page 34: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/34.jpg)
Putting Data in the Array
WordData[] centaur = new WordData[1000];
int numObj = 0;
java.io.File griffin = new java.io.File("stuff.txt");
Scanner dragon = new Scanner( griffin );
while ( dragon.hasNext() ) {
String unicorn = dragon.next();
centaur[numObj] = new WordData(unicorn);
numObj++;
}
![Page 35: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/35.jpg)
Incrementing in an Index
WordData[] centaur = new WordData[1000];
int numObj = 0;
java.io.File griffin = new java.io.File("stuff.txt");
Scanner dragon = new Scanner( griffin );
while ( dragon.hasNext() ) {
String unicorn = dragon.next();
centaur[numObj++] = new WordData(unicorn);
}
![Page 36: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/36.jpg)
How can you call the chew()method on all objects??
Assume you have an array cow that contains 12 objects of the class Bovin which has a method called chew()
for (int i = 0; i < 12; i++) {
A. cow.chew(i);
B. cow.chew[i];
C. cow[i].chew();
D. cow.chew()[i]
E. Bovine.chew(i)
![Page 37: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/37.jpg)
Multiple Methods
names[3].trim().length()
array
elementString
returned valueString
returned value
int
![Page 38: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/38.jpg)
Finding Things in an Array
• Programs may need to search an array of objects for one that matches an instance variable
• If the data in the object is private, you will have to use a method to access it
![Page 39: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/39.jpg)
Search Objects with Get
String target = "special";
for (int roc = 0; roc < numObj; roc++) {
if (target.equals(centaur[roc].getWord()) {
System.out.println("found it");
break;
}
}
![Page 40: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/40.jpg)
Search Objects with match()
String target = "special";
for (int roc = 0; roc < numObj; roc++) {
if (centaur[roc].match( target )) {
System.out.println("found it");
break;
}
}
![Page 41: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/41.jpg)
Graphics Coordinates
• The screen has a coordinate system with the origin in the upper left corner
• Coordinates are given in pixels (Picture Elements)
• An X coordinate specifies the distance from the left edge
• A Y coordinate specified the distance from the top edge
• The screen size depends on the device
![Page 42: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/42.jpg)
drawLine• The drawLine method of java.awt.Graphics
objects draws a line from x1,y1 to x2,y2
bird.drawLine(5, 3, 17, 23);
• The line is drawn using the current color
53
17
23
![Page 43: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/43.jpg)
Location 0,0 is
D. center
A. upper left corner
B. lower left corner
C. upper right corner
![Page 44: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/44.jpg)
drawRect• The drawRect method of java.awt.Graphics
objects draws a rectangle of width and height whose upper left corner is x,y
bird.drawRect( x, y, width, height);
bird.drawRect( 5, 3, 10, 15 );
53
10
15
![Page 45: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/45.jpg)
fillRect• The fillRect method of java.awt.Graphics
objects draws a rectangle of width and height whose upper left corner is x,y and fills it with the current color
bird.fillRect( 5, 3, 10, 15 );
53
10
15
![Page 46: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/46.jpg)
Outside and Overlapping• It is permissible to have a graphics object
(such as a rectangle) extend outside the visible window
• The portion outside the window will not be visible
• An object can overlap one another
bird.fillRect( 5, 3, 10, 15 );
bird.setColor( Color.GREEN );
bird.fillRect( 8, 15, 10, 15 );
![Page 47: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/47.jpg)
drawOval• The drawOval method of java.awt.Graphics
objects draws a circle or oval to fit in a box of width and height whose upper left is x,y
bird.drawOval( x, y, width, height );
bird.drawOval( 5, 3, 10, 15 );
53
10
15
![Page 48: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/48.jpg)
fillOval
• fillOval is just like drawOval, but it colors in the circle with the current color
bird.fillOval( 5, 3, 10, 15 );
53
10
15
![Page 49: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/49.jpg)
What is the output?public void paint(java.awt.Graphics parrot) {
parrot.setColor(java.awt.Color.BLACK);
parrot.drawRect( 0, 0, 100, 100);
parrot.fillRect( 50, 0, 50, 50 );
}
A. B. C. D.
![Page 50: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/50.jpg)
drawPolygon
• The drawPolygon draws lines between the points specified by an array
drawPolygon(int[] xPoints,
int[] yPoints, int nPoints)
• where there are nPoints values in both arrays
• Lines are drawn from (xPoints[0], yPoints[0]) to (xPoints[1], yPoints[1]) and from there to (xPoints[2], yPoints[2]) and so forth
• A line is drawn between the last point and the first point
![Page 51: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/51.jpg)
drawPolygon Example
int[] xs = { 5, 12, 5};
int[] ys = { 7, 20, 20};
bird.drawPolygon( xs, ys, 3 );
5, 7
12, 205, 20
![Page 52: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/52.jpg)
fillPolygon Example
int[] xs = { 5, 12, 5};
int[] ys = { 7, 20, 20};
bird.fillPolygon( xs, ys, 3 );
5, 7
12, 205, 20
![Page 53: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/53.jpg)
Teaching Evaluation
• The official University teaching evaluation survey is on Blackboard
• Complete the survey for ALL classes
• 19% of the students in COMP163 have completed the evaluation
![Page 54: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/54.jpg)
COMP163 Schedule
Mon, Nov 12
More on GUIsWed, Nov 14
Secure ProgrammingFri, Nov 16
review
Mon, Nov 19
Exam 3Wed, Nov 21
Thanksgiving Holiday(no classes)
Fri, Nov 23
Thanksgiving Holiday(no classes)
Mon, Nov 26
Software engineeringWed, Nov 28
final reviewThursday, Nov 29
Lab Final
Tuesday, Dec 4
Final10:30 – 12:30
Exam
![Page 55: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/55.jpg)
![Page 56: More about GUIswilliams.comp.ncat.edu/COMP163/GUIreview.pdf · COMP163 Schedule Mon, Nov 12 More on GUIs Wed, Nov 14 Secure Programming Fri, Nov 16 review Mon, Nov 19 Exam 3 Wed,](https://reader036.fdocuments.net/reader036/viewer/2022081607/5edf5387ad6a402d666aad99/html5/thumbnails/56.jpg)