0

我正在尝试将矩形呈现给 JPanel:

playerRect = new Rectangle(100,100,10,10);

问题是, playerRect 每次都以 100,0 呈现。我已经更新了 Eclipse 和 Java,并对我的代码进行了故障排除,并使用了 x、y、宽度、高度(尽管我不确定我的代码如何影响 java.awt.Rectangle)。

关于导致这种情况的任何线索?

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

public class Player {

private World world;
private Rectangle playerRect;
private Image playerImg;

protected int xDirection, yDirection; 

//block variables
private int hoverX, hoverY;
private boolean hovering;

public Player(World world){
    this.world = world;
    playerImg = new ImageIcon("D:/Student Data/gametest1/GameEngine/res/player.png").getImage();

    playerRect = new Rectangle(100, 100, 10, 10); //  ### here's the issue ###

}


private void setXDirection(int d){
    xDirection = d;
}

private void setYDirection(int d){
    yDirection = d;
}

public void update(){
    move();
    checkForCollision();
}

private void move(){
    playerRect.x += xDirection;
    playerRect.y =+ yDirection;


}

private void checkForCollision(){

}


//Drawing methods
public void draw(Graphics g){
    g.drawImage(playerImg, playerRect.x, playerRect.y, null);
}

private void drawBlackOutline(Graphics g){
    g.setColor(Color.BLACK);
    g.drawRect(hoverX,hoverY, world.blocks[0].width, world.blocks[0].height);
    if(hovering){drawBlackOutline(g);}
}

//mouse events
public void mousePressed(MouseEvent e){

}
public void mouseReleased(MouseEvent e){

}
public void mouseMoved(MouseEvent e){
    int x = e.getX();
    int y = e.getY();
    int px = playerRect.x;
    int py = playerRect.y;
    for(int i =0; i <world.arrayNum; i++){
        if(world.blocks[i].contains(x,y)){
            hovering = true;
            hoverX = world.blocks[i].x;
            hoverY = world.blocks[i].y;
            break;
        }else{hovering = false;}
    }



}
public void mouseDragged(MouseEvent e){

}



private class Weapon{
    public static final int UNARMED = 0;
    public static final int PICKAXE = 1;
    public static final int GUN = 2;

    public int CURRENT_WEAPON;

    public Weapon( int w){
        switch(w){
            default:
                System.out.println("no weapon sellected");
                break;
            case UNARMED:
                CURRENT_WEAPON = UNARMED;
                break;
            case PICKAXE:
                CURRENT_WEAPON = PICKAXE;
                break;
            case GUN:
                CURRENT_WEAPON = GUN;
                break;
        }
    }


    public void selectWeapon( int w){
        switch(w){
        default:
            System.out.println("no weapon sellected");
            break;
        case UNARMED:
            CURRENT_WEAPON = UNARMED;
            break;
        case PICKAXE:
            CURRENT_WEAPON = PICKAXE;
            break;
        case GUN:
            CURRENT_WEAPON = GUN;
            break;
        }
    }

    public boolean isEquipped(int w){
        if(w == CURRENT_WEAPON){
            return true;
        }
        else
            return false;
    }

}
}

这是矩形被绘制到 JPanel 的位置:

package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

private static final long serialVersionUID = 1L;
//Double Buffering
private Image dbImage;
private Graphics dbg;
//JPanel variables
static final int GWIDTH = 900, GHEIGHT = 600;
static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
//Game variables
private Thread game;
private volatile boolean running = false;
public int tickCount = 0;

private static final int DELAYS_BEFORE_YIELD = 10;
private long period = 6*1000000; //ms --> nano

//Game Objects
World world;
Player p1;


public GamePanel(){
    world = new World();
    p1 = new Player(world);
    setPreferredSize(gameDim);
    setBackground(Color.WHITE);
    setFocusable(true);
    requestFocus();
    //Handle all key inputs from user
    addKeyListener(new KeyAdapter(){
        @Override
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_W){
                world.navigateMap(World.PAN_UP);}

            if(e.getKeyCode() == KeyEvent.VK_S){
            world.navigateMap(World.PAN_DOWN);}

            if(e.getKeyCode() == KeyEvent.VK_A){
            world.navigateMap(World.PAN_LEFT);}

            if(e.getKeyCode() == KeyEvent.VK_D){
            world.navigateMap(World.PAN_RIGHT);}
        }
        @Override
        public void keyReleased(KeyEvent e){
            world.stopMoveMap();
        }
        @Override
        public void keyTyped(KeyEvent e){

        }

    });

    addMouseListener(new MouseAdapter(){
        @Override
        public void mousePressed(MouseEvent e){

        }
        @Override
        public void mouseReleased(MouseEvent e){

        }
        @Override
        public void mouseClicked(MouseEvent e){

        }
    });

    addMouseMotionListener(new MouseAdapter(){
        @Override
        public void mouseMoved(MouseEvent e){
            p1.mouseMoved(e);
        }
        @Override
        public void mouseDragged(MouseEvent e){

        }
        @Override
        public void mouseEntered(MouseEvent e){

        }
        @Override
        public void mouseExited(MouseEvent e){

        }
    });

}


private void startGame(){
    if(game == null || !running){
        game = new Thread(this);
        game.start();
        running = true;
    }
}

public void addNotify(){
    super.addNotify();
    startGame();
}

public void stopGame(){

    if(running){
        running = false;

    }
}


public void run() {

long lastTime = System.nanoTime();


    long beforeTime, afterTime, diff, sleepTime, overSleepTime = 0;
    int delays = 0;
    while(running){
        beforeTime =System.nanoTime();
        gameUpdate();
        gameRender();
        paintScreen();
        afterTime = System.nanoTime();
        diff = afterTime - beforeTime;
        sleepTime = (period - diff) - overSleepTime;
        //if the sleep time is between 0 and the period, sleep
        if(sleepTime < period && sleepTime > 0){
            try {
                game.sleep(sleepTime/1000000L);
                overSleepTime = 0;
            } catch (InterruptedException e) {
                System.err.println("You done goofed!");
            }

        } 
        //the difference was greater than the period
        else if(diff>period){
            overSleepTime = diff - period;
        }
        //accumulate the amount of delays, and eventually yield
        else if(++delays >= DELAYS_BEFORE_YIELD){
            game.yield();
        }
        //the loop took less time than expected,but we need to make up for            oversleep time
        else{overSleepTime = 0;}}
        }



private void gameUpdate(){
    if(running && game != null){
        //update game state
        world.moveMap();
        p1.update();
    }
}

private void gameRender(){
    if(dbImage == null){ //create the buffer
        dbImage = createImage(GWIDTH, GHEIGHT);
        if(dbImage == null){
            System.err.println("dbImage is still null!");
            return;
        }else{
            dbg = dbImage.getGraphics();
        }
    }
//Clear the screen
    dbg.setColor(Color.BLACK);
    dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
//Draw Game elements
    draw(dbg);
}

//#####   Draw all game content in this method   #####//
public void draw(Graphics g){
    world.draw(g);
    p1.draw(g);
    //g.setColor(Color.RED);
    //g.setFont(new Font("PR Celtic Narrow", Font.BOLD, 50));
    //String str = "MentalBrink Lv. 5";
    //g.drawString(str, 100,100);


}

private void paintScreen(){
    Graphics g;
    try{
        g = this.getGraphics();
        if(dbImage != null && g != null){
            g.drawImage(dbImage,  0,  0, null);
        }
        Toolkit.getDefaultToolkit().sync(); //for Linux people. 
        g.dispose();
    }catch(Exception e){
        System.err.println(e);
    }

}

private void log(String s){
    System.out.println(s);
}
}
4

0 回答 0