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);
}
}