Hello I am currently making a small game in java, and I am running into problems trying to making the gamepanel. I am using a JPanel as the basis and JPanel to the top to put in a HUD, and a gamepanel for the playable protion of the game. I can add the two panels the HUD and gamepanel fine. However the objects which are JComponents will not show up at all. I know they are created I'm not sure how to make them visible though.
Here are my classes: This is my window class that holds the panels:
public class Window extends JFrame {
public static final int ScreenWidth = 1000;
public static final int ScreenHeight = 700;
public static JFrame frame;
private StatesHandler statesHandler;
public Window(JFrame frame) throws IOException {
this.frame = frame;
this.statesHandler = new StatesHandler();
displaywindow();
}
public void paint(Graphics g) {
super.paint(g);
}
public void displaywindow() {
this.frame.setLayout(new FlowLayout());
this.frame.setPreferredSize(new Dimension(ScreenWidth,ScreenHeight));
this.frame.setMaximumSize(new Dimension(ScreenWidth,ScreenHeight));
this.frame.setMinimumSize(new Dimension(ScreenWidth,ScreenHeight));
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setResizable(false);
this.frame.setBackground(Color.pink);
this.frame.setLocationRelativeTo(null);
this.frame.pack();
this.frame.setVisible(true);
}
public JFrame getFrame() {
return this.frame;
}
This is this the level class: it holds the hud and the gamepanel private Window win;
Level(Window win) {
this.setPreferredSize(new Dimension(gw,gh));
this.win = win;
this.setFocusable(true);
this.requestFocus();
Hud hud = new Hud();
getwinframe().getContentPane().add(hud);
getwinframe().getContentPane().validate();
gamepanel gmp = new gamepanel();
getwinframe().getContentPane().add(gmp);
getwinframe().getContentPane().revalidate();
Ball ball = new Ball(600, 200, 3, 3);
System.out.println(this);
gmp.add(ball);
gmp.revalidate();
}
Here is the Ball which I am trying to add to gamepanel
public Ball(int xpos, int ypos,int vx, int vy) {
this.xpos =xpos;
this.ypos = ypos;
this.vx = vx;
this.vy = vy;
this.collsionbox = new Rectangle(xpos, ypos, bwidth, bheight);
}
This is the gamepanel which I'm trying to add the ball to
public gamepanel(){
this.setPreferredSize(new Dimension(Window.ScreenWidth, Window.ScreenHeight -Window.ScreenHeight/6));
this.setLayout(new FlowLayout());
System.out.println(Window.ScreenWidth +" " + Window.ScreenHeight/6);
this.setBackground(Color.pink);
Ball ball2 = new Ball(500, 100, 3, 3);
System.out.println(ball2);
add(ball2);
validate();
}