Java Software Solutions - Chapter 8: Arrays
Arrays are objects that help us organize large amounts of information
Chapter 8 focuses on:
array declaration and use
bounds checking and capacity
arrays that store object references
variable length parameter lists
multidimensional arrays
the ArrayList class
polygons and polylines
mouse events and keyboard events
-------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} } } Mouse Events Rubberbanding is the visual effect in which a shape is "stretched" as it is drawn using the mouse The following example continually redraws a line as the mouse is dragged See RubberLines.java See RubberLinesPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // RubberLines.java Author: Lewis/Loftus // // Demonstrates mouse events and rubberbanding. //******************************************************************** import javax.swing.JFrame; public class RubberLines { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Rubber Lines"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new RubberLinesPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // RubberLines.java Author: Lewis/Loftus // // Demonstrates mouse events and rubberbanding. //******************************************************************** import javax.swing.JFrame; public class RubberLines { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Rubber Lines"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new RubberLinesPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // RubberLinesPanel.java Author: Lewis/Loftus // // Represents the primary drawing panel for the RubberLines program. //******************************************************************** import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; public class RubberLinesPanel extends JPanel { private Point point1 = null, point2 = null; //----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. //----------------------------------------------------------------- public RubberLinesPanel() { LineListener listener = new LineListener(); addMouseListener (listener); addMouseMotionListener (listener); setBackground (Color.black); setPreferredSize (new Dimension(400, 200)); } continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Draws the current line from the initial mouse-pressed point to // the current position of the mouse. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); page.setColor (Color.yellow); if (point1 != null && point2 != null) page.drawLine (point1.x, point1.y, point2.x, point2.y); } //***************************************************************** // Represents the listener for all mouse events. //***************************************************************** private class LineListener implements MouseListener, MouseMotionListener { //-------------------------------------------------------------- // Captures the initial position at which the mouse button is // pressed. //-------------------------------------------------------------- public void mousePressed (MouseEvent event) { point1 = event.getPoint(); } continue Copyright © 2012 Pearson Education, Inc. continue //-------------------------------------------------------------- // Gets the current position of the mouse as it is dragged and // redraws the line to create the rubberband effect. //-------------------------------------------------------------- public void mouseDragged (MouseEvent event) { point2 = event.getPoint(); repaint(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} } } Key Events A key event is generated when the user types on the keyboard Listeners for key events are created by implementing the KeyListener interface A KeyEvent object is passed to the appropriate method when a key event occurs Copyright © 2012 Pearson Education, Inc. key pressed a key on the keyboard is pressed down key released a key on the keyboard is released key typed a key on the keyboard is pressed down and released Key Events The component that generates a key event is the one that has the current keyboard focus Constants in the KeyEvent class can be used to determine which key was pressed The following example "moves" an image of an arrow as the user types the keyboard arrow keys See Direction.java See DirectionPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Direction.java Author: Lewis/Loftus // // Demonstrates key events. //******************************************************************** import javax.swing.JFrame; public class Direction { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Direction"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new DirectionPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Direction.java Author: Lewis/Loftus // // Demonstrates key events. //******************************************************************** import javax.swing.JFrame; public class Direction { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Direction"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new DirectionPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // DirectionPanel.java Author: Lewis/Loftus // // Represents the primary display panel for the Direction program. //******************************************************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DirectionPanel extends JPanel { private final int WIDTH = 300, HEIGHT = 200; private final int JUMP = 10; // increment for image movement private final int IMAGE_SIZE = 31; private ImageIcon up, down, right, left, currentImage; private int x, y; continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Constructor: Sets up this panel and loads the images. //----------------------------------------------------------------- public DirectionPanel() { addKeyListener (new DirectionListener()); x = WIDTH / 2; y = HEIGHT / 2; up = new ImageIcon ("arrowUp.gif"); down = new ImageIcon ("arrowDown.gif"); left = new ImageIcon ("arrowLeft.gif"); right = new ImageIcon ("arrowRight.gif"); currentImage = right; setBackground (Color.black); setPreferredSize (new Dimension(WIDTH, HEIGHT)); setFocusable(true); } continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Draws the image in the current location. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); currentImage.paintIcon (this, page, x, y); } //***************************************************************** // Represents the listener for keyboard activity. //***************************************************************** private class DirectionListener implements KeyListener { //-------------------------------------------------------------- // Responds to the user pressing arrow keys by adjusting the // image and image location accordingly. //-------------------------------------------------------------- public void keyPressed (KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: currentImage = up; y -= JUMP; break; continue Copyright © 2012 Pearson Education, Inc. continue case KeyEvent.VK_DOWN: currentImage = down; y += JUMP; break; case KeyEvent.VK_LEFT: currentImage = left; x -= JUMP; break; case KeyEvent.VK_RIGHT: currentImage = right; x += JUMP; break; } repaint(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void keyTyped (KeyEvent event) {} public void keyReleased (KeyEvent event) {} } } Summary Chapter 8 has focused on: array declaration and use bounds checking and capacity arrays that store object references variable length parameter lists multidimensional arrays The ArrayList class polygons and polylines mouse events and keyboard events Copyright © 2012 Pearson Education, Inc.
File đính kèm:
- LESSON_08_Arrays.pptx