I want to create a simple game menue for the game pong. In the background there is a ball that bounces of the edges just to look nice. Now I want to add Buttons to this menue screen. But then the Background isn't painted anymore. So i want to draw a live Background but the buttons should still stay in the front. This live Background is implemented with a loop. In the following are the GUI class and the DrawStartMenueClass.
I heard about Layers but everybody uses them with some kind of tool in NetBeans and I got eclipse so I don't have that opportunity. I guess you can put the buttons and the background in different containers but then again I am not sure how that would. Every help is welcome.
public class GUI {
public GUI() {
//Frame erzeugen
Var.frame = new JFrame("Pong");
Var.frame.setSize(Var.dimension);
Var.frame.setVisible(true);
Var.frame.setResizable(true);
Var.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Var.frame.setLocationRelativeTo(null);
//Komponenten erzeugen
Var.start = new JButton("Start");
Var.optionen = new JButton("Optionen");
Var.modus = new JButton("Modus");
Var.skins = new JButton("Skins");
Var.beenden = new JButton("Beenden");
//Komponenten anpassen
Var.start.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 400, Var.buttonWidth, 50);
Var.optionen.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 500, Var.buttonWidth, 50);
Var.modus.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 600, Var.buttonWidth, 50);
Var.skins.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 700, Var.buttonWidth, 50);
Var.beenden.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 800, Var.buttonWidth, 50);
//Komponenten hinzufügen
Var.frame.getContentPane().add(new DrawStartMenue());
Var.frame.add(Var.start);
Var.frame.add(Var.optionen);
Var.frame.add(Var.modus);
Var.frame.add(Var.skins);
Var.frame.add(Var.beenden);
}
}
package main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class DrawStartMenue extends JPanel{
private static final long serialVersionUID = 1L;
private GridLayout experimentLayout = new GridLayout(6,1);
public Graphics2D g2d;
public void paint(Graphics g) {
//Objekt g zum Zeichnen erzeugen
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Hintergrund
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, Var.screenWidth, Var.screenHeight);
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(8));
g2d.drawRect(Var.whiteRectX1, Var.whiteRectY1 , Var.whiteRectWidth, Var.whiteRectHeight);
//Ball im Hintergrund
for(int i = 0; i < Var.ballList.size(); i++) {
g2d.fillOval(Var.ballList.get(i).ballX, Var.ballList.get(i).ballY,
Var.ballList.get(i).ballWidth, Var.ballList.get(i).ballHeight);
}
//Schriftzug
drawCenteredString(g, "PONG", Var.rect, new Font("NO CONTINUE", Font.PLAIN, 230));
repaint();
}