Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are two questions 1. When you actually run the bubbles application, what i

ID: 3627610 • Letter: T

Question

There are two questions
1. 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?

1 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 }


2. Look at the paintComponent method in the TryoutPanel class one more time.

In the box provided below, add a statement to paintComponent that draws a circle on the screen with center at (280,300) and radius 50. (Hint: a circle is an oval with the same width and height. The center of this circle is 50 pixels below and 50 pixels to the right of the NW corner of this oval).

public void paintComponent(Graphics g) {
super.paintComponent(g);
setForeground(myColor);
ENTER CODE HERE

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);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote