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

Java. I cannot get the applet to display the number entered by the user. import

ID: 3667617 • Letter: J

Question

Java. I cannot get the applet to display the number entered by the user.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import javax.swing.JApplet;

public class DrawMenu extends JApplet implements ActionListener
{
   //declare barmenu
   private JMenuBar BarMenu;
   //declare colormenu of barmenu
   private JMenu ColorMenu;
   //menu item variables
   private JMenuItem BlkSel, BluSel, CynSel, GrySel, GrnSel,
       OrgSel, RedSel, YlwSel;
   //declare current color to variable color
   private Color CurColor = Color.black;
   //declare numtf variable ot jtf
   private JTextField NumTF;
   //decalre label variable
   private JLabel Label;
   //declare ok button variable jbutton
   private JButton OKBtn;
   //declare num variable
   private int Num = 0;
  
   //init method
   public void init()
   {
       //container
       Container C = getContentPane();
       setSize(500, 300);
       Label = new JLabel("Enter Number");
       Label.setLocation(200, 40);
       Label.setSize(100, 30);
       NumTF = new JTextField(1);
       NumTF.setLocation(300, 40);
       NumTF.setSize(100, 30);
       OKBtn = new JButton("OK");
       OKBtn.setLocation(400, 40);
       OKBtn.setSize(50, 30);
      
       //ok button action listener
       OKBtn.addActionListener(this);
       //container layout
       C.setLayout(null);
       C.add(Label);
       C.add(NumTF);
       C.add(OKBtn);
      
       //create barmenu variable
       BarMenu = new JMenuBar();
       //menu bar container
       setJMenuBar(BarMenu);
       //add color menu to menu bar
       ColorMenu = new JMenu("Color");
       BarMenu.add(ColorMenu);
       //add items to menu
       BlkSel = new JMenuItem("Black");
       BluSel = new JMenuItem("Blue");
       CynSel = new JMenuItem("Cyan");
       GrySel = new JMenuItem("Gray");
       GrnSel = new JMenuItem("Green");
       OrgSel = new JMenuItem("Orange");
       RedSel = new JMenuItem("Red");
       YlwSel = new JMenuItem("Yellow");
       //action listener to menu items
       BlkSel.addActionListener(this);
       BluSel.addActionListener(this);
       CynSel.addActionListener(this);
       GrySel.addActionListener(this);
       GrnSel.addActionListener(this);
       OrgSel.addActionListener(this);
       RedSel.addActionListener(this);
       YlwSel.addActionListener(this);
      
       //add menu items to color menu
       ColorMenu.add(BlkSel);
       ColorMenu.add(BluSel);
       ColorMenu.add(CynSel);
       ColorMenu.add(GrySel);
       ColorMenu.add(GrnSel);
       ColorMenu.add(OrgSel);
       ColorMenu.add(RedSel);
       ColorMenu.add(YlwSel);
   }
  
   //method paint
   public void Paint(Graphics g)
   {
       super.paint(g);
       g.setColor(CurColor);
       switch(Num)
       {
       case 1: g.fillRect(20, 20, 20, 100);
       break;
       case 2: g.fillRect(20, 40, 80, 20);
       g.fillRect(80, 40, 20, 40);
       g.fillRect(20, 80, 80, 20);
       g.fillRect(20, 80, 20, 50);
       g.fillRect(20, 120, 80, 20);
       break;
       case 3: g.fillRect(20, 40, 80, 20);
       g.fillRect(80, 40, 20, 40);
       g.fillRect(20, 80, 80, 20);
       g.fillRect(80, 80, 20, 50);
       g.fillRect(20, 120, 80, 20);
       break;
       case 4: g.fillRect(20, 20, 20, 60);
       g.fillRect(40, 60, 40, 20);
       g.fillRect(80, 20, 20, 100);
       break;
       case 5: g.fillRect(20, 40, 80, 20);
       g.fillRect(20, 40, 20, 40);
       g.fillRect(20, 80, 80, 20);
       g.fillRect(80, 80, 20, 50);
       g.fillRect(20, 120, 80, 20);
       break;
       case 6: g.fillRect(20, 20, 20, 100);
       g.fillRect(40, 20, 40, 20);
       g.fillRect(40, 60, 20, 20);
       g.fillRect(40, 100, 20, 20);
       g.fillRect(60, 60, 20, 60);
       break;
       case 7: g.fillRect(40, 20, 30, 20);
       g.fillRect(70, 20, 20, 80);
       break;
       case 8: g.fillRect(20, 20, 20, 100);
       g.fillRect(40, 20, 40, 20);
       g.fillRect(40, 60, 20, 20);
       g.fillRect(40, 100, 20, 20);
       g.fillRect(60, 40, 20, 80);
       break;
       case 9: g.fillRect(20, 20, 20, 60);
       g.fillRect(40, 20, 40, 20);
       g.fillRect(40, 60, 20, 20);
       g.fillRect(40, 100, 20, 20);
       g.fillRect(60, 40, 20, 80);
       break;
       case 0: g.fillRect(20, 20, 29, 100);
       g.fillRect(40, 20, 60, 20);
       g.fillRect(40, 100, 60, 20);
       g.fillRect(100, 20, 20, 100);
       break;
       }
   }
  
   //define method actionperformed takes object of actionevent
   public void actionPerformed(ActionEvent e)
   {
       if(e.getSource() == BlkSel)
           CurColor = Color.black;
       else if(e.getSource() == BluSel)
           CurColor = Color.blue;
       else if(e.getSource() == CynSel)
           CurColor = Color.cyan;
       else if(e.getSource() == GrySel)
           CurColor = Color.gray;
       else if(e.getSource() == GrnSel)
           CurColor = Color.green;
       else if(e.getSource() == OrgSel)
           CurColor = Color.orange;
       else if(e.getSource() == RedSel)
           CurColor = Color.red;
       else if(e.getSource() == YlwSel)
           CurColor = Color.yellow;
       //if ok button is pressed
       if(e.getSource() == OKBtn)
           Num = Integer.parseInt(NumTF.getText());
       repaint();
   }
}

Explanation / Answer

Solution:

By seeing the program , it is clear that you didn't assigned the input value from user, to the variable Num.

So just include the following in your code, to achieve the desired results.

Inside the paint method, before switch(Num) , we have to assign the value from text field named NumTF to Num:

Num=Integer.parseInt(NumTF.getText());

To display the input entered by the user in Applet in desired position:

you can change the position as you want.

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