图形g / KeyEvent e不兼容

时间:2016-02-19 03:23:00

标签: java graphics applet

当我尝试在数组中绘制对象时,如果标题中没有KeyEvent e,则会成功显示它们,但是如果KeyEvent e显示在标题中(据我所知)需要是为了调用方法)它不会打印任何东西。

如果我在标题中没有KeyEvent e,我如何使用我创建的方法移动数组中的对象?

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;
}
}

0 个答案:

没有答案