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

The MenuGrow code and its associated driver, MenuGrowDriver, given below, create

ID: 3573155 • Letter: T

Question


The MenuGrow code and its associated driver, MenuGrowDriver, given below, create a display window and display in that window a square with NW corner at (0,0) and edge side 100. A fully functional menu with one item, Quit, has also been provided with the code (however this button has been disabled in the working version displayed below).


import javax.swing.JMenuBar;
public class MenuGrowDriver{

  public static void main(String[] args){
    DisplayWindow d = new DisplayWindow();
    JMenuBar bar = new JMenuBar();
    d.setJMenuBar(bar);
    MenuGrowSquare p = new MenuGrowSquare(bar);
    d.addPanel(p);
    d.showFrame();
  }
}


import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // needed for event handling

public  class MenuGrowSquare extends JPanel implements ActionListener{

  JMenuBar b;
  JMenu growMenu = new JMenu("Grow");
  JMenuItem quit = new JMenuItem("Quit");
  JMenuItem grow = new JMenuItem("Grow");
  int edge = 100;
  
  public MenuGrowSquare(JMenuBar bar){  
    setPreferredSize(new Dimension(500,500));  
    b = bar;
    b.add(growMenu);
    growMenu.add(quit);    
    quit.addActionListener(this);     
  }
  
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawRect(0,0,edge,edge);
  }
  
public void actionPerformed(ActionEvent e){      
   if (e.getSource() == quit)  System.exit(0); //quit!
}
}

Your job here: given the JMenuItem grow in the MenuGrow class, complete its implementation so that every time grow is chosen from the menu , the window clears and then another square is drawn. Each new square should be 20 pixels on a side greater than the previous square (and thus the initial square is 100 on a side; the one drawn after one menu selection should be 120 pixels on a side; after a second menu selection the square should be 140 on a side, and so forth (the NW corner should remain at (0,0)).

JMenuBar b;
JMenu growMenu = new JMenu("Grow");
JMenuItem quit = new JMenuItem("Quit");
JMenuItem grow = new JMenuItem("Grow");
int edge = 100;

public MenuGrowSquare(JMenuBar bar){
setPreferredSize(new Dimension(500,500));
b = bar;
b.add(growMenu);
growMenu.add(quit);  
quit.addActionListener(this);   
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0,0,edge,edge);
}

public void actionPerformed(ActionEvent e){    
if (e.getSource() == quit) System.exit(0); //quit!
}

}

Explanation / Answer

MenuBar b;
JMenu growMenu = new JMenu("Grow");
JMenuItem quit = new JMenuItem("Quit");
JMenuItem grow = new JMenuItem("Grow");
int edge = 120;
  
public MenuGrowSquare(JMenuBar bar){
setPreferredSize(new Dimension(500,500));
b = bar;
b.add(growMenu);
growMenu.add(quit);
quit.addActionListener(this);
growMenu.add(grow);
grow.addActionListener(this);
  
}
  
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0,0,edge,edge);
}
  
public void actionPerformed(ActionEvent e){
if (e.getSource() == quit) System.exit(0); //quit!

if(e.getSource()==grow){
repaint();
edge=edge+20;

}
}

The code is attached in Bold

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