Download presentation
Presentation is loading. Please wait.
Published byKelly Robbins Modified over 9 years ago
1
Definition: An event is an object thrown in response to a user of programmatic action Definition: A listener is a series of methods that executes in response to an events Definition: An adapter is a class that has default listener methods Implementation: Implement the listener interface or extend an adapter class Create listener methods appropriately Register the listener to a component or a listener manager Note: An alternate implementation is to create an anonymous listener object passed as an argument when registering.
2
Listen for button clicks public class MyPanel extends JPanel implements ActionListener { Jbutton b; public MyPanel() { JButton b = new JButton(“hello”); add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton x = (JButton)(e.getSource()); System.out.println (“it’s a ” + x.getText() + “ button”); } } } Note: A single listener can repond to events from multiple objects
3
Purpose Listen for user keyboard entries Limitation: the component must have focus Implementation Create keyPressed, keyTyped, and keyReleased methods if using the implements KeyListener interface Create only the methods needed if using a KeyAdapter Register the listener with the component
4
class KeyPanel extends JPanel { private int x=100, y=100; String keyChar = ‘x'; public KeyPanel() { setFocusable(true); // Doesn’t work for JApplets addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_DOWN: y+=10; break; case KeyEvent.VK_UP: y-=10; break; case KeyEvent.VK_LEFT: x-=10; break; case KeyEvent.VK_RIGHT: x+=10; break; default: keyChar = “” + e.getKeyChar(); } repaint(); }}); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(keyChar,x,y);}}
5
Differences from KeyListener objects Don’t need to worry about whether a component has focus Monitors keyboard events before they are sent to components with focus The listener method is dispatchKeyEvent, not keyPressed Register the Listener with the system KeyboardFocusManager Useful for applets, because applet JPanels won’t get focus Implementation KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); manager.addKeyEventDispatcher(new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent event) { /* code goes here; return true; } });
6
Mouse Listener Events: Mouse operations public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} Mouse Motion Listener: Movements of the mouse public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} Item Listener: State of an object changed Public void itemStateChanged(ItemEvent e) {} Even more: FocusListener, WindowListener, etc.
7
Definition: A path of execution through a process Any Class can be a thread if it: implements runnable provides a run method (public void run()) A thread starts by calling start(), which in turn calls run() A thread ends when the run method exits Be careful Threads all run concurrently; order of execution is not known One thread can step on variables set in another thread
8
public class MovingPanel extends JPanel implements ActionListener { private String message = “Welcome to Java” private int x=0, y = 20; public MovingPanel() {Timer timer=new Timer(1000,this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(message, x, y); } public void actionPerformed(ActionEvent e) { if (x>getWidth() { x = -20; } x+=5; if (y>=getHeight() – 20) y = 20; y+=5; repaint(); }
9
int data[][] = new int[4][4], count = 1; for (int row=0; row<data.length; row++) for (int col=0; col<data[row].length; col++) data[row][col] = count++; OR int[][] data = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; for (int row=0; row<data.length; row++) { for (int col=0; col<data[row].length; col++) System.out.printf("%4d", data[row][col]); System.out.println(); } [[0][1][2][3] [0]1234 [1]5678 [2]9101112 [3]13141516
10
count=1; int values[][] = new int[4][], cols[] = {2, 4, 1,3}, count = 1; for (int row=0; row<values.length; row++) { values[row] = new int[cols[row]]; for (int col=0; col<values[row].length; col++) values[row][col] = count++; } for (int row=0; row<values.length; row++) { for (int col=0; col<values[row].length; col++) System.out.printf("%4d", values[row][col]); System.out.println(); } Definition: Different rows have different numbers of columnt
11
public double distance(Point first, Point second) { double delta1 = Math.pow(first.x – second.x, 2); double delta2 = Math.pow(first.y – second.y), 2; return Math.sqrt(delta1 + delta2); } Consider a two dimensional array, data Problem: Find the pair of points closest together using the above method.
12
Moving sprites in four dimensions int[][] directions = { {0,1}, {1,0}, {0,-1}, {-1,0}}; Moving sprites in eight directions int[][] directions = { {0,2}, {1,1}, {2,0}, {1,-1}, {0,-2}, {-1, -1}, {0,-2}, { -1,1} }; Moving sprites in sixteen directions int[][] directions = { { 4, 0}, { 3, 1}, { 2, 2}, { 1, 3}, { 0, 4}, {-1, 3}, {-2, 2}, {-3, 1}, {-4, 0}, {-3,-1}, {-2,-2}, {-1,-3}, { 0,-4}, { 1,-3}, { 2,-2}, { 3,-1} }; Questions: What is the relation between the directions and the one that is half away in the array? How would we extend this to twice the size?
13
Divide the panel into row and column cells. Define rules for what happens if predators or avatars intersect the various cells Examples of possible game rules Move them automatically to another place Have the game shift to another room Give the players points or create additional avatars Kill the avatar that hits that particular cells The rules for avatars may vary from that of predators
14
First dimension: month Second dimension: day Third dimension: hour Fourth dimension : Type of measurement [0] = temperature, [1] = humidity Symbolic constants: public final static int T=0, H=1; Questions: To what does the following refer? int x=3, y=4, z=5, k=32, m=50; data[x][y][z][T] = 32; data[x][y][z][H] = 50; How do we instantiate such an array?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.