0

This is the code of my main class for the game of Pong and it works fine till I add the instruction button. When I add the instruction button the screen no longer shows anything other than the button.

 package pongg;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
//import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Pong  extends JFrame implements ActionListener, KeyListener{
    public static Pong pong;
    int width =700, height = 700;
    public Renderer renderer;
    public Paddle player1;
    public Paddle player2;
    public Ball ball;
    JButton rules = new JButton ("INSTRUCTIONS"); 


    public boolean comp = false; //ai 
    public boolean w, s, up, down;  // so many booleans so that when u press two keys they dont interfere w each other
    public int gameStatus = 0; // 1 = paused, 0= stop, 2=play

    public Pong(){
    Timer timer = new Timer(20, this);
    JFrame jframe = new JFrame("PONG"); 
    renderer = new Renderer();
    jframe.setSize(width +15, height+35);
    jframe.setVisible(true);
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.add(renderer);


    jframe.addKeyListener (this);

    timer.start();

    if (gameStatus ==0){
        rules.setSize(200,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.add(rules);
        setVisible(true);
    }

    }

    private void add(JButton rules2) {


    }

    public void start (){

    gameStatus =2;
    player1 = new Paddle(1);
    player2 = new Paddle (2);
    ball = new Ball(this);
    }

    public void update (){
    if (w){
    player1.move(true); // up is true and down is false
    }

    if (s){
    player1.move(false);
    }

    if (up){
    player2.move(true);
    }

    if (down){
    player2.move(false);
    }

    ball.update(player1, player2);
    }


    public void render (Graphics2D g){

    g.setColor (Color.ORANGE);
    g.fillRect(0,0, width, height);
    //g.setRenderingHint (RenderingHints.KEY_ANTIALISING, RenderingHint.VALUE_ANTIALIAS_ON);

    if (gameStatus ==0){

    g.setColor(Color.BLACK);
    g.setFont(new Font("Arial", Font.PLAIN, 40)); 
    g.drawString ("WELCOME TO PONG TWIST!", width/10, 100);

    g.setFont(new Font("Arial", Font.PLAIN, 20)); 
    g.drawString ("Press Space to Play", width/2 -150, height/2 - 50 );
    g.drawString ("Press Shift to Play with Computer", width/2 -200, height/2);


    }

    if (gameStatus ==2 || gameStatus ==1){

    g.setColor(Color.WHITE);
    g.setStroke(new BasicStroke (5f)); // make it thicker
    g.drawLine(width/2, 0, width/2, height); // FIX MAYBE


    player1.render (g);
    player2.render(g);
    ball.render(g);
    }
    if (gameStatus ==1){
    g.setColor(Color.WHITE);

    g.drawString ("PAUSED", width/2 -105, height /2 - 105);
    }
    }
K.K
  • 1

1 Answers1

0

One of a possible problem is the incorrect layout. You can try before add rules.setLayout(null):

rules.setSize(200, 100);
jframe.add(rules);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Also, you can have more explanation here:

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38