package Main2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable{
final int originalTileSize = 16; //16x16 tile
final int scale = 3;
final int tileSize = originalTileSize * scale; // 48x48
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWith = tileSize * maxScreenCol; //768 pixels
final int screenHight = tileSize * maxScreenRow; //57 6pixels
KeyHandler keyH = new KeyHandler();
Thread gameThread;
int playerX = 100;
int playerY = 100;
int playerspeed = 4;
public GamePanel(){
this.setPreferredSize(new Dimension(screenWith, screenHight ));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
while(gameThread !=null){
//System.out.println("The game loop is running");
update();
repaint();
}
}
public void update() {
if(keyH.upPressed == true) {
playerY -= playerspeed;
}
if(keyH.downPressed == true) {
playerY += playerspeed;
}
if(keyH.leftPressed == true) {
playerX -= playerspeed;
}
if(keyH.rightPressed == true) {
playerX += playerspeed;
}
}
public void painComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(playerX, playerY, tileSize, tileSize );
g2.dispose();
}
}
ompu 0 Newbie Poster
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.