0

When I attempt to draw the objects in the array it successfully displays them if there is no KeyEvent e in the header, but if KeyEvent e is in the header like is shown (and to my knowledge it needs to be in order to call the method) it does not print anything out.

How can I use the method I created that moves an object in the array if I cannot have KeyEvent e in the header?

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Field extends Applet
{   
public void paint(Graphics g, KeyEvent e) 
{
    drawField(g,e);
}
public void drawField(Graphics g, KeyEvent e) 
{
    int Field[][] = new int[1000][1000];
    int Roadl[][] = new int[1000][1000];
    Field[100][100] = 1;
    Field[100][300] = 2;
    Field[100][500] = 3;
    Roadl[100][500] = 500;
    Field[300][100] = 4;
    Roadl[300][100] = 500;
    Field[600][600] = 6;
    moveCar(Field,600,600,e);

    for(int i = 0; i < Field.length; i++)
        {
            for (int k = 0; k < Field[i].length; k++)
        {
            if (Field[i][k] == 1)
            {
               VertBuilding vb1 = new VertBuilding(i,k,g);
            } 
            else if (Field[i][k] == 2)
            {
               HorizBuilding hb1 = new HorizBuilding(i,k,g);
            } 
            else if (Field[i][k] == 3)
            {
               VertRoad vr1 = new VertRoad(i,k,Roadl[i][k],g);
            } 
            else if (Field[i][k] == 4)
            {
               HorizRoad hr1 = new HorizRoad(i,k,Roadl[i][k],g);
            } 
            else if (Field[i][k] == 5)
            {
               Coin c1 = new Coin(i,k,g);
            } 
             else if (Field[i][k] == 6)
            {
               HorizCar car1 = new HorizCar(i,k,g);
            }
             else if (Field[i][k] == 7)
            {
               VertCar car1 = new VertCar(i,k,g);
            }
        }
    }
}
public void moveCar(int[][] arr1, int i, int k, KeyEvent e)
{
 i += keyTypedH(e);
 k += keyTypedV(e);
    arr1[i][k] = carD(e);
}
public int keyTypedV (KeyEvent e) {
   char c = e.getKeyChar();
  int y = 0;
  if (c == 's')      
  {
       y+=10; 
                          }
  else if (c == 'w') 
  {
       y-=10;
                          }  

  return y;
}
public int keyTypedH (KeyEvent e) {
   char c = e.getKeyChar();
  int x = 0;

  if (c == 'a') 
  {
   x-=10;
                          } 
  else if (c == 'd')
  {
       x+=10;
                          } 
  return x;
}
 public int carD (KeyEvent e) {
   char c = e.getKeyChar();
  int carLocation = 0;

  if (c == 'a') 
  {
      carLocation = 6; 
                          } 
  else if (c == 'd')
  {
      carLocation = 6; 
                          } 
  else  if (c == 's')      
  {
      carLocation = 7; 
                          }
  else if (c == 'w') 
  {
      carLocation = 7; 
                          }  

  return carLocation;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
h.m
  • 1
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 21 '16 at 04:54
  • .. 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Feb 21 '16 at 04:54

0 Answers0