q = 2
000 8 9 15 19 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class MovingRect extends JPanel 6 implements ActionListener { 7 int y - 150; Timer timer = new Timer (1, this); boolean moveUp - true; 10 int rectHeight 50; 11 int rectWidth = 50; 12 MovingRect() { 13 timer.start(); 14 } protected void paintComponent (Graphics g) { 16 super.paint Component (g); 17 g.fillRect (getWidth()/2-rectWidth/2, 18 y, rectWidth, rectHeight); } 20 public void actionPerformed (ActionEvent e) { 21 if(y0) 22 moveUp = false; 23 } 24 if(y > getHeight() - rectHeight) { 25 moveUp - true; 26 } 27 if (moveUp) 28 Y--; 29 else y++; 31 repaint(); 32 33 public static void main(String[] args) { 34 JFrame frame = new JFrame(); 35 frame.add(new MovingRect()); 36 frame.setTitle ("Moving Rectangle"); 37 frame.setSize(400,400); 38 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; 39 frame.setLocationRelativeTo (null); 40 frame.setVisible(true); 41 42 نام و نام و ر ا ا ا ا ا ا ا ا ا ا ل لمر ما لا نه اما ا مر ا ه ه ه oo co o on a w Jноо со O COON 30
8 9 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class MovingRect2 extends MovingRect 6 implements MouseListener { 7 MovingRect2() { this.addMouseListener(this); } 10 public void mouseEntered (MouseEvent e) 0) 11 public void mouseExited (MouseEvent e) {} 12 public void mouse Pressed (MouseEvent e) { 13 timer.stop(); 14 } 15 public void mouseReleased (MouseEvent e) { 16 timer.start(); 17 } public void mouseClicked (MouseEvent e) } public static void main(String[] args) { 20 JFrame frame = new JFrame(); 21 frame.add(new MovingRect2()); 22 frame.setTitle ("Moving Rectangle .. now pauseable"); 23 frame.setSize(400,400); 24 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 25 frame.setLocationRelativeTo (null); 26 frame.setVisible(true); 27 28 } 18 ол ол м н о о co o o o o o o c 19 1 import java.awt.event.MouseEvent; 2 import javax.swing.JFrame; 3 4 public class MovingRect3 extends MovingRect24 public void mousePressed (MouseEvent e) { 5 6 coonWNOOCWNE 10 11 12 13 14 15 16 17 18 } public static void main(String[] args) { JFrame frame - new JFrame(); frame.add(new MovingRect3()); frame.setTitle ("Moving Rectangle .. now pauseable 2"); frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo (null); frame.setVisible(true); 19
10.1 Describe what happens after the class MovingRect is run. (1 point) 10.2 Sketch the output with coordinates after the class MovingRect is run for q milliseconds, where q is the last two digits of your student ID. (For example, if your student ID is 6022300153, then q is 53.) (3 points) Note: You can assume that the execution time of each statement in the class is negligible compared to 1 millisecond. 10.3 Complete the class Moving Rect3 (inherited from the class MovingRect2) such that the moving box is paused only when the user presses the mouse inside it. Explain how your added statements work comprehensively and concisely. (3 points) 10.4 Identify the listeners used in MovingRect, MovingRect2, and MovingRect3, respectively. (1 point) 10.5 Name all source objects that fire an EventObject object (or an object inherited from EventObject) listened by the listener in MovingRect2. (2 points)
000 8 9 15 19 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class MovingRect extend
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
000 8 9 15 19 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class MovingRect extend
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!