Question: //Chapter 16, GRAPHICS; Java programming, joyce Farrell, 8th ed //output //Debug; need to fix error; applied to chapter 16. //different size of round 20 and
//Chapter 16, GRAPHICS; Java programming, joyce Farrell, 8th ed
//output
//Debug; need to fix error; applied to chapter 16.
//different size of round 20 and 10.
1 // The panel should change colorwhen user's mouse enters or exits it 2 // A circle should appear wherever the user clicks 3 // The circle becomes smaller when the user double-clicks 4 5 import javax.swing.*; 6 import java.awt.*; 7 import java.awt.Color; 8 import java.awt.event.*; 9 10 public class DebugSixteen2 extends JPanel implements MouseListener 11 { 12 int x, y; 13 int size; 14 15 public DebugSixteen2() 16 { 17 addMouseListener(); 18 } 19 public static void main(String[] args) 20 { 21 JFrame frame = new JFrame(); 22 frame.add(new DebugSixteen2()); 23 frame.setSize(250, 150); 24 frame.setVisible(true); 25 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 } 27 28 @Override 29 public void mousePressed(MouseEvent e) 30 { 31 x = e.getX(); 32 y = e.gety(); 33 } 34 35 @Override 36 public void mouseClicked(MouseEvent e) 37 { 38 if(e.getClickCount() == 2) 39 size = 20; 40 else 41 size = 20; 42 repaint(); 43 } 44 @Override 45 public void mouseEntered(MouseEvent e) 46 { 47 setBackground(Color.ORANGE); 48 } 49 @Override 50 public void mouseExited(MouseEvent e) 51 { 52 } 53 @Override 54 public void mouseReleased(MouseEvent e) 55 { 56 } 57 @Override 58 public void paintComponent(Graphics g) 59 { 60 super.paintComponent(g); 61 g.drawOval(x - size, y - size, size * 2, size * 2); 62 } 63 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
