Hi im making a project for my studies and im new in java. I need to do a game - Arkanoid. What i wanted to do is a game manu with 2 buttons "New game" and "Quit". Someone from stackoverflow.com rebuild my menu code but it still doesnt work :( After clicking a "New Game" im getting blank frame :(. Probably becouse of Gra
constructor. How constructor of Gra
need to look like to make it works with my menu code.
Here is the code (i will paste all Arkanoid
class code and most important parts of Gra
class):
Arkanoid class:
package arkanoid;
import javax.swing.*;
import java.awt.event.*;
public class Arkanoid extends JFrame
{
private static final long serialVersionUID = 5647459635833443178L;
public Arkanoid() {
super("Arkanoid");
setSize(500,400);
setTitle("Arkanoid BETA");
setLocationRelativeTo(null);
setResizable(false);
final JPanel panel = new JPanel();
setContentPane(panel);
panel.add(new JButton(new AbstractAction("New game") {
public void actionPerformed (ActionEvent e) {
panel.removeAll();
panel.add(new Gra()); //constructor of Gra need to return some Panel i think but i dont know how to do that
panel.revalidate();
panel.repaint();
}
}));
panel.add(new JButton(new AbstractAction("Quit") {
public void actionPerformed (ActionEvent e) {
Arkanoid.this.setVisible(false);
}
}));
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Arkanoid frame = new Arkanoid();
frame.setVisible(true);
}
});
}
}
Gra class:
package arkanoid;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Gra extends JPanel
{
/*some not important variables*/
Pilka pilka;
Paletka paletka;
Cegla cegla_tab[];
Serce serce_tab[];
Timer timer;
public Gra()
{
addKeyListener(new TAdapter());
setFocusable(true);
cegla_tab = new Cegla[liczba_cegiel];
serce_tab = new Serce[max_zycia];
//setDoubleBuffered(true);
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}
public void addNotify()
{
super.addNotify();
start_gry();
}
public void start_gry()
{
pilka = new Pilka();
paletka = new Paletka();
/* painting a bricks and lifes for game start */
}
public void koniec_gry() //end of a game
{
czas_konca = System.currentTimeMillis();
gra = 3;
timer.cancel();
}
public void paint(Graphics g)
{
super.paint(g);
//repaiting ball positions, bricks and paddle
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
paletka.klawisz_puszczony(e);
}
public void keyPressed(KeyEvent e)
{
paletka.klawisz_wcisniety(e);
}
}
class ScheduleTask extends TimerTask
{
public void run()
{
if(paletka.czy_pausa()==false)
{
pilka.przesun();
paletka.przesun();
czy_zderzenie();
repaint();
}
}
}
public void czy_zderzenie()
{
//checking a collisions with bricks
}
}