Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Listings 8.21 and 8.22 pp. 428-429 are needed: Listing8.21_8.2201312018.pdf Here are the .gif files for the arrows: Arrows.zip I posted previously posted the same

1. image text in transcribed

Listings 8.21 and 8.22 pp. 428-429 are needed: Listing8.21_8.2201312018.pdfimage text in transcribed

Here are the .gif files for the arrows: Arrows.zipimage text in transcribed

image text in transcribed

I posted previously posted the same question and goted the following answer from chegge. But, its not drwing the the arrow on the field. I am using jGrasp editior. Can some one fix the issues to print out the direction arrow.

" /*The java program that creates DirectionPanel * object and add to the frame.*/ //Direction.java import javax.swing.JFrame; public class Direction { 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); } }

--------------------------------------------------------------------------------

/*The program display arrow on the panel*/ //DirectionPanel.java 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; 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); } public void paintComponent (Graphics page) { super.paintComponent (page); currentImage.paintIcon (this, page, x, y); } private class DirectionListener implements KeyListener { /*Re-write the keyPressed method that stops the arrow * moving beyond boundaries.*/ public void keyPressed (KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: currentImage = up; //stops moving up if(yHEIGHT) y=HEIGHT-IMAGE_SIZE; else y += JUMP; break; case KeyEvent.VK_LEFT: currentImage = left; //stops moving left if(xWIDTH) x=WIDTH-IMAGE_SIZE; else x += JUMP; break; }

repaint(); } public void keyTyped (KeyEvent event) {} public void keyReleased (KeyEvent event) {} } }

"

PP 8.27 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 PP 8.27 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions