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?

Another problem, this time in my AWT pong game that has more features (especially since the Swing one doesn't work yet) like CPU level choose, and different types of walls, as well as a level editor. In here, an infinite loop occurs that makes the game stop.

I don't feel like giving you the whole huge thing, so here is what you need to know:


StaticPong.NUMBEROFCOLUMNS = 22,
StaticPong.NUMBEROFLINES = 10,

elementArray is an array of the board elements used in collision testing. It has [NUMBEROFLINES] and [NUMBEROFCOLUMNS] as parameters.

MWalls is a class for moving walls. All it does is call the super constructor (it extends a class called Elements).

Path is another class that just calls the super constructor, it also extends Elements. It doesn't stop the ball, but it is supposed to provide the path for the moving wall to take.

public void moveMovingWalls()
    {
        for (int j=0; j<StaticPong.NUMBEROFLINES; j++) {
            for (int i=0; i<StaticPong.NUMBEROFCOLUMNS; i++) {
                if (elementArray[j][i].getID() == 4) {
                    elementArray[j][i] = new Path(Color.gray, 5);
                        if (j+1<StaticPong.NUMBEROFLINES && elementArray[j+1][i].getID() == 5) {
                            elementArray[j+1][i] = new MWalls(Color.yellow, 4);
                        }
                        else if (j>0 && elementArray[j-1][i].getID() == 5) {
                            elementArray[j-1][i] = new MWalls(Color.yellow, 4);
                        }
                        else if (i+1<StaticPong.NUMBEROFCOLUMNS && elementArray[j][i+1].getID() == 5) {
                            elementArray[j][i-1] = new MWalls(Color.yellow, 4);
                        }
                        else if (i>0 && elementArray[j][i-1].getID() == 5) {
                            elementArray[j][i-1] = new MWalls(Color.yellow, 4);
                        }
                }
            }
        }
    }
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.