drawImage无法正常工作(Java)

时间:2015-09-19 13:40:55

标签: java swing

JavaGame.java

public class JavaGame extends JFrame implements Runnable {
static Rectangle object = new Rectangle(60, 60, 50, 50);
static Rectangle object1 = new Rectangle(60, 60, 50, 50);
int x, y, xDirection, yDirection;
static AI AI = new AI(object);
static BI BI = new BI(object1);
private Image dbImage;
private Graphics dbg;
Image face;

public void run(){
    try{
        while(true){

            move();
            Thread.sleep(2);
        }
        }catch(Exception e){
        System.out.println("ERROR");
    }
}

public void move(){
    x += xDirection;
    y += yDirection;
    if(y<=25)
        y = 25;
    if(y>=350)
        y = 350;
    if(x<=5)
        x = 5;
    if(x>=520)
        x = 520;
}

public void setXDirection(int xdir){
    xDirection = xdir;
}
public void setYDirection(int ydir){
    yDirection = ydir;
}

public class AL extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode();
        if(keyCode == e.VK_W){
            setYDirection(-1);
        }
        if(keyCode == e.VK_S){
            setYDirection(+1);
        }
        if(keyCode == e.VK_A){
            setXDirection(-1);
        }
        if(keyCode == e.VK_D){
            setXDirection(+1);
        }

    }
    public void keyReleased(KeyEvent f){
        int keyCode = f.getKeyCode();
        if(keyCode == f.VK_W){
            setYDirection(0);
        }
        if(keyCode == f.VK_S){
            setYDirection(0);
            }
        if(keyCode == f.VK_A){
            setXDirection(0);
        }
        if(keyCode == f.VK_D){
            setXDirection(0);
        }

    }
}

public static final int WIDTH = 640, HEIGHT = 480;
public JavaGame(){
    ImageIcon i = new ImageIcon("C:/Users/admin/workspace/Project/src/ProjectGame/Player.gif");
    face = i.getImage();

    addKeyListener(new AL());
    setTitle("Java Game");
    setSize(WIDTH, HEIGHT);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    x=WIDTH/2;
    y=HEIGHT/2;
   }

public void paint(Graphics g){
    dbImage = createImage(getWidth(),getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage,0,0,this);
    }

public void paintComponent(Graphics g){
    g.setColor(Color.RED);
    g.fillOval(x, y, 50, 50);
    g.drawImage(face, x, y, this);
    AI.paint(g);
    BI.paint(g);
    repaint();

    }

public static void main(String[] arg){
    JavaGame jg = new JavaGame();
    Thread t = new Thread(jg);
    t.start();
    Thread t1 = new Thread(AI);
    t1.start();
    Thread t2 = new Thread(BI);
    t2.start();
}

}

AI.java

public class AI implements Runnable {


Rectangle AI;
int xDirection, yDirection;

boolean resting;
Image face1;

public AI(Rectangle r){

    AI = r;

}
public void paint(Graphics g){
    ImageIcon j = new ImageIcon("C:/Users/admin/workspace/Project/src/ProjectGame/Enemy.gif");
    face1 = j.getImage();
    g.setColor(Color.BLUE);
    if(AI != null)
        g.fillRect(AI.x, AI.y, AI.width, AI.height);
        **g.drawImage(face, x, y, this);**

}
public int chooseRandomDirection(){
    Random r = new Random();
    int[] randDirection = new int[2];
    randDirection[0] = 1;
    randDirection[1] = -1;
    int randChoise = r.nextInt(2);
    return randDirection[randChoise];

}
public void setXDirection(int dir){
    xDirection = dir;
}
public void setYDirection(int dir){
    yDirection = dir;
}
public void move(){
    int widthLimit = JavaGame.WIDTH - AI.width;
    int heightLimit = JavaGame.HEIGHT - AI.height;
    AI.x += xDirection;
    if (AI.x > widthLimit) {
        AI.x = widthLimit;
    }
    if (AI.x < 0) {
        AI.x = 0;
    }
    AI.y += yDirection;
    if (AI.y > heightLimit) {
        AI.y = heightLimit;
    }
    if (AI.y < AI.height) {
        AI.y = AI.height;
    } 
}

public void run(){
    try{
        while(true){
            if(resting = true){
                setXDirection(chooseRandomDirection());
                setYDirection(chooseRandomDirection());
                long start = System.currentTimeMillis();
                long end = start + 1 * 1000;
                while(System.currentTimeMillis() < end){
                    move();
                    Thread.sleep(5);
                }
            }else{
                Thread.sleep(3000);

            }

        }
    }catch(Exception ex){
        System.err.println(ex.getMessage());
    }

}

}

AI.java上的g.drawImage(face, x, y, this);不起作用。说

The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, AI)

如何解决这个问题?

0 个答案:

没有答案