How can I add a main method to this program to get it to run?? import javax.swin
ID: 3827782 • Letter: H
Question
How can I add a main method to this program to get it to run??
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 int up=1, down=2, right=3, left=4, currentImage;
private int x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this panel
//-----------------------------------------------------------------
public DirectionPanel()
{
x = WIDTH / 2;
y = HEIGHT / 2;
currentImage = right;
setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
JButton t = new JButton("Click here");
t.addKeyListener (new DirectionListener());
add(t);
setFocusable(true);
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
if (currentImage == left) arrow(page,x,y,50,-1.57);
if (currentImage == up) arrow(page,x,y,50,3.14);
if (currentImage == right) arrow(page,x,y,50,1.57);
if (currentImage == down) arrow(page,x,y,50,6.28);
}
//*****************************************************************
// 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) {}
}
private void arrow(Graphics page, int x, int y, int len, double alpha)
{
page.setColor (Color.green);
int x1 = x+(int)(len*Math.sin(alpha));
int y1 = y+(int)(len*Math.cos(alpha));
page.drawLine (x, y, x1, y1);
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+2.5)), y1+(int)(20*Math.cos(alpha+2.5)));
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+3.7)), y1+(int)(20*Math.cos(alpha+3.7)));
}
}
Explanation / Answer
private class DirectionListener implements KeyListener { //Direction.java import javax.swing.*; import javax.swing.JFrame; import java.awt.*; import java.awt.event.*; 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: 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 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 { 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(); } //empty definitions for unused methods private void KeyTyped (KeyEvent event) {} private void KeyReleased (KeyEvent event) {} } } 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); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.