0

嗨,我正在为我的学习制作一个项目,并且我是 Java 新手。我需要做一个游戏——打砖块。我想做的是一个带有“新游戏”和“退出”两个按钮的游戏手册。来自stackoverflow.com的人重建了我的菜单代码,但它仍然不起作用:(单击“新游戏”后我得到空白框架:(。可能是因为Gra构造函数。Gra需要如何构造函数才能使其与我的菜单代码一起使用. 这是代码(我将粘贴所有Arkanoid类代码和类的最重要部分Gra):

打砖块类:

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

格拉班:

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
}
}
4

1 回答 1

2

您需要覆盖paintComponent,而不是paint。

此外,Gra 的构造函数确实返回了一个 JPanel。构造函数将返回您正在构造的对象,在本例中是一个 Gra,它也是一个 JPanel。这是在工作的多态性。

哦,根据上面的评论,你真的不应该处理图形对象。

于 2013-05-31T18:58:53.437 回答