6 7 17 DOO 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class SecretMessage extend
Posted: Sun May 15, 2022 2:07 pm
6 7 17 DOO 1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class SecretMessage extends JPanel implements MouseMotionListener, MouseListener { int x = 10; 8 int y = 10; 9 int w = 30; 10 int h = 30; 11 boolean getBall - false; 12 JLabel 1 = new JLabel("Secret message"); 13 e SecretMessage() { 14 setBackground (Color.gray); 15 1. setForeground (Color.gray); 16 add (1); this.addMouseMotionListener(this); 18 this.addMouseListener(this); 19 } 20 protected void paintComponent (Graphics g) { 21 super.paint Component (g); 22 g.setColor (Color.black); 23 g.fillRect (270, 250, 50, 50); 24 g.setColor (Color.white); 25 g.filloval(x, y, w, h); 26 } 27 public void mouseDragged(MouseEvent e) { 28 if (getBall) 29 x = e.getX() - (w/2); 30 y = e.getY() - (h/2); 31 repaint(); 32 } 33 } 34 public void mouseMoved (MouseEvent e) O 35 public void mouse Pressed(MouseEvent e) { 36 if((e.getX() >x) && (e.getX()<x+w) 37 && (e.getY() >y) - (e.getY()<y+h)) { 38 getBall = true; } else { 40 getBall - false; 41 } 42 43 public void mouseReleased (MouseEvent e) { 44 if (getBall) 45 if((e.getX()>270) (e.getX()<320) && (e.getY()>250) && (e.getY()<300)) { 47 setBackground (Color.black); 48 } else { 49 setBackground (Color.gray); 50 } 51 52 HHHHHHHHHHNNNNNNNNNNMmmmmmmmmmninin Im OO 0000 Moto co Jo un w O o co Jo un co Jo o co o o o o 39 46
oh oh oh oh o un un un un un un un ODCO 53 54 55 56 57 58 59 60 61 62 63 64 public void mouseClicked (MouseEvent e) {} public void mouseExited (MouseEvent e) U public void mouseEntered(MouseEvent e) {} public static void main(String[] args) { JFrame f = new JFrame ("Secret Message"); f.add(new SecretMessage()); f.setLocation (40,150); f.setSize(350,350); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; } 7.1 Name the methods that must be overridden when implementing MouseListener. (Your scores will be deducted for incorrect answers.) (1 point) 7.2 Describe what happens when a user releases the mouse button inside the black box (the user may press the mouse button somewhere on the panel). You will get full scores only when your description covers all possible cases. (6 points)
oh oh oh oh o un un un un un un un ODCO 53 54 55 56 57 58 59 60 61 62 63 64 public void mouseClicked (MouseEvent e) {} public void mouseExited (MouseEvent e) U public void mouseEntered(MouseEvent e) {} public static void main(String[] args) { JFrame f = new JFrame ("Secret Message"); f.add(new SecretMessage()); f.setLocation (40,150); f.setSize(350,350); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; } 7.1 Name the methods that must be overridden when implementing MouseListener. (Your scores will be deducted for incorrect answers.) (1 point) 7.2 Describe what happens when a user releases the mouse button inside the black box (the user may press the mouse button somewhere on the panel). You will get full scores only when your description covers all possible cases. (6 points)