0

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
}
}
  • Not all stackoverflow users are polish – Extreme Coders May 31 '13 at 18:17
  • 1
    for a JPanel, you override `paintComponent`, not `paint`, and secondly *please* do not dispose of the Graphics that is passed in. You didn't create it, so you really should not dispose of it. Both these items are readily [findable on this site](http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java) and [please don't dispose](http://stackoverflow.com/questions/14096836/java-garbage-collection-and-graphics-dispose-method) – Anya Shenanigans May 31 '13 at 18:42
  • thank you for this [link](http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java), fallowing this example i write my own working code. What is more i delete line with `g.dispose();` some one of my friend said that i should do this :/ he was wrong then. Thank you and LunaEques one more time –  Jun 01 '13 at 06:34

1 Answers1

2

You need to override paintComponent, not paint.

Also, Gra's constructor does return a JPanel. The constructor will return the object you're constructing, which in this case is a Gra, which is also a JPanel. This is polymorphism at work.

Oh, and as per the comment above, you really shouldn't be disposing of the graphics object.

LunaEques
  • 178
  • 2
  • 8