Question: When you actually run the bubbles application, what is the name of the
ID: 3627579 • Letter: Q
Question
Question:When you actually run the bubbles application, what is the name of the object that is the listener for the bubbles button in the running application?
import java.awt.*;
2 import javax.swing.*;
3 import java.awt.event.*;
4
5 public class BubblePanel extends JPanel implements
6 ActionListener{
7
8 JButton quit = new JButton("Quit");
9 JButton bubbles = new JButton("Bubbles");
10 JLabel counterLabel = new JLabel("Enter bubble count: ");
11 JTextField counter = new JTextField(5);
12
13 private int count;
14 private Rectangle[] boxes;
15 private BubbleModel m;
16
17 public BubblePanel(BubbleModel m){ // pass reference to model
18 setPreferredSize(new Dimension(700,700));
19 this.m = m;
20 this.add(counterLabel);
21 this.add(counter);
22 this.add(bubbles);
23 this.add(quit);
24 bubbles.addActionListener(this);
25 quit.addActionListener(this);
26 setForeground(Color.blue);
27 }
28
29 public void paintComponent(Graphics g){
30 super.paintComponent(g);
31 drawBubbles(g);
32 }
33
34 public void drawBubbles(Graphics g){
35 if (boxes != null){
36 for(int j = 0; j < boxes.length; j++)
37 g.drawOval(boxes[j].x,boxes[j].y,boxes[j].width,boxes[j].height);
38 }
39 }
40
41 public void actionPerformed(ActionEvent e){
42 if (e.getSource() == quit)
43 System.exit(0);
44 if (e.getSource() == bubbles){
45 count = Integer.parseInt(counter.getText());
46 boxes = m.makeBubbles(count);
47 repaint();
48 }
49 }
50 }
Explanation / Answer
The answer to the second problem is little tricky. They give you the radius, 50, so know that to change to into a height or width we must double that--so 100 is the height and the width. You should take a look at the Java API for drawOval to get the x and y coordinates of the circle. The problem tells us the center of our circle or oval (same thing) is at 280,300. This is not your x and y coordinates. Your x and y coordinates are actually going to be the north west corner or your circle (top left). Since we know the radius of the circle is 50, we can just subtract 50 from each center coordinate to get the coordinates for the NW corner of our circle. And ouila! you have your coordinates for your circle/oval. g.drawOval(230,250,100,100);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.