I'm new to Swing, and still relatively new to Java as a whole, and am having trouble with my first Swing program. I'm trying to make a simple Pong game - it will display the title screen with a "Play" button on it that when pressed starts the game.
Here is what I have. The problem is that after the button is pressed, the ball, enemy, and player will not be painted.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class Pong extends JApplet implements ActionListener, Runnable, KeyListener
{
private Image dbImage;
private Graphics dbg;
private JButton play;
private Toolkit toolkit;
private Ball ball;
private Player player;
private Enemy enemy;
private Thread main;
Container panel = getContentPane();
public void init()
{
addKeyListener(this);
toolkit = getToolkit();
panel.setBackground(Color.black);
panel.setLayout(null);
play = new JButton("PLAY!");
play.setBounds(200, 174, 100, 25);
play.addActionListener(this);
play.setActionCommand("play");
panel.add(play);
initilizeBall();
initilizeEnemy();
initilizePlayer();
main = new Thread(this);
main.start();
}
public void start()
{
}
public void stop()
{
}
public void paintComponent(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if (StaticPong.gameState == StaticPong.PLAY) {
enemy.drawCpu(g2d);
player.drawPlayer(g2d);
ball.drawBall(g2d);
}
}
public void destroy()
{
}
public void run()
{
while(true) {
repaint();
while(StaticPong.gameState == StaticPong.PLAY) {
enemy.moveCpu(2, ball);
player.movePlayer(2);
ball.moveBall(2);
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
repaint();
}
}
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == 38) {
player.setPlayerMoveUp(true);
}
else if (key == 40) {
player.setPlayerMoveDown(true);
}
}
public void keyReleased(KeyEvent e)
{
if (player.getGoUp() == true) {
player.setPlayerMoveUp(false);
}
else if (player.getGoDown() == true) {
player.setPlayerMoveDown(false);
}
}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent ae)
{
if (ae.getActionCommand().equals("play")) {
toolkit.beep();
play.setVisible(false);
play = null;
StaticPong.gameState = StaticPong.PLAY;
}
}
private void initilizeBall() {
Random rng = new Random();
int startBallX = 150; int startBallY = rng.nextInt(150) + 25;
ball = new Ball(startBallX, startBallY, true);
}
private void initilizePlayer() {
int startX = 80; int startY = 100;
player = new Player(startX, startY);
}
private void initilizeEnemy() {
int startX = 80; int startY = 100;
enemy = new Enemy(startX+340, startY);
}
}
import java.awt.*;
public class Player
{
private int xPos; private int yPos;
private int points;
private boolean goUp = false; private boolean goDown = false;
public Player(int xPos, int yPos)
{
this.xPos = xPos;
this.yPos = yPos;
points = 0;
}
public void movePlayer(int speed)
{
if (goDown == true) {
yPos += speed;
}
else if (goUp == true) {
yPos -= speed;
}
else {}
}
public int getPlayerPoints()
{
return points;
}
public void addPlayerPoints()
{
points++;
}
public void setPlayerMoveUp(boolean goUp)
{
this.goUp = goUp;
}
public void setPlayerMoveDown(boolean goDown)
{
this.goDown = goDown;
}
public boolean getGoUp()
{
return goUp;
}
public boolean getGoDown()
{
return goDown;
}
public void drawPlayer(Graphics2D g)
{
g.setColor(Color.blue);
g.fillRect(xPos, yPos, 20, 40);
g.setColor(Color.white);
Font font = new Font("Arial", Font.BOLD, 20);
g.setFont(font);
g.drawString(""+points, xPos+3, yPos+25);
}
public int getX()
{
return xPos;
}
public int getY()
{
return yPos;
}
}
import java.awt.*;
public class Enemy
{
private int xPos; private int yPos;
private int points;
public Enemy(int xPos, int yPos)
{
this.xPos = xPos;
this.yPos = yPos;
}
public void moveCpu(int speed, Ball ball)
{
if (ball.getTowardsEnemy() == true && ball.getX() > 260) {
if (ball.getY() > yPos+25) {
yPos += speed;
}
else if (ball.getY() < yPos+25) {
yPos -=speed;
}
}
else {
if (yPos < 75) {
yPos += speed;
}
if (yPos > 75) {
yPos -= speed;
}
}
}
public void drawCpu(Graphics2D g)
{
g.setColor(Color.red);
g.fillRect(xPos, yPos, 20, 40);
g.setColor(Color.white);
Font font = new Font("Arial", Font.BOLD, 20);
g.setFont(font);
g.drawString(""+points, xPos+3, yPos+25);
}
public int getEnemyPoints()
{
return points;
}
public void addEnemyPoints()
{
points++;
}
public int getX()
{
return xPos;
}
public int getY()
{
return yPos;
}
}
import java.awt.*;
public class Ball
{
private int xPos; private int yPos; private int yspeed;
private boolean towardsEnemy; private boolean goUp;
public Ball(int xPos, int yPos, boolean towardsEnemy)
{
this.xPos = xPos;
this.yPos = yPos;
this.towardsEnemy = towardsEnemy;
this.goUp = true;
yspeed = 2;
}
public void moveBall(int xspeed)
{
if (towardsEnemy == true && goUp == false) {
xPos += xspeed;
yPos += yspeed;
}
else if (towardsEnemy == false && goUp == false) {
xPos -= xspeed;
yPos += yspeed;
}
else if (towardsEnemy == true && goUp == true) {
xPos += xspeed;
yPos -= yspeed;
}
else if (towardsEnemy == false && goUp == true) {
xPos -= xspeed;
yPos -= yspeed;
}
}
public void drawBall(Graphics g)
{
g.setColor(Color.green);
g.fillOval(xPos, yPos, 10, 10);
}
public void setGoUp(boolean goUp)
{
this.goUp = goUp;
}
public void setTowardsEnemy(boolean towardsEnemy)
{
this.towardsEnemy = towardsEnemy;
}
public int getX()
{
return xPos;
}
public int getY()
{
return yPos;
}
public boolean getGoUp()
{
return goUp;
}
public void addYSpeed(int speed)
{
yspeed += speed;
}
public int getYSpeed()
{
return yspeed;
}
public boolean getTowardsEnemy()
{
return towardsEnemy;
}
}
public class StaticPong
{
public static final int START = 0;
public static final int PLAY = 1;
public static final int END = 2;
public static int gameState = START;
}
This should go up to pressing "Play" and then it should show the enemy moving, the ball moving, and the player moving if you have the up/down keys going. But for some reason, its not painting the ball, enemy, and player like I thought it would.
Any help?