Question
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.
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 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(y
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 happenStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started