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

JAVA HELP - This is my code for this problem. I can\'t figure out the rest. Plea

ID: 3863253 • Letter: J

Question

JAVA HELP - This is my code for this problem. I can't figure out the rest. Please help me solve the problem. ):

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JButton;

public class Keys2 extends JFrame {

   private JPanel contentPane;
   String line1;
   JButton btnQ = new JButton("Q");
  

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   Keys2 frame = new Keys2();
                   frame.setVisible(true);
                   } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   public Keys2() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 750, 500);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);
                      
       JTextArea textArea = new JTextArea();
       textArea.setBounds(10, 11, 714, 177);
       contentPane.add(textArea);
       textArea.addKeyListener( new KeyListener()
       {
          
       public void keyPressed(KeyEvent event)
       {
       line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
       int underlyingCode = event.getKeyCode();
       System.out.println(underlyingCode);
       char c = line1.charAt(0);
       if(c == 'Q')
       {
           btnQ.setBackground(Color.RED);
       }
       }
       public void keyTyped(KeyEvent event)
       {
       //line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
       }     
      
           public void keyReleased(KeyEvent event) {
           line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
               // line1 = event.getKeyCode();
               System.out.println(line1);
               char c = line1.charAt(0);
               if(c == 'Q')
               {
                   btnQ.setBackground(UIManager.getColor("Button.background"));
               }
          
           }
               }); // end textArea.addKeyListener
      
       JButton btnBackQuote = new JButton("`");
       btnBackQuote.setBounds(10, 209, 47, 39);
       contentPane.add(btnBackQuote);
      
       JButton btnTab = new JButton("Tab");
       btnTab.setBounds(10, 259, 71, 39);
       contentPane.add(btnTab);
      
       JButton btnCaps = new JButton("Caps");
       btnCaps.setBounds(10, 308, 89, 38);
       contentPane.add(btnCaps);
      
       JButton btnShift = new JButton("Shift");
       btnShift.setBounds(10, 357, 101, 38);
       contentPane.add(btnShift);
      
       JButton btnSpace = new JButton("");
       btnSpace.setBounds(110, 406, 388, 45);
       contentPane.add(btnSpace);
      

       btnQ.setBounds(91, 259, 47, 38);
       contentPane.add(btnQ);
              
   }
  

}

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JButton;

public class Keys2 extends JFrame {

   private JPanel contentPane;
   String line1;
   JButton btnQ = new JButton("Q");
  

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   Keys2 frame = new Keys2();
                   frame.setVisible(true);
                   } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   public Keys2() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 750, 500);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);
                      
       JTextArea textArea = new JTextArea();
       textArea.setBounds(10, 11, 714, 177);
       contentPane.add(textArea);
       textArea.addKeyListener( new KeyListener()
       {
          
       public void keyPressed(KeyEvent event)
       {
       line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
       int underlyingCode = event.getKeyCode();
       System.out.println(underlyingCode);
       char c = line1.charAt(0);
       if(c == 'Q')
       {
           btnQ.setBackground(Color.RED);
       }
       }
       public void keyTyped(KeyEvent event)
       {
       //line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
       }     
      
           public void keyReleased(KeyEvent event) {
           line1 = String.format("%s", KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
               // line1 = event.getKeyCode();
               System.out.println(line1);
               char c = line1.charAt(0);
               if(c == 'Q')
               {
                   btnQ.setBackground(UIManager.getColor("Button.background"));
               }
          
           }
               }); // end textArea.addKeyListener
      
       JButton btnBackQuote = new JButton("`");
       btnBackQuote.setBounds(10, 209, 47, 39);
       contentPane.add(btnBackQuote);
      
       JButton btnTab = new JButton("Tab");
       btnTab.setBounds(10, 259, 71, 39);
       contentPane.add(btnTab);
      
       JButton btnCaps = new JButton("Caps");
       btnCaps.setBounds(10, 308, 89, 38);
       contentPane.add(btnCaps);
      
       JButton btnShift = new JButton("Shift");
       btnShift.setBounds(10, 357, 101, 38);
       contentPane.add(btnShift);
      
       JButton btnSpace = new JButton("");
       btnSpace.setBounds(110, 406, 388, 45);
       contentPane.add(btnSpace);
      

       btnQ.setBounds(91, 259, 47, 38);
       contentPane.add(btnQ);
              
   }
  

}

12.20 (Bping Tutor: Tuning a Crucial Skill in the Computer Age) Typing quickly and correctly is an essential skill for working effectively with computers and the Internet. In this exercise, you'll build a GUI application that can help users learn to "touch type" (i.e. type correctly without look- ing at the keyboard). The application should display a virtual keyboard (Fig. 12.50) and should al low the user to watch what he or she is typing on the screen without looking at the actual keyboard. Use JButtons to represent the keys. As the user presses each key, the application highlights the cor- responding JButton on the GUI and adds the character to a JTextArea that shows what the user has typed so far. Hint: To highlight a JButton, use its setBackground method to change its background color. When the key is released, reset its original background color. You can obtain the JButton's original background color with the getBackground method before you change its color. Type some text using your keyboard The keys you press will be highlighted andthetetwillbe displayed. e Clicking the buttons with your mouse wilnotperform any action. Fig. 12.50 I Typing tutor You can test your program by typing a pangram phrase that contains every letter of the alphabet at least on such as "The quick brown fox jumped over a lazy dog." You can find other ce pangrams on the web. To make the program more interesting you could monitor the user's accuracy. You could have the user type specific phrases that you've prestored in your program and that you display on the screen above the virtual keyboard. You could keep track of how many keystrokes the user types cor- rectly and how many are typed incorrectly. You could also keep track of which keys the user is hav. ing difficulty with and display a report showing those keys.

Explanation / Answer

Hey here is the updated Clean and Clear Code written and tested for u .

package com.abhi.chegg.programs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Keys2 extends JFrame implements KeyListener
{
//Individual keyboard rows
String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","<<<<<"};//BackSpace
String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\"};
String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":",""","Enter"};
String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?"," ^" };
String fifthRow[]={" " ,"<" ,"\/",">" };

//all keys without shift key press
String noShift="`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
//special charactors on keyboard that has to be addressed duing keypress
String specialChars ="~-+[]\;',.?";

//Jbuttons corresponting to each individual rows
JButton first[];

JButton second[];

JButton third[];

JButton fourth[];

JButton fifth[];

//default color of the button to be repainted when key released
Color cc = new JButton().getBackground();

/*
* Invoked when a key has been pressed.
*
*/
public void keyPressed(KeyEvent evt)
{


}//end of keypressed

/**
* Invoked when a key has been released.
*/
public void keyReleased(KeyEvent evt)
{

}

/*
* Driver main method to start the application
*/
public static void main(String[] args) {

new Keys2().setVisible(true);
}
/*
* No argument construtor to create frame
*/
public Keys2()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set non resizable
this.setResizable(false);
//super.setSize(500,300);
//set size of the content pane ie frame
this.getContentPane().setPreferredSize(new Dimension(1000,600));
//super.getContentPane().setSize(800,400);
//set location for the frame
this.setLocation(50,50);

//init and paint frame
initWidgets();
}

/**
* Method to initialize frame component
*/
private void initWidgets()
{
//set the text area on top
JTextArea text = new JTextArea();
text.setPreferredSize(new Dimension(800,200));
//JScrollPane scrollPane = new JScrollPane(text);
//scrollPane.setPreferredSize(new Dimension(800, 200));

// add(typingArea, BorderLayout.PAGE_START);
// add(scrollPane, BorderLayout.CENTER);
//set the info label on top
JLabel info = new JLabel("<html>Type some text using your keyboard.The keys you press will be highlighted and text will be displayed.<br> Note : Clicking the buttons with your mouse will not perform any action. <br><br> </html>" );
//set the bold font for info
info.setFont(new Font("Verdana",Font.BOLD,14));

/*set the layout and place compomnet in place and pack it */
setLayout(new BorderLayout());
/*Various panel for the layout */
JPanel jpNorth = new JPanel();
JPanel jpCenter = new JPanel();
JPanel jpKeyboard = new JPanel();
JPanel jpNote = new JPanel();
add( jpNorth, BorderLayout.NORTH);
add( jpNote);
add( jpCenter, BorderLayout.CENTER);
add(jpKeyboard, BorderLayout.SOUTH);

jpNorth.setLayout(new BorderLayout());
jpNorth.add(info, BorderLayout.WEST);
jpNorth.add(info, BorderLayout.SOUTH);

jpCenter.setLayout( new BorderLayout());
jpCenter.add(text, BorderLayout.WEST);
jpCenter.add(text, BorderLayout.CENTER);

//add(text,BorderLayout.WEST);
// add(scrollPane,BorderLayout.CENTER);

//layout for keyboard
jpKeyboard.setLayout(new GridLayout(5,1));
//pack the components
pack();

/*paint first keyboard row and add it to the keyboard*/
first = new JButton[firstRow.length];
//get the panel for the row
JPanel p = new JPanel(new GridLayout(1, firstRow.length));
for(int i = 0; i < firstRow.length; ++i)
{
JButton b= new JButton(firstRow[i]);
b.setPreferredSize(new Dimension(100,50));
first[i] = b;
p.add(first[i]);
}
jpKeyboard.add(p);

/*paint second keyboard row and add it to the keyboard*/
second = new JButton[secondRow.length];
//get the panel for the row
p = new JPanel(new GridLayout(1, secondRow.length));
for(int i = 0; i < secondRow.length; ++i)
{
second[i] = new JButton(secondRow[i]);
p.add(second[i]);

}
jpKeyboard.add(p);

/*paint third keyboard row and add it to the keyboard*/

third = new JButton[thirdRow.length];
//get the panel for the row
p = new JPanel(new GridLayout(1, thirdRow.length));
for(int i = 0; i < thirdRow.length; ++i)
{
third[i] = new JButton(thirdRow[i]);
p.add(third[i]);
}
jpKeyboard.add(p);

/*paint fourth keyboard row and add it to the keyboard*/
fourth = new JButton[fourthRow.length];
//get the panel for the row
p = new JPanel(new GridLayout(1, fourthRow.length));
for(int i = 0; i < fourthRow.length; ++i)
{
fourth[i] = new JButton(fourthRow[i]);
p.add(fourth[i]);
if(i==fourthRow.length-2)
p.add(new JPanel());

}
p.add(new JPanel());
jpKeyboard.add(p);

//paint the fifth row
fifth = new JButton[fifthRow.length];
//get the panel for the row
p = new JPanel(new GridLayout(1, fifthRow.length));
/*put empty panel for layout adjustments */
for(int i = 0; i < 1; ++i)
{
JPanel spacePanel = new JPanel();
p.add(spacePanel);
}
/*draw the buttons */
// for(int i = 0; i < fifthRow.length; ++i)
// {
// if(i==1) //space bar panel
// {
// JButton b = new JButton(fifthRow[i]);
// b.setPreferredSize(new Dimension(400,10));
// b.setBounds(10, 10, 600, 100);
// fifth[i]=b;
// //add empty panels for layout
// p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());
// }
// else
// {
// fifth[i]=new JButton(fifthRow[i]);
// }
// if(i==0) //first black panel
// {
// //place a black panel at first
// JPanel spacePanel = new JPanel();
// p.add(spacePanel);
// }
//
// p.add(fifth[i]);
//
// }
  
for(int i = 0; i < fifthRow.length; ++i)
{
if(i==0) //space bar panel
{
JButton b = new JButton(fifthRow[i]);
fifth[i]=b;
//add empty panels for layout
p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());
Container glassPane = (Container) getRootPane().getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(getBounds().y-350, 180, 0, 20);
gbc.anchor = GridBagConstraints.SOUTHWEST;
b.setPreferredSize(new Dimension(450,50));
glassPane.add(fifth[i], gbc);
}
else
{
fifth[i]=new JButton(fifthRow[i]);
p.add(fifth[i]);
}
if(i==0) //first black panel
{
//place a black panel at first
JPanel spacePanel = new JPanel();
p.add(spacePanel);
}

}
  
jpKeyboard.add(p);
/*add listeners */
getContentPane().addKeyListener(this);
text.addKeyListener(this);
/*add listeners to all the button */
for(JButton b : first)
b.addKeyListener(this);

for(JButton b : second)
b.addKeyListener(this);
for(JButton b : third)
b.addKeyListener(this);

for(JButton b : fourth)
b.addKeyListener(this);

for(JButton b : fifth)
b.addKeyListener(this);

} //end of initWidgets   

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
  
}
}//end of class