0

I'm wondering how can i store objects in a vector and then draw them with the paintComponent(Graphics g) method later.

My drawing class is atm:

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

public class PaintingPanel extends JPanel{

public static Rectangle playField = new Rectangle(10, 10, 800, 800);
public static int playFieldCentreX = 399;
public static int playFieldCentreY = 399;

public static Vector contents;

public PaintingPanel(Vector contents)
{   
    contents = new Vector();
    contents.addElement(Breakout.paddle);
    contents.addElement(Breakout.ball);



}

public void paintComponent(Graphics g)
{

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.draw(playField);
    Breakout.paddle.paintThis(g2d);
    Breakout.ball.paintThis(g2d);

}

}

I'm also not sure if i have added the objects correct in the vector, if i do System.out.println(contents); i just get a vector with 2 null elements.

the Breakout.ball, and paddle elements is both 2 Rectangles. If i try to draw them directly as i have done so far in the bottom it works fine, but its when i want to add in like 20+ bricks i would need to store all the objects in a vector and then draw it all out.

If anyone got some good pointers, or a link to a good tutorial that would be great!

Edit: parts of breakout class

public class Breakout extends TimerTask implements KeyListener {                
public static Ball ball;
public static Paddle paddle;
public static PaintingPanel panel;
public static boolean gameRunning;
public static Breakout game;    
public Breakout()
{

}   
public static void setupGame()
{
    paddle = new Paddle(350, 790, 100, 10); //initial paddle creation                           
    ball = new Ball(393, 400, 7);

    showGUI();
}   
public static void beginGame()
{
    gameRunning = true;         
}   
public void run()
{
    if(gameRunning == true)
    {
        Ball.move();
        Paddle.move();

    }
}
}   
public static void main(String[] args)
{   
    Timer t = new Timer();  
    t.schedule(new Breakout(), 0, 40);

    panel = new PaintingPanel(PaintingPanel.contents);
    setupGame();
    beginGame();    

    while (gameRunning == true)
    {
            panel.repaint();
    }       

    // TODO a lot
}
private static void showGUI()
{       
    JFrame frame = new JFrame("Breakout");  
    game = new Breakout();                   

    frame.addKeyListener(game);

    frame.add(panel);

    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);                          //  create game window
    frame.setVisible(true);                                 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
}
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3483682
  • 51
  • 1
  • 7

2 Answers2

2

What you should do is have each object class (if they are even classes, which IMO, they should be), implement a Drawable interface, with a single draw(Graphics g) method.

public interface Drawable {
    public void draw(Graphics g);
}

public class Ball implenents Drawable {
    @Override
    public void draw(Graphics g) {
        // draw ball
    }
}

Then the Vector (I recommend a List instead) could be typed Drawable. Then you can just loop through it, calling the draw method of each object. (That is after adding all your Drawable objects to it.

public class PaintingPanel extends JPanel {
    private List<Drawable> objects;
    BreakOut breakOut;

    public PaintingPanel(BreakOut breakout) {
        this.breakOut = breakOut;
        this.objects = breakOut.getSomething();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Drawable object: objects) {
            object.draw(g);
        }
    }
}

IMO, this would be the better design. So you can add any object that implements the Drawable interface into the List<Drawable>


See some example here and here and here and here and here and here that draw multiple objects and use a javax.swing.Timer for animation, which maybe you want to consider doing.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

We'd need to see your Breakout class to know whether you actually added anything or not. If they're null objects, then it means they were never instantiated. So, basically, call the constructor and use its returned object reference to assign Breakout.paddle and Breakout.ball, respectively.

Gurgadurgen
  • 408
  • 3
  • 13
  • the Breakout.paddle.paintThis(g2d); and Breakout.ball.paintThis(g2d); will draw my ball and paddle as it should, will paste in some of the breakout class anyway if its needed though – user3483682 Apr 02 '14 at 04:11