Unit 3 Event Driven Programming
Unit 3 Event Driven Programming
Unit 3 Event Driven Programming
(x, y)
+y
y -Axis
Java 2D API
• Java 2D API
– Provides advanced 2D graphics capabilities
– Part of the Java 2 Platform, Standard Edition
– Includes features for processing line art, text and images
– Class java.awt.Graphics2D
• Enables drawing with Java 2D API
• Offers three graphics primitives: images, text and shapes
• java.awt.Graphics subclass
• Contains seven state attributes
– Clipping, compositing, font, paint, rendering hints, stroke and transforms
Java 2D Shapes
• Java 2D shape classes
– Package java.awt.geom
• Ellipse2D.Double
• Line2D.Double
• Rectangle2D.Double
• RoundRectangle2D.Double
• Arc2D.Double
Java 2D Image Processing
• Image processing
– Manipulation of digital images by applying filters
• Filters – mathematical operations that change images
– Compression filters
» Reduce digital image’s memory usage
– Measurement filters
» Collect data from digital images
– Enhancement filters
» Alter certain physical aspects of an image
• import java.awt.*;
• Assemble the GUI
– use GUI components,
• basic components (e.g., Button, TextField)
• containers (Frame, Panel)
– set the positioning of the components
• use Layout Managers
• Attach events
A sample GUI program
Import java.awt.*;
class MyGui {
public static void main(String [] s ) {
Frame f = new Frame (“My Frame”);
Button b = new Button(“OK”);
TextField tf = new TextField(“George”, 20);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
}
}
output
Events
b.addActionListener( );
Button
method to add a listener listener object
Frame
f.addWindowListener( );
Events
• Each GUI component (e.g., a Button) that wishes to
respond to an event type (e.g., click), must register an
event handler, called a Listener.
• The listener is an object of a "Listener" interface.
• A Listener class can be created by subclassing
(through "implements") one of Listener interfaces (all
listener inrefaces are in the java.awt.event package =
> must import java.awt.event.*; )
• The registration of the listener is done by a call to a
method such as addActionListener(<Listener Object>).
Each GUI component class has one or more such
add…() methods, where applicable.
Listener Interfaces
INTERFACE NAME (IN JAVA.AWT.EVENT )
[1] ActionListener
[2] ItemListener
[3] MouseMotionListener
[4] MouseListener
[5] KeyListener
[6] FocusListener
[7] AdjustmentListener
[8] ComponentListener
[9] WindowListener
[10] ContainerListener
[11] TextListener
Listener Interfaces
Each listener interface has methods that need to be
implemented for handling different kinds of events.
We cannot use
• AppletContext
• “Applet” derived from AWT Panel
• Hooks into Browser environment
• Can be used to link to another Web page
A sample Applet
// HelloApplet.java: for processing applet methods
import java.awt.*;
import java.applet.*;
public class HelloApplet extends Applet
{
public void init() {
setBackground(Color.yellow);
System.out.println("init() method invoked");
}
public void start()
{
System.out.println("start() method invoked");
}
public void paint( Graphics g )
{
System.out.println("paint() method invoked");
g.drawString( "Hi there", 24, 25 );
}
public void stop()
{
System.out.println("stop() method invoked");
}
}
sample Applet
another sample Applet (run in Applet Viewer)
sample Applet
running within Netscape
sample Applet code
import java.applet.*; // for Applet class
import java.awt.*; // for Graphics class
public class MyApplet extends Applet {
public void paint( Graphics g ) {
g.drawString("Hi there", 40, 40);
g.drawOval(40, 60, 45, 45);
g.drawRect(100, 60, 50, 50);
g.drawLine(170, 60, 250, 170);
} // end paint()
public void init() {
setBackground(Color.yellow);
}
} // end class MyApplet
Another example
// MyApplet.java: draws rectangle with yellow color fill
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
public synchronized void paint(Graphics g)
{
int x,y,width,height;
Dimension dm = size();
x = dm.width/4;
y = dm.height / 4;
width = dm.width / 2;
height = dm.height / 2;
// Draw the rectangle in the center with colors!
g.setColor(Color.blue);
g.drawRect(x,y,width,height);
g.setColor(Color.yellow);
g.fillRect(x + 1,y + 1,width - 2,height - 2);
}
}
Applet method execution
As soon as the browser (or Appletviewer) accesses the page that contains the
applet:
… … … (MyAppletSound.java)
how to do that ….
• Delegate-model architecture
– Variant of MVC
– Combines the view and controller into a single object - delegate
– The delegate provides both a graphical presentation of the
model and an interface for modifying the model
• Example
– Every JButton has an associated ButtonModel.
• The JButton is the delegate.
• The ButtonModel maintains state information (button is pressed or
enabled) and list of ActionListeners.
• The JButton provides a graphical presentation and modifies the
ButtonModel’s state
Class AbstractAccountView
• Method updateDisplay() is marked abstract,
requiring each AbstractAccountView subclass
to provide an appropriate implementation for
displaying the Account information
• update() invokes updateDisplay() each time
an Account notifies the AbstarctAccountView
of a change
• Interface Observer defines method update,
which takes as an argument a reference to an
Observable instance that issued the update
• An Observable object issues an update by
invoking notifyObservers() of class Observable
• notifyObservers() invoke update() for each
registered Observer
• An Observer can listen for updates from
multiple Observable objects
Class AccountController
• AccountController implements the controller
in the MVC architecture
• AccountController provides a user interface
for modifying Account data
• AccountController extends JPanel because it
provides a set of GUI components for
depositing and withdrawing Account funds
Swing Components
• Swing is a collection of libraries that contains
primitive widgets or controls used for designing
Graphical User Interfaces (GUIs).
Component
Container
JComponent Window
JPanel Frame
JFrame
Adding components
• Once a component is created, it can be added
to a container by calling the container’s add
method:
Container cp = getContentPane();
cp.add(new JButton(“cancel”));
cp.add(new JButton(“go”));