Question: // Direction.java Author: Lewis/Loftus // // Demonstrates key events. //******************************************************************** import javax.swing.JFrame; public class Direction { //----------------------------------------------------------------- // Creates and displays the application frame. //-----------------------------------------------------------------
// Direction.java Author: Lewis/Loftus // // Demonstrates key events. //******************************************************************** import javax.swing.JFrame; public class Direction { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Direction"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DirectionPanel()); frame.pack(); frame.setVisible(true); } } //******************************************************************** // DirectionPanel.java Author: Lewis/Loftus // // Represents the primary display panel for the Direction program. //******************************************************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DirectionPanel extends JPanel { private final int WIDTH = 300, HEIGHT = 200; private final int JUMP = 10; // increment for image movement private final int IMAGE_SIZE = 31; private ImageIcon up, down, right, left, currentImage; private int x, y; //----------------------------------------------------------------- // Constructor: Sets up this panel and loads the images. //----------------------------------------------------------------- public DirectionPanel() { addKeyListener(new DirectionListener()); x = WIDTH / 2; y = HEIGHT / 2; up = new ImageIcon("arrowUp.gif"); down = new ImageIcon("arrowDown.gif"); left = new ImageIcon("arrowLeft.gif"); right = new ImageIcon("arrowRight.gif"); currentImage = right; setBackground(Color.black); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); } Modify the Direction program from this chapter so that the image is not allowed to move out of the visible area of the panel. Ignore any key event that would cause that to happen. in java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
