Adam_Vriend
Adam_Vriend

Reputation: 1

Trying and failing to draw on an already set JPanel

I'm really new to coding with java (and coding in general). I'm currently in the middle of Grade 11 CS at school, but everything we've covered is super simple.

For our culminating, I'm trying to make pong. I realized that I would have to use a JPanel in order to do what I wanted, but we had only been using Console as of yet, so I was coming into this with 0 knowledge.

Anyway, I'm having trouble drawing on an already declared JPanel. Every solution or tutorial on how to draw in JPanel included using an entirely different class, and I have no clue how to implement that into my pre-existing code; that being...

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;


public class pong extends JPanel {

    private JFrame f3, f2, f1;
    private JPanel pLabel, pButton, pPong1, pPong2;
    private JButton b1, b2;
    private JLabel lab, score;

    int singleScore = 0;
    int doubleScore1 = 0;
    int doubleScore2 = 0;

    int By;
    int Bx;

    int Paddle1y;
    int Paddle2y;

    public pong()
    {
        gui();
    }

    public void gui()
    {        
        f1 = new JFrame("Pong!");
        f1.setVisible(true);
        f1.setSize(700, 500);
        f1.setResizable(false);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pButton = new JPanel();
        pButton.setBackground(Color.BLACK);

        pLabel = new JPanel();
        pLabel.setBackground(Color.WHITE);

        b1 = new JButton("1 Player");
        b1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                pong1();
            }

        });

        b2 = new JButton("2 Players");
        b2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                pong2();
            }
        });

        lab = new JLabel("Welcome to Pong!");

        pButton.add(b1);
        pButton.add(b2);

        pLabel.add(lab);

        f1.add(pButton, BorderLayout.SOUTH);
        f1.add(pLabel, BorderLayout.CENTER);
    }

    public void pong1()
    {
        f1.dispose();

        f2 = new JFrame("One Player Pong");
        f2.setVisible(true);
        f2.setSize(700, 500);
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f2.setResizable(false);

        pPong1 = new JPanel();
        pPong1.setVisible(true);
        pPong1.setBackground(Color.WHITE);

        score = new JLabel("Score = " + singleScore);

        f2.add(pPong1);

        pPong1.add(score, BorderLayout.NORTH);
    }

    public void pong2()
    {
        f1.dispose();

        f3 = new JFrame("Two Player Pong");
        f3.setVisible(true);
        f3.setSize(700, 500);
        f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f3.setResizable(false);

        pPong2 = new JPanel();
        pPong2.setVisible(true);
        pPong2.setBackground(Color.WHITE);

        score = new JLabel("Player 1 Score = " + doubleScore1 + "      Player 2 Score = " + doubleScore2);

        f3.add(pPong2);

        pPong2.add(score, BorderLayout.NORTH);
    }

    public static void main(String[] args)
    {        
        new pong();
    } 
}

Anyway, any help you could give me is appreciated. Thank you in advance.

Upvotes: 0

Views: 41

Answers (1)

Pyrrhic
Pyrrhic

Reputation: 65

You overwrite paintComponent() to draw things, and call that by using repaint() when you want to update your display. Methods in the Graphics class will help you, such as Graphics.drawLine(), Graphics.fillRect(), etc.

An example of implementation: https://github.com/Blackop778/Minesweeper/blob/master/src/main/java/blackop778/minesweeper/graphics/MinesweeperPanel.java

Upvotes: 2

Related Questions