Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * This program demonstrates Focus events and Key events. A colored square is * drawn on the panel.

import java.awt.*; import java.awt.event.*; import javax.swing.*;

/** * This program demonstrates Focus events and Key events. A colored square is * drawn on the panel. By pressing the arrow keys, the user can move the square * up, down, left, or right. By pressing the keys R, G, B, or K, the user can * change the color of the square to red, green, blue, or black, respectively. * The panel changes appearance when it has the input focus; a cyan-colored * border is drawn around it. When it does not have the input focus, the message * "Click to Activate" is displayed and the border is gray. The panel should * have focus whenever the program window is active. This class contains a * main() routine so that it can be run as a program */ public class KeyboardAndFocusDemo extends JPanel {

/** * The main program just opens a window that shows an object of type * KeyboardAndFocusDemo. Note that it should request focus for the panel. This * has to be done after the window is made visible for it to have any effect. */ public static void main(String[] args) { JFrame window = new JFrame("Keyboard and Focus Demo"); KeyboardAndFocusDemo content = new KeyboardAndFocusDemo(); window.setContentPane(content); window.setSize(400, 400); window.setLocation(100, 100); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); // ***something needs to be done here content.requestFocusInWindow();

}

private static final int SQUARE_SIZE = 50; // Length of side of square. private Color squareColor; // The color of the square. private int squareTop, squareLeft; // Coordinates corner of square. private Rectangle square; /** * The constructor sets the initial position and color of the square and * registers itself to act as a listener for Key, Focus, and Mouse events. */ public KeyboardAndFocusDemo() {

squareTop = 100; // Initial position of top-left corner of square. squareLeft = 100; squareColor = Color.RED; // Initial color of square.

setBackground(Color.WHITE);

} // end constructor

/** * Draws a border, square, and message in the panel. The message and * the color of the border depend on whether or not the pane has * the input focus. */ public void paintComponent(Graphics g) {

super.paintComponent(g); // Fills the panel with its // background color, which is white.

//*** Draw a 3-pixel border, colored cyan if the panel has the //*** keyboard focus, or in light gray if it does not. */ if (hasFocus()) { g.setColor(Color.CYAN); } else { g.setColor(Color.LIGHT_GRAY); } int width = getSize().width; // Width of the panel. int height = getSize().height; // Height of the panel. g.drawRect(0,0,width-1,height-1); g.drawRect(1,1,width-3,height-3); g.drawRect(2,2,width-5,height-5);

//*** Draw the square. g.setColor(Color.RED); g.fillRect(squareTop, squareLeft, SQUARE_SIZE, SQUARE_SIZE);

/* Print a message that depends on whether the panel has the focus. */

g.setColor(Color.MAGENTA); if (hasFocus()) { g.drawString("Arrow Keys Move Square",7,20); g.drawString("K, R, G, B Change Color",7,40); } else g.drawString("Click to activate",7,20);

} // end paintComponent()

public void setColor(char e) { if (e == 'b' || e == 'B') { squareColor = Color.BLUE; } if (e == 'g' || e == 'G') { squareColor = Color.GREEN; } if (e == 'r' || e == 'R') { squareColor = Color.RED; } if (e == 'k' || e == 'K') { squareColor = Color.BLACK; } } public void shiftRight() { } } // end class KeyboardAndFocusDemo

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

public class KeyboardListener implements KeyListener, MouseListener, FocusListener { private KeyboardAndFocusDemo keyboard;

public KeyboardListener(KeyboardAndFocusDemo demo) { keyboard = demo; }

@Override public void keyTyped(KeyEvent e) { char key = e.getKeyChar(); keyboard.setColor(key); keyboard.repaint(); }

@Override public void keyPressed(KeyEvent e) {

}

@Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub

}

@Override public void mouseClicked(MouseEvent e) { keyboard.repaint();

}

@Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub

}

@Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub

}

@Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub

}

@Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub

}

@Override public void focusGained(FocusEvent e) { // TODO Auto-generated method stub

}

@Override public void focusLost(FocusEvent e) { // TODO Auto-generated method stub

}

}

Instruction: 3) Implement the required event-handling methods in your Listener class that modify the state of the instance variables in KeyboardAndFocusDemo, so when paintComponent() is called, it gives the user instant feedback that they have modified the color, or direction of the square. In other words, - When the user types a 'b' 'B' 'g' 'G' 'r' 'R' 'k' or 'K', the square's color should be updated. - When the user pressed an arrow key up, down, left or right, the square should move 8 pixels in the appropriate direction. - The program should also give visual feedback based on whether the window has focus or not (cyan or gray border).

Demo: https://www.youtube.com/watch?v=HLBVvHx3JfM

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions