Title: Swing II
1Swing II
2Swing II Topics
- WindowListener interface
- Icons
- Scrollbars
- The Graphics class
- Colors
- Fonts the drawString method
3WindowListener interface
- Must implement WindowListener interface
(java.awt.event). - All methods receive a WindowEvent
(java.awt.event). - Recall that the Jframe setDefaultCloseOperation
is very limited. - Instead, you may respond in any manner you wish
by implementing the WindowListener inteface and
then by using the setWindowListener method.
4WindowListener interface
- Method summary
- void windowActivated ( WindowEvent e )
- Invoked when the Window is set to be the active
Window. - void windowClosed ( WindowEvent e )
- Invoked when a window has been closed as the
result of calling dispose on the window. - void windowClosing ( WindowEvent e )
- Invoked when the user attempts to close the
window from the window's system menu.
5WindowListener interface
- Method summary
- void windowDeactivated ( WindowEvent e )
- Invoked when a Window is no longer the active
Window. - void windowDeiconified ( WindowEvent e )
- Invoked when a window is changed from a minimized
to a normal state. - void windowIconified ( WindowEvent e )
- Invoked when a window is changed from a normal to
a minimized state.
6WindowListener interface
- Method summary
- void windowOpened ( WindowEvent e )
- Invoked the first time a window is made visible.
- You do not need to fully implement all methods.
You can simply define them with an empty block - public void windowDeiconified ( WindowEvent e )
7Basic WindowListener template
- import java.awt.event.WindowEvent
- import java.awt.event.WindowListener
- public class MyWindowListener implements
WindowListener - public void windowActivated ( WindowEvent e )
- public void windowClosed ( WindowEvent e )
- public void windowClosing ( WindowEvent e )
- public void windowDeactivated ( WindowEvent e )
- public void windowDeiconified ( WindowEvent e )
- public void windowIconified ( WindowEvent e )
- public void windowOpened ( WindowEvent e )
-
-
8Extending JFrame and implementing some Listener
- We have 3 architectural options
- Class A extends JFrame class B implements some
Listener. - Class A extends JFrame and implements some
Listener. - Class A extends JFrame and class A contains an
inner class B that implements some Listener.
9Inner classes
- Ex.
- public class MyOuterClass
-
- private class MyInnerClass
-
-
-
10Inner classes
- Advantages
- Can be used to make the outer class
self-contained. - Inner and outer classes have access to each
others private members. - Private inner classes cannot be accessed by name
outside of the defining outer class.
11Extending JFrame implementing a WindowListener
in an inner class
-
- public class MyFrame extends JFrame
-
- private class MyWindowListener implements
WindowListener - public void windowActivated ( WindowEvent e )
- public void windowClosed ( WindowEvent e )
- public void windowClosing ( WindowEvent e )
- public void windowDeactivated ( WindowEvent e )
- public void windowDeiconified ( WindowEvent e )
- public void windowIconified ( WindowEvent e )
- public void windowOpened ( WindowEvent e )
-
-
- public MyFrame ( )
-
- addWindowListener( new MyWindowListener() )
-
-
-
12WindowAdapter class
- An abstract adapter class for receiving window
events. The methods in this class are empty. This
class exists as convenience for creating listener
objects. - Extend this class to create a WindowEvent
listener and override the methods for the events
of interest. (If you implement the WindowListener
interface, you have to define all of the methods
in it. This abstract class defines null methods
for them all, so you can only have to define
methods for events you care about.) - Create a listener object using the extended class
and then register it with a Window using the
window's addWindowListener method. When the
window's status changes by virtue of being
opened, closed, activated or deactivated,
iconified or deiconified, the relevant method in
the listener object is invoked, and the
WindowEvent is passed to it.
13Icons
- Simply a (typically small) picture.
- JLabels, JButtons, and JMenuItems can have
- icons
- strings
- icons and strings
- nothing at all
- If the item doesnt have any associated text, use
the setActionCommand method to explicitly assign
a command string to the item.
14Icons
- Creating icons from image files
- ImageIcon dukeIcon new ImageIcon(
duke_waving.gif ) - Adding an icon to a label
- JLabel dukeLabel new JLabel( wood check )
- dukeLabel.setIcon( dukeIcon )
- JLabel dukeLabel new JLabel( dukeIcon )
15Icons
- Icons may be used with buttons and menu items
too - JButton happyButton new JButton( happy )
- ImageIcon happyIcon new ImageIcon(
smiley.gif ) - happyButton.setIcon( happyIcon )
16Scrollbars
- Create the object to be scrolled
- JTextArea memo new JTextArea( 15, 30 )
- Create the JScrollPane
- public JScrollPane ( Component objectToBeScrolled
) - Add the JScrollPane to a container such as JPanel
or JFrame.
17Scrollbars
- JScrollPane
- public JScrollPane ( Component objectToBeScrolled
) - public void setHorizontalScrollBarPolicy ( int
policy ) - policies
- JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
- JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
- public void setVerticalScrollBarPolicy ( int
policy ) - policies
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
- JScrollPane.VERTICAL_SCROLLBAR_NEVER
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
18The Graphics class
- Coodinate system and origin
- (0,0) ----------gt x
-
-
- v
- y
- JFrames paint method draws whatever needs to be
drawn. - public void paint ( Graphics g )
19Graphics class methods
- Method summary
- void clearRect ( int x, int y, int width, int
height ) - Clears the specified rectangle by filling it with
the background color of the current drawing
surface. - void draw3DRect ( int x, int y, int width, int
height, boolean raised ) - Draws a 3-D highlighted outline of the specified
rectangle. - void drawArc ( int x, int y, int width, int
height, int startAngle, int arcAngle ) - Draws the outline of a circular or elliptical arc
covering the specified rectangle.
20Graphics class methods
- Method summary
- void drawLine ( int x1, int y1, int x2, int y2 )
- Draws a line, using the current color, between
the points (x1, y1) and (x2, y2) in this graphics
context's coordinate system. - void drawOval ( int x, int y, int width, int
height ) - Draws the outline of an oval.
- void drawPolygon ( int xPoints, int yPoints,
int nPoints ) - Draws a closed polygon defined by arrays of x and
y coordinates. - void drawPolyline ( int xPoints, int yPoints,
int nPoints ) - Draws a sequence of connected lines defined by
arrays of x and y coordinates.
21Graphics class methods
- Method summary
- void drawRect ( int x, int y, int width, int
height ) - Draws the outline of the specified rectangle.
- void drawRoundRect ( int x, int y, int width, int
height, int arcWidth, int arcHeight ) - Draws an outlined round-cornered rectangle using
this graphics context's current color. - void drawString ( String str, int x, int y )
- Draws the text given by the specified string,
using this graphics context's current font and
color. - void fill3DRect ( int x, int y, int width, int
height, boolean raised ) - Paints a 3-D highlighted rectangle filled with
the current color.
22Graphics class methods
- Method summary
- void fillArc ( int x, int y, int width, int
height, int startAngle, int arcAngle ) - Fills a circular or elliptical arc covering the
specified rectangle. - void fillOval ( int x, int y, int width, int
height ) - Fills an oval bounded by the specified rectangle
with the current color. - void fillPolygon ( int xPoints, int yPoints,
int nPoints ) - Fills a closed polygon defined by arrays of x and
y coordinates. - void fillPolygon ( Polygon p )
- Fills the polygon defined by the specified
Polygon object with the graphics context's
current color. - void fillRect ( int x, int y, int width, int
height ) - Fills the specified rectangle.
23Graphics class methods
- Method summary
- void fillRoundRect ( int x, int y, int width, int
height, int arcWidth, int arcHeight ) - Fills the specified rounded corner rectangle with
the current color. - Color getColor ( )
- Gets this graphics context's current color.
- Font getFont ( )
- Gets the current font.
- FontMetrics getFontMetrics ( )
- Gets the font metrics of the current font.
- FontMetrics getFontMetrics ( Font f )
- Gets the font metrics for the specified font.
24Graphics class methods
- Method summary
- void setColor ( Color c )
- Sets this graphics context's current color to the
specified color. - void setFont ( Font font )
- Sets this graphics context's font to the
specified font. - void translate ( int x, int y )
- Translates the origin of the graphics context to
the point (x, y) in the current coordinate system.
25Drawing with the graphics class
-
- public class Face extends JFrame
-
- public void paint ( Graphics g )
- super.paint( g )
- g.setColor( Color.BLUE )
- g.drawLine( 55, 60, 75, 60 )
-
-
-
Typically, one overrides paint but never calls it
directly. Additionally, there is a repaint
method. Typically, one calls repaint when it is
necessary to redraw the contents of the frame
(but one never overrides repaint).
26Colors
- We saw how we can call setColor in our paint
method to change colors. - We may use the predefined colors such as
Color.BLUE, Color.CYAN, etc. - Colors are represented by triples of RGB values
- Color brown new Color( 200, 150, 0 )
- where each int component is in 0..255
27Colors
- Other useful methods
- public Color brighter ( )
- Returns a brighter version of the calling
objects color. - public Color darker ( )
- Returns a darker version of the calling objects
color. - new Color ( 0.5f, 1.0f, (float)0.2 )
- Where each float rgb component is in 0.0 .. 1.0
28Colors
- Color c JColorChooser.showDialog( null,
"JColorChooser", null )
29Fonts the drawString method
- Recall the drawString method
- g.drawString( hello, x, y )
- To change the current font
- Font f new Font( SansSerif, Font.PLAIN, 24
) -
- g.setFont( f )
- g.drawString( hello, x, y )
30Fonts the drawString method
- Say the user picks a different font from a menu.
- What sequence of events occur?
- How do we then change the appearance of the text
in our paint/drawing program?
31Additional information
32Additional listeners
- MouseListener
- MouseMotionListener
- MouseWheelListener
- KeyListener
- WindowStateListener
- WindowFocusListener
33MouseListener interface
- Method summary
- void mouseClicked ( MouseEvent e )
- Invoked when the mouse button has been clicked
(pressed and released) on a component. - void mouseEntered ( MouseEvent e )
- Invoked when the mouse enters a component.
- void mouseExited ( MouseEvent e )
- Invoked when the mouse exits a component.
34MouseListener interface
- Method summary
- void mousePressed ( MouseEvent e )
- Invoked when a mouse button has been pressed on a
component. - void mouseReleased ( MouseEvent e )
- Invoked when a mouse button has been released on
a component. - Also MouseAdapter MouseInputAdapter classes.
35MouseEvent class
- Method summary
- int getButton ( )
- Returns which, if any, of the mouse buttons has
changed state. - int getClickCount ( )
- Returns the number of mouse clicks associated
with this event. - int getX ( )
- Returns the horizontal x position of the event
relative to the source component.
36MouseEvent class
- Method summary
- int getY ( )
- Returns the vertical y position of the event
relative to the source component. - String paramString ( )
- Returns a parameter string identifying this
event. - int getModifiersEx ( ) (inherited from
InputEvent) - Returns the extended modifier mask for this
event. Extended modifiers represent the state of
all modal keys, such as ALT, CTRL, META, and the
mouse buttons just after the event occurred.
37MouseMotionListener interface
- Method summary
- void mouseDragged ( MouseEvent e )
- Invoked when a mouse button is pressed on a
component and then dragged. - void mouseMoved ( MouseEvent e )
- Invoked when the mouse cursor has been moved onto
a component but no buttons have been pushed. - Also MouseMotionAdapter MouseInputAdapter
classes.
38MouseWheelListener interface
- Method summary
- void mouseWheelMoved ( MouseWheelEvent e )
- Invoked when the mouse wheel is rotated.
- No MouseWheelAdapter!
39MouseWheelEvent class
- Method summary
- int getScrollAmount ( )
- Returns the number of units that should be
scrolled in response to this event. - int getWheelRotation ( )
- Returns the number of "clicks" the mouse wheel
was rotated.
40KeyListener interface
- Added to a component by its addKeyListener
method. - KeyAdapter class available as well.
- Method summary
- void keyPressed ( KeyEvent e )
- Invoked when a key has been pressed (low level).
- void keyReleased ( KeyEvent e )
- Invoked when a key has been released (low level).
- void keyTyped ( KeyEvent e )
- Invoked when a key has been typed (higher level).
41KeyEvent class
- Indicates what key was pressed.
- Defines constants for function and arrow keys.
- Allows one to detect if modifiers (Alt, Ctrl,
Shift) were also pressed.
42WindowStateListener interface
- Method summary
- void windowStateChanged ( WindowEvent e )
- Invoked when window state is changed.
- WindowEvent WindowAdapter classes were already
discussed.
43WindowFocusListener interface
- Method summary
- void windowGainedFocus ( WindowEvent e )
- Invoked when the Window is set to be the focused
Window, which means that the Window, or one of
its subcomponents, will receive keyboard events. - void windowLostFocus ( WindowEvent e )
- Invoked when the Window is no longer the focused
Window, which means that keyboard events will no
longer be delivered to the Window or any of its
subcomponents. - WindowEvent WindowAdapter classes were already
discussed.