// 3/13/2013 Greenstein: changed from applet to JFrame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyboardAndFocusDemo implements KeyListener, FocusListener, MouseListener { static final int SQUARE_SIZE = 40; // Length of a side of the square. Color squareColor; // The color of the square. int squareTop, squareLeft; // Coordinates of top-left corner of square. boolean focussed = false; // True when this applet has input focus. JFrame frame; DisplayPanel canvas; // The drawing surface on which the applet draws. final int XSIZE = 500, YSIZE = 500; public static void main(String[] args) { KeyboardAndFocusDemo kafd = new KeyboardAndFocusDemo(); kafd.Run(); } public void Run() { // Create a frame to hold everything frame = new JFrame ("Hear Mouse"); frame.setSize(XSIZE, YSIZE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setBackground(Color.black); // only needed if frame is larger than panel // Define panel to draw on canvas = new DisplayPanel(); // create a panel to draw on canvas.setBackground(Color.white); canvas.addFocusListener(this); // Set up the applet to listen for events canvas.addKeyListener(this); // from the canvas. canvas.addMouseListener(this); // connects the MouseListerner to the panel window // Put frame together frame.getContentPane().add(canvas); // puts panel on frame frame.setVisible(true); squareTop = YSIZE / 2 - SQUARE_SIZE / 2; squareLeft = XSIZE / 2 - SQUARE_SIZE / 2; squareColor = Color.red; } // end Run(); class DisplayPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // Fills the panel with its // background color, which is white. if (focussed) g.setColor(Color.cyan); else g.setColor(Color.lightGray); int width = getSize().width; // Width of the applet. int height = getSize().height; // Height of the applet. g.drawRect(0,0,width-1,height-1); g.drawRect(1,1,width-3,height-3); g.drawRect(2,2,width-5,height-5); g.setColor(squareColor); g.fillRect(squareLeft, squareTop, SQUARE_SIZE, SQUARE_SIZE); /* If the applet does not have input focus, print a message. */ if (!focussed) { g.setColor(Color.magenta); g.drawString("Click to activate",7,20); } } // end paintComponent() } // end nested class DisplayPanel // ------------------- Event handling methods ---------------------- public void focusGained(FocusEvent evt) { // The applet now has the input focus. focussed = true; canvas.repaint(); // redraw with cyan border } public void focusLost(FocusEvent evt) { // The applet has now lost the input focus. focussed = false; canvas.repaint(); // redraw without cyan border } public void keyTyped(KeyEvent evt) { char ch = evt.getKeyChar(); // The character typed. if (ch == 'B' || ch == 'b') { squareColor = Color.blue; canvas.repaint(); } else if (ch == 'G' || ch == 'g') { squareColor = Color.green; canvas.repaint(); } else if (ch == 'R' || ch == 'r') {
squareColor = Color.red; canvas.repaint(); } else if (ch == 'K' || ch == 'k') { squareColor = Color.black; canvas.repaint(); } } // end keyTyped() public void keyPressed(KeyEvent evt) { int key = evt.getKeyCode(); // keyboard code for the key that was pressed if (key == KeyEvent.VK_LEFT) { squareLeft -= 8; if (squareLeft < 3) squareLeft = 3; canvas.repaint(); } else if (key == KeyEvent.VK_RIGHT) { squareLeft += 8; if (squareLeft > XSIZE - 3 - SQUARE_SIZE) squareLeft = YSIZE - 3 - SQUARE_SIZE; canvas.repaint(); } else if (key == KeyEvent.VK_UP) { squareTop -= 8; if (squareTop < 3) squareTop = 3; canvas.repaint(); } else if (key == KeyEvent.VK_DOWN) { squareTop += 8; if (squareTop > YSIZE - 3 - SQUARE_SIZE) squareTop = YSIZE - 3 - SQUARE_SIZE; canvas.repaint(); } } // end keyPressed() public void keyReleased(KeyEvent evt) { // empty method, required by the KeyListener Interface } public void mousePressed(MouseEvent evt) { // Request that the input focus be given to the // canvas when the user clicks on the applet. canvas.requestFocus(); } public void mouseEntered(MouseEvent evt) { } // Required by the public void mouseExited(MouseEvent evt) { } // MouseListener public void mouseReleased(MouseEvent evt) { } // interface. public void mouseClicked(MouseEvent evt) { } } // end class KeyboardAndFocusDemo