Lecture 12
- Graphics and GUI. News about the structure of the final exam.
- Graphics in Java are implemented using the Graphics class and other
classes from java.awt.*
- AWT = Abstract Windowing Toolkit
- Most examples in the textbook use an applet to show graphics.
- An applet is a class which extends Applet class. The Applet class
extends Panel. The Panel class extends Container. The Container
class extends Component. The Component class is the super class of
every AWT item.
- This is why applets are good to demonstrate graphics but you can use the
Frame class to demonstrate graphics.
- All applets have the getGraphics method. With this you can get a
reference to an instance of Graphics.
- The Graphics class has a variety of methods for drawing.
- There are methods to draw lines, ovals, rectangles, fill ovals, etc.
- Additionally, there are methods for setting the colour of the text and
lines.
- Up to now we have been using the println method to display text. On a
graphic display you have to use methods like drawString.
- Colours are handled using red, green and blue levels. If you mix these
primary colours you can form other colours. For example, 100% red,
68% green and 68% blue gives you pink.
- Levels are measured on a scale of 0 to 255. 0, 0, 0 is no red,
no green, no blue giving us black. 255, 255, 255 would be
100% red, 100% green, 100% blue.
- Sometimes you want to predefined colour. The Color class gives you this.
For example, Color.blue is a static instance of the Color class set
for blue.
- If I draw two red squares and they overlap, I can have then blend
together so you cannot tell where one square begins and the other
ends.
- Or I can set XOR mode. Where ever the two squares overlap they will
cancel each other out and you will see the background.
- If two objects of different colour overlap then the point where they
overlap will have a contrasting colour.
- Before using the drawString method you can use the setFont method to
pick a font and a font style.
- If you have ever used a paint program (like Paint on Windows95 or
MacPaint on the Apple Macintosh), then you should have an idea
of what shapes the Graphics class can create.
- In a paint program I might pick the draw oval tool, move the
mouse pointer to position 20, 40 (20 pixels from the left side of
the screen and 40 pixels down from the top of the screen), click
the mouse, draw the pointer to 100, 150.
- In an applet I might have:
Graphics g = getGraphics();
g.drawOval(20, 40, 100-20, 150-40);
- Graphics and a graphical user interface (GUI) are different things
though.
- A GUI requires a Component class like all other graphical objects in
Java.
- Rather than drawing the rectangles and circles you create components.
- A list of the components is on page 358 of the text book.
- If you have used the Internet you have probably seen the different
components.
- Image you are at a web site and about to purchase your favourite RPG.
They give you the option to pay by VISA, MasterCard or AMEX. They
would have three RadioButtons. If you pick VISA the other two
options are unselected. If you then pick MasterCard it unselects
VISA.
- You might also have a one line text box which you enter your credit card
number. This would be a TextField.
- If you needed to enter a mailing address (more than one line) you would
have a TextField.
- You probably also see a SUBMIT button. How does the applet know when you
click the SUBMIT button?
- When you create the GUI you also want to create an Listener. A list of
the different Listeners is on page 365.
- You create the GUI, create a Listener and then add the SUBMIT button to
the Listener.
- While the applet is running the Listener is running in the background.
When the user clicks the SUBMIT button the Listener senses the click
and runs its actionPerformed method.
- The actionPerformed could then tell the applet that the user clicked
the SUBMIT button. The applet then quits running and submits all
the user information to a database (or some other such thing).
- You can have a Listener looking for mouse movements, mouse clicks, when
the mouse leaves the window.
- Almost any kind of input can be listened for.
- How do we position all the components?
- The Container class has a setLayout method.
- There are five different layout managers which are listed on page 383 of
the textbook.