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

JAVA PROGRAMMING. (GUI) Please fill in the missing code in the designated areas

ID: 3717738 • Letter: J

Question

JAVA PROGRAMMING. (GUI)

Please fill in the missing code in the designated areas that says "Student provides missing code......" below.

Please also provide screenshot of the GUI compiled once complete. I have provided a sample one which should give an idea.

//-----------------------------------------------------------

// Chapter #35, Problem #1

// Problem1.java

//-----------------------------------------------------------

import javax.swing.*;

public class Problem1

{

   public static void main(String[] args)

   {

      GUI gui = new GUI();

      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      gui.setBounds(100,100,700,700);

      gui.setVisible(true);

   }

}

//-----------------------------------------------------------

// Chapter #35, Problem #1

// GUI.java

//-----------------------------------------------------------

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//----------------------------------------------------

class DOT

//----------------------------------------------------

{

   public Color color;

   public int x;

   public int y;

   public DOT(Color color,int x,int y)

   {

      this.color = color;

      this.x = x;

      this.y = y;

   }

};

//----------------------------------------------------

class TEXT

//----------------------------------------------------

{

   public Color color;

   public int x;

   public int y;

   public String text;

   public TEXT(Color color,int x,int y)

   {

      this.color = color;

      this.x = x;

      this.y = y;

      text = new String("");

   }

};

//----------------------------------------------------

public class GUI extends JFrame

//----------------------------------------------------

{

   private final int DOTSIZE = 4;

   private final int MAXIMUMDOTS = 10000;

   private final int MAXIMUMTEXTS = 100;

   private final Color COLORS[] =

   {

      Color.BLACK,

      Color.RED,

      Color.GREEN,

      Color.BLUE

   };

   private JMenuBar menuBar;

   private JMenu fileMenu;

   private JMenuItem clearMenuItem;

   private JMenuItem aboutMenuItem;

   private JMenuItem exitMenuItem;

   private JMenu modeMenu;

   private JCheckBoxMenuItem modeMenuItems[] = new JCheckBoxMenuItem[2];

   private ModeEventHandler modeEventHandler = new ModeEventHandler();

   private ButtonGroup modeButtonGroup = new ButtonGroup();

   private String modeNames[] = { "Draw","Text" };

   private char mode = 'D';

   private JMenu colorMenu;

   private JRadioButtonMenuItem colorMenuItems[] = new JRadioButtonMenuItem[4];

   private ColorEventHandler colorEventHandler = new ColorEventHandler();

   private ButtonGroup colorButtonGroup = new ButtonGroup();

   private String colorNames[] = { "Black","Red","Green","Blue" };

   private Color color = Color.RED;

   private DrawingPanel drawingPanel;

   private DOT dots[] = new DOT [MAXIMUMDOTS+1];

   private int dotCount;

   private TEXT texts[] = new TEXT [MAXIMUMTEXTS+1];

   private int textCount;

   //----------------------------------------------------

   public GUI()

   //----------------------------------------------------

   {

      super("Chapter #35, Problem #1");

   // File menu

      fileMenu = new JMenu("File");

      fileMenu.setMnemonic('F');

   // File | Clear

      clearMenuItem = new JMenuItem("Clear");

      fileMenu.add(clearMenuItem);

Student provides code for definition of anonymous ActionListener inner-class to handle "Clear" menu selection

   // File | About...

      aboutMenuItem = new JMenuItem("About...");

Student provides code for definition of anonymous ActionListener inner-class to handle "About" menu selection

   // File | Exit

      exitMenuItem = new JMenuItem("Exit");

Student provides code for definition of anonymous ActionListener inner-class to handle "Exit" menu selection

   // Mode menu

   // Mode | { Draw,Text }

Student provides code to set up "Mode" menu items

   // Color menu

   // Color | { Black,Red,Green,Blue }

Student provides code to set up "Color" menu items

      menuBar = new JMenuBar();

      setJMenuBar(menuBar);

      menuBar.add(fileMenu);

      menuBar.add(modeMenu);

      menuBar.add(colorMenu);

     dotCount = 0;

      textCount = 0;

      drawingPanel = new DrawingPanel();

      add(drawingPanel,BorderLayout.CENTER);

      drawingPanel.addMouseMotionListener( new MouseHandler1() );

      drawingPanel.addMouseListener( new MouseHandler2() );

   // *Note* The KeyHandler() does not handle key events properly when it is

   //    associated with the drawingPanel and not the GUI JFrame object.

      addKeyListener( new KeyHandler() );

   }

   //----------------------------------------------------

   private class ModeEventHandler implements ItemListener

   //----------------------------------------------------

   {

      public void itemStateChanged(ItemEvent event)

      {

Student provides code to change mode as needed

      }

   }

   //----------------------------------------------------

   private class ColorEventHandler implements ActionListener

   //----------------------------------------------------

   {

      public void actionPerformed(ActionEvent event)

      {

Student provides code to change color as needed

      // Any change of color resets Text Mode to Draw.

         if ( mode == 'T' )

         {

            mode = 'D';

            modeMenuItems[0].setSelected(true);

         }

      }

   }

   //----------------------------------------------------

   private class MouseHandler1 extends MouseMotionAdapter

   //----------------------------------------------------

   {

      //----------------------------------------------------

      public void mouseDragged(MouseEvent event)

      //----------------------------------------------------

      {

Student provides code to handle each mouse-dragged event when in (D)raw mode

     }

   }

   //----------------------------------------------------

   private class MouseHandler2 extends MouseAdapter

   //----------------------------------------------------

   {

      //----------------------------------------------------

      public void mouseClicked(MouseEvent event)

      //----------------------------------------------------

      {

Student provides code to handle mouse-clicked event when in (T)ext mode

      }

   }

   //----------------------------------------------------

   private class KeyHandler extends KeyAdapter

   //----------------------------------------------------

   {

      public void keyTyped(KeyEvent event)

      {

         if ( mode == 'T' )

         {

            texts[textCount].text = new String(texts[textCount].text + event.getKeyChar());

            drawingPanel.repaint();

         }

      }

   }

   //----------------------------------------------------

   private class DrawingPanel extends JPanel

   //----------------------------------------------------

   {

      public void paintComponent(Graphics g)

      {

         super.paintComponent(g);

Student provides code to display dots[] and texts[]

      }

   }

}

Explanation / Answer

I donot know the Colour mode part.

But for actionListioner the skeleton code is as below which can be further developed.

fileMenu.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

  

//handle Clear Menu Selection

//handleFileMenu();

  

}

});

aboutMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

  

//handle AboutMenu Selection

//handleAboutMenu();

  

}

});

exitMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

  

//handle ExitMenu Selection

//exitMenu();

  

}

});

Please let me know if any more precise implementation is required