Hello guys,
I have a few questions that I couldn’t really answer with google and I would like to ask you. I’ve learned the basics of java a few years ago but did never really get into programming. I recently had new interest and tried again, first relearning the things I had forgotten. I seem to understand written code quite well but have problems in writing code myself and thought I would learn best if I try it myself.
I began with a simple pong tutorial and wanted to write something similar without copying the code and I have now a tiny little program where I can move a dot with the arrow keys. I know this is nothing big and a lot of people have done this before but I would be grateful if you could look at the code and point out mistakes I have done especially concerning oo-design and what I should change to make the code better.
Additionally I have following questions concerning the code:
- I have two classes until now, a game class with an inner class that represents the panel on which is drawn and a player class. I have seen a pong tutorial where the panel is a separate class extending JPanel. I thought that the JPanel belonged to the Game class because it will use possibly variables from it in the future and it would be more of a hassle creating a separate class for it but I’m not really sure if this is true
- @SuppressWarnings(“serial”) was added above my inner class, why this?
- Should I make all member variables of the Player class private and use these with setters and getters? If there are maybe 20-30 variables that would be a huge amount of code or not?
- The arrow keys work good but sometimes if you release just one key the dot stops for a short time, due to the fact that xa or xy is set to 0, what would be an approach to solve this?
I know this is not a finished product but an exercice for me to improve my skills. I probably have made a lot of mistakes and I am happy if you can point out some general mistakes, then I will try to add more things and play around with it.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game implements KeyListener {
static JFrame frame;
static Player player;
static DrawingPanel mapPanel;
int mapPanelSize = 300;
static boolean loopCondition;
static int frameSize = 500;
public Game() {
}
public static void main(String[] args) {
Game game = new Game();
game.runGame();
}
public void runGame() {
player = new Player();
frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
DrawingPanel mapPanel = new DrawingPanel();
mapPanel.setSize(300, 300);
frame.add(BorderLayout.CENTER, mapPanel);
frame.addKeyListener(this);
frame.requestFocus();
loopCondition = true;
while (loopCondition) {
mapPanel.repaint();
try {
Thread.sleep(80);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent event) {
System.out.println("KeyPressed");
player.keyPressed(event);
}
@Override
public void keyReleased(KeyEvent event) {
System.out.println("KeyReleased");
player.keyReleased(event);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@SuppressWarnings("serial")
class DrawingPanel extends JPanel {
final int DIAMETER = 10;
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.fillRect(0, 0, mapPanelSize, mapPanelSize);
g2d.setColor(Color.red);
g2d.fillOval(player.getMapPositionX(), player.getMapPositionY(),
DIAMETER, DIAMETER);
}
}
}
import java.awt.event.KeyEvent;
public class Player {
private int mapPositionX;
private int mapPositionY;
private int xa;
private int ya;
public int getMapPositionX() {
return mapPositionX;
}
public void setMapPositionX(int mapPositionX) {
this.mapPositionX = mapPositionX;
}
public int getMapPositionY() {
return mapPositionY;
}
public void setMapPositionY(int mapPositionY) {
this.mapPositionY = mapPositionY;
}
public void movePlayer() {
if (mapPositionX + xa > 0) {
mapPositionX += xa;
}
if (mapPositionY + ya > 0) {
mapPositionY += ya;
}
}
public void keyPressed(KeyEvent event) {
/*
* switch(event.getKeyCode()){
* case(KeyEvent.VK_LEFT):
* System.out.println("KEY-LEFT"); xa= -1; movePlayer();
*
* case(KeyEvent.VK_RIGHT): xa= 1; movePlayer();
*
* case(KeyEvent.VK_DOWN): ya = 1; movePlayer();
*
* case(KeyEvent.VK_UP): ya = -1; movePlayer(); }
*/
if (event.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("KEY-LEFT");
xa = -1;
movePlayer();
}
if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("KEY-Right");
xa = 1;
movePlayer();
}
if (event.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("KEY-Up");
ya = -1;
movePlayer();
}
if (event.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("KEY-Down");
ya = 1;
movePlayer();
}
}
public void keyReleased(KeyEvent event) {
xa = 0;
ya = 0;
}
}