Salve, sono un principiante in java. Ho creato un programma dove una palla, chiamata player si muove sullo schermo, ma quando avvio il programma il player si trova nell'angolo destro del programma, invece che muoverlo in tutto il frame. Potete darmi una mano? grazie per l 'aiuto.
il codice è il seguente:
Game.java
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
JFrame window = new JFrame("nfjkbnfd");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new GamePanel());
window.pack();
window.setVisible(true);
}
}
GamePanel.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable, KeyListener{
//fields
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
private Thread thread;
private boolean running;
private BufferedImage image;
private Graphics2D g;
private int FPS = 30;
private double averageFPS;
private Player player;
//constructor
public GamePanel() {
super();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
}
//functions
public void addNotify() {
super.addNotify();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
addKeyListener(this);
}
@Override
public void run() {
running = true;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
g = (Graphics2D) image.getGraphics();
player = new Player();
//FPS
long startTime;
long URDTimeMillis;
long waitTime;
long totalTime = 0;
int frameCount = 0;
int maxFrameCount = 30;
long targetTime = 1000 / FPS;
//GAMe LOOP
while (running) {
//FPS
startTime = System.nanoTime();
gameUpdate();
gameRender();
gameDraw();
URDTimeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime - URDTimeMillis;
try {
Thread.sleep(waitTime);
}
catch(Exception e) {
}
totalTime += System.nanoTime() - startTime;
frameCount++;
if(frameCount == maxFrameCount) {
averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
frameCount = 0;
totalTime = 0;
}
}
}
private void gameUpdate() {
player.update();
}
private void gameRender() {
g.setColor(new Color(0, 100, 255));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.drawString("FPS" + averageFPS, 10, 10);
//end FPS
player.draw(g);
}
private void gameDraw() {
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
@Override
public void keyPressed(KeyEvent key) {
int keyCode = key.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT) {
player.setLeft(true);
}
if(keyCode == KeyEvent.VK_RIGHT) {
player.setRight(true);
}
if(keyCode == KeyEvent.VK_UP) {
player.setUp(true);
}
if(keyCode == KeyEvent.VK_DOWN) {
player.setDown(true);
}
}
@Override
public void keyReleased(KeyEvent key) {
int keyCode = key.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT) {
player.setLeft(false);
}
if(keyCode == KeyEvent.VK_RIGHT) {
player.setRight(false);
}
if(keyCode == KeyEvent.VK_UP) {
player.setUp(false);
}
if(keyCode == KeyEvent.VK_DOWN) {
player.setDown(false);
}
}
@Override
public void keyTyped(KeyEvent key) {
}
}
Player.java
import java.awt.*;
public class Player {
//FIELDS
private int x;
private int y;
private int r;
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private int dx;
private int dy;
private int speed;
private int lives;
private Color color1;
private Color color2;
//CONSTRUCTOR
public Player() {
x = GamePanel.WIDTH / 2;
y = GamePanel.HEIGHT / 2;
r = 5;
dx = 0;
dy = 0;
speed = 5;
lives = 3;
color1 = Color.WHITE;
color2 = Color.RED;
}
//FUNCTIONS
public void setLeft(boolean b) {left = b;}
public void setRight(boolean b) {right = b;}
public void setUp(boolean b) {up = b;}
public void setDown(boolean b) {down = b;}
public void update() {
if(left) {
dx = -speed;
}
if(right) {
dx = speed;
}
if(up) {
dy = -speed;
}
if(down) {
dy = speed;
}
x += dx;
y += dy;
if(x < r) x = r;
if(y < r) y = r;
if(x < GamePanel.WIDTH - r) x = GamePanel.WIDTH - r;
if(y < GamePanel.HEIGHT - r) y = GamePanel.HEIGHT - r;
dx = 0;
dy = 0;
}
public void draw(Graphics2D g) {
g.setColor(color1);
g.fillOval(x - r, y - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(3)); //3 pixels
g.setColor(color1.darker());
g.drawOval(x - r, y - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(1)); //3 pixels
}
}