0

Im currently trying to create a brick breaker game however I am experiencing a few issues.

I currently have an abstract class shape and methods for square and circle to draw the shapes for the game. However in my main method when I try to add the shapes it only outputs one shape so in this case for my code it only displays the circle and not the paddle. I was wondering if anyone could help me resolve this?

Main

The line where im adding the new square is not working in this case.

import javax.swing.*;
import java.awt.*;

public class Main {

public static void main(String[] args) {
    FilledFrame frame = new FilledFrame();

    frame.setSize(700, 600);
    frame.setResizable( false );
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setTitle("CE203_RegNo_1603977");
    frame.setVisible( true );

}



static class FilledFrame extends JFrame{


    public FilledFrame(){


        // left border
        JPanel leftBorder   = new JPanel();
        leftBorder.setSize(4, 700);
        leftBorder.setBackground(Color.BLACK);
        add(leftBorder, BorderLayout.WEST);

        // top border
        JPanel topBorder    = new JPanel();
        topBorder.setSize(700, 4);
        topBorder.setBackground(Color.BLACK);
        add(topBorder, BorderLayout.NORTH);

        //right border
        JPanel rightBorder  = new JPanel();
        rightBorder.setSize(4, 700);
        rightBorder.setBackground(Color.BLACK);
        add(rightBorder, BorderLayout.EAST);

        /*
        JPanel cenBorder = new JPanel();
        cenBorder.setSize(696, 696);
        cenBorder.setBackground(Color.blue);
        add(cenBorder, BorderLayout.CENTER);
        */


        // create paddle

        Square paddle       = new Square(100, 8, 310, 550, Color.BLUE);
        add(paddle, BorderLayout.SOUTH);


        // create ball
        Circle ball        = new Circle(20, 20, 120, 350, Color.YELLOW);
        add(ball, BorderLayout.CENTER);
    }

}


}

Shape

import javax.swing.*;
import java.awt.*;

public abstract class Shape extends JPanel {

public int posX;
public int posY;
public Color color;


public Shape(int posX, int posY, Color color){
    this.posX   = posX;
    this.posY   = posY;
    this.color  = color;


}

public abstract void paintComponent (Graphics g);

}

Square

import java.awt.*;

import java.awt.Color;

public class Square extends Shape {

protected int width;
protected int height;

public Square(int width, int height, int posX, int posY, Color color) {

    super(posX, posY, color);

    this.width  = width;
    this.height = height;

}


@Override
public void paintComponent (Graphics g) {
    g.setColor(color);
    g.fillRect(posX, posY, width, height);



}
}

Circle

import java.awt.*;

public class Circle extends Shape {

protected int width;
protected int height;


public Circle(int width, int height, int posX, int posY, Color color) {
    super(posX, posY, color);

    this.width = width;
    this.height = height;


}

@Override
public void paintComponent(Graphics g) {

    g.setColor(color);
    g.fillOval(posX, posY, width, height);


}
}
Virdee
  • 17
  • 6
  • A big mistake is to have Shape extend from JPanel. It should be a logical class not a component class. Only one class should do all the drawing – Hovercraft Full Of Eels Nov 27 '18 at 17:58
  • The default layout of a JFrame is BorderLayout, add without constraint put the component in center position. The second add replace the paddle by the circle. You have to change the layout. – Aubin Nov 27 '18 at 17:58
  • @Aubin I have updated the code above to include BorderLayout on the square shape however im still getting the same issue and only the circle is displaying – Virdee Nov 27 '18 at 18:05
  • @HovercraftFullOfEels How would you recommend I resolve this issue as whenever I remove 'extends JPanel' from the class I get an error when trying to create and add the square and circle shapes – Virdee Nov 27 '18 at 18:06
  • See [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/). It demonstrates how to paint multiple Rectangle shapes. Modify the basic logic to support a generic Shape and you will be able to paint squares or circles or triangles etc. – camickr Nov 27 '18 at 18:19
  • See [this answer](https://stackoverflow.com/questions/41941855/create-the-square-rectangle-triangle-of-java-in-jframe/41944799#41944799) and [this answer](https://stackoverflow.com/a/42167399/2180785) maybe they will help you implement @HovercraftFullOfEels recommendation. – Frakcool Nov 27 '18 at 20:19

0 Answers0