L26 - CompSci101 Lecture 27 1 Computer Science 101 SS C...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci101 Lecture 27 1 Computer Science 101 SS C Lecture 26 Contents An example using a JButton to move a shape around the JPanel area The Timer object Examples using a Timer object to move a shape around the JPanel area Coursebook: 17 2 Example 1 - Button moves object This JPanel uses a JButton to move a circle about the JPanel area. The direction in which the circle moves is changed using the arrow keys. Note: In this example pressing the arrow keys CHANGES the direction in which the circle is moving but it DOES NOT CHANGE the position of the circle. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3 Example 1 - Button moves object import java.awt.*;import javax.swing.*; import java.awt.event.*; public class L26JPanel01 extends JPanel implements ActionListener , KeyListener { private static final int UP = 0; private static final int DOWN = 1; private static final int RIGHT = 2; private static final int LEFT = 3; private static final int SIZE = 50; private JButton moveB; private int direction = RIGHT; private int left = 200, top = 200; public L26JPanel01() { addKeyListener(this); moveB = new JButton("MOVE"); moveB .addActionListener(this); add(moveB); } 4 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Example 1 - Button moves object public void actionPerformed(ActionEvent e) { } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillOval(left, top, SIZE, SIZE); } else if (direction == LEFT) { left = left - SIZE; } else if (direction == RIGHT) { left = left + SIZE; } else if (direction == DOWN) { top = top + SIZE; } if (direction == UP) { top = top - SIZE; } //get focus away from moveB requestFocusInWindow(); //back to the JPanel repaint(); 5 35 36 37 38 39 40 41 42 43 44 45 46 47 48 Example 1 - Button moves object public void keyPressed(KeyEvent e) { } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { direction = RIGHT; } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { direction = LEFT; } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { direction = DOWN; } if (e.getKeyCode() == KeyEvent.VK_UP) { direction = UP; } 6 Timer object An instance of the Timer class can be used to create some animation in our JPanels. The javax.swing.Timer class allows you to schedule periodic actions. A Timer object continuously Fres ActionEvents each followed by a speciFed delay. The Timer object we use in CS101 is deFned in the javax.swing package. CompSci101 Lecture 27 2 7 Timer object - invisible button A Timer object is like an invisible button which is being pressed over and over again causing the actionPerformed() method to be called repeatedly ....
View Full Document

Page1 / 5

L26 - CompSci101 Lecture 27 1 Computer Science 101 SS C...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online