So this Java code Ive been working on will not display my gif? Does anyone know
ID: 3828006 • Letter: S
Question
So this Java code Ive been working on will not display my gif? Does anyone know why? or could provide a solution ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class DirectionPanel extends JPanel
{
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);
}
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);
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
currentImage.paintIcon(this, page, x, y);
}
//*****************************************************************
// Represents the listener for keyboard activity.
//*****************************************************************
private class DirectionListener implements KeyListener
{
//--------------------------------------------------------------
// Responds to the user pressing arrow keys by adjusting the
// image and image location accordingly.
//--------------------------------------------------------------
public void keyPressed(KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
x += JUMP;
break;
}
repaint();
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
}
}
Explanation / Answer
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;
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.white);
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
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode() ) //Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes//
{
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right; x += JUMP;
break;
}
repaint();
}
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
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);
}
}
A key event is generated when a keyboard key is pressed. Key events allow a program to respond immediately to the user while we r typing or pressing other keyboard keys such as the arrow keys. If key events are being processed, the program can respond as soon as the key is pressed; there is no need to wait for the Enter key to be pressed or for some other component (like a button) to be activated. The Direction program responds to key events. An image of an arrow is displayed, and the image moves across the screen as the arrow keys are pressed. Actually, four different images are used, one each for the arrow pointing in each of the primary directions (up, down, right, and left).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.