{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L26 - Computer Science 101 SS C Lecture 26 Contents 2...

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

View Full Document Right Arrow Icon
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 fires ActionEvents each followed by a specified delay. The Timer object we use in CS101 is defined in the javax.swing package.
Image of page 1

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

View Full Document Right Arrow Icon
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 .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern