我目前正在开发一个简单的java游戏。我总共有四个课程,Main,Countdown,Menu和MenuPanel。倒计时是一个单独的框架,我试图添加到我的主框架。菜单是我试图显示的游戏菜单,但每次我都这样,它只是一个空白的白框。我不能让这些工作。倒计时不显示,菜单只是空白。以下是我正在使用的课程:
主: package finalproject;
import basicgraphics.BasicFrame;
import basicgraphics.CollisionEventType;
import basicgraphics.Sprite;
import basicgraphics.SpriteCollisionEvent;
import basicgraphics.SpriteComponent;
import basicgraphics.images.Picture;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JLabel;
class Rim extends Sprite {
Picture basePic;
Rim() throws IOException {
basePic = new Picture("rim.jpg");
setPicture(basePic);
}}
class Backboard extends Sprite {
Picture basePic;
Backboard() throws IOException {
basePic = new Picture("backboard.jpg");
setPicture(basePic);
}
public void processEvent(SpriteCollisionEvent ce) {
if (ce.eventType == CollisionEventType.SPRITE) {
if (ce.xlo) {
setVelX(Math.abs(getVelX()));
}
if (ce.xhi) {
setVelX(-Math.abs(getVelX()));
}
if (ce.ylo) {
setVelY(Math.abs(getVelY()));
}
if (ce.yhi) {
setVelY(-Math.abs(getVelY()));
}
}}}
class Background extends Sprite{
Picture basePic;
Background() throws IOException{
basePic = new Picture("background.jpg");
setPicture(new Picture("background.jpg"));
}}
class BasketBall extends Sprite {
Picture basePic;
BasketBall() throws IOException {
basePic = new Picture("basketball2.png");
setPicture(basePic);
}
public void preMove() {
this.setVelY(this.getVelY() + 0.03);
}
//public int score = 0;
//public void processEvent(SpriteCollisionEvent se) {
//if(se.sprite2 instanceof Rim) {
// int score;
// System.out.println("score");
//}}
JLabel scoreLabel = new JLabel("Score: 0");
public void scored(){
//scoreLabel.setText("Score: " + score);
}
@Override
public void processEvent(SpriteCollisionEvent ce) {
if (ce.eventType == CollisionEventType.WALL) {
if (ce.xlo) {
setVelX(Math.abs(getVelX()));
}
if (ce.xhi) {
setVelX(-Math.abs(getVelX()));
}
if (ce.ylo) {
setVelY(Math.abs(getVelY()));
}
if (ce.yhi) {
setVelY(-Math.abs(getVelY()));
}
}}}
public class Main {
public static enum STATE{
MENU,
GAME;
};
public static STATE State = STATE.MENU;
public static Countdown countdown;
//public static Menu menu;
//public static MenuPanel menupanel;
//int score = 0;
//JLabel scoreLabel = new JLabel("Score: 0");
public void scored()
{
//scoreLabel.setText("Score: " + score);
//scoreLabel.setBounds(5, 5, 50, 50);
}
public static void main(String[] args) throws IOException {
countdown = new Countdown();
//menu = new Menu("Basketball", "Background.jpg");
BasicFrame bball = new BasicFrame("BasketBall");
SpriteComponent sc = new SpriteComponent();
final BasketBall ball = new BasketBall();
ball.setX(350);
ball.setY(280);
//final Countdown countdown = new Countdown();
//countdown.setX(350);
//countdown.setY(280);
Background court = new Background();
court.setX(0);
court.setY(0);
Rim rim = new Rim();
rim.setX(163);
rim.setY(186);
Backboard backboard = new Backboard();
backboard.setX(159);
backboard.setY(140);
bball.add("BasketBall",sc,0,0,1,1);
//menu.add("Menu",sc, 0, 0, 1, 1);
sc.setPreferredSize(new Dimension(575,400));
sc.addSprite(court);
//sc.addSprite(countdown);
sc.addSprite(rim);
sc.addSprite(backboard);
sc.addSprite(ball);
bball.show();
//if(State == STATE.GAME){
sc.start(0,5);
//}
//else if (State == STATE.MENU){
menu.show();
//}
bball.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke){
//if (State == STATE.GAME){
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
{
ball.setVelX(ball.getVelX() + -0.2);
}
else if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
ball.setVelX(ball.getVelX() +0.2);
}
else if(ke.getKeyCode() == KeyEvent.VK_UP)
{
ball.setVelY(ball.getVelY() - 1.0);
}
}});
}}
倒计时: package finalproject;
import basicgraphics.Sprite;
import basicgraphics.images.Picture;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Countdown extends Sprite{
Timer timer;
int counter = 10;
/*Picture basePic;
Countdown() throws IOException {
if (counter == 1);
basePic = new Picture("one.png");
if (counter == 2);
basePic = new Picture("two.png");
}*/
public void Countdown() {
final JFrame frame = new JFrame("timer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel("10");
JPanel panel = new JPanel();
panel.add(label, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
//panel.add(frame);
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(counter));
counter--;
if (counter == -1) {
timer.removeActionListener(this);
timer.stop();
}
}
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Countdown();
}
});
}
}
菜单: package finalproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menu extends JFrame
{
private Image background;
private MenuPanel mp;
private ActionListener al;
private Box b;
private final String MENU[] = {"New Game", "Continue", "High Scores",
"Exit"};
public Menu(String name, String imagename)
{
super(name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex)
{
}
mp = new MenuPanel(imagename);
mp.setBounds(0, 0, 700, 600);
getContentPane().add(mp);
setSize(700, 600);
}
public Menu(String name, ActionListener al)
{
super(name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex)
{
}
mp = new MenuPanel();
mp.setBounds(0, 0, 700, 600);
getContentPane().add(mp);
for(int i = 0; i < MENU.length; i++)
{
JButton jb = new JButton(MENU[i]);
this.add(jb);
jb.addActionListener(al);
jb.setBounds(250, 400 + 32 * i, 200, 30);
mp.add(jb);
}
setSize(700, 600);
setVisible(true);
}
public Menu(String name)
{
super(name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex)
{
ex.printStackTrace();
}
mp = new MenuPanel();
mp.setBounds(0, 0, 700, 600);
getContentPane().add(mp);
setSize(700, 600);
}
public void setActionListener(ActionListener al)
{
for(int i = 0; i < MENU.length; i++)
{
JButton jb = new JButton(MENU[i]);
this.add(jb);
jb.addActionListener(al);
jb.setBounds(250, 400 + 32 * i, 200, 30);
mp.add(jb);
}
setVisible(true);
}
public Menu(String name, String imagename, ActionListener al)
{
super(name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex)
{
ex.printStackTrace();
}
mp = new MenuPanel(imagename);
mp.setBounds(0, 0, 700, 600);
getContentPane().add(mp);
for(int i = 0; i < MENU.length; i++)
{
JButton jb = new JButton(MENU[i]);
this.add(jb);
jb.addActionListener(al);
jb.setBounds(250, 400 + 32 * i, 200, 30);
mp.add(jb);
}
setSize(700, 600);
setVisible(true);
}
}
MenuPanel: package finalproject;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuPanel extends JPanel
{
private String imgName;
public MenuPanel()
{
super();
setLayout(null);
imgName = "";
setSize(700, 600);
setVisible(true);
}
public MenuPanel(String image)
{
super();
setLayout(null);
setSize(700, 600);
imgName = image;
setVisible(true);
}
public void paintComponent(Graphics g)
{
if(!imgName.equals(""))
{
try
{
MediaTracker mt = new MediaTracker (this);
Image intro =
Toolkit.getDefaultToolkit().getImage(imgName);
mt.addImage(intro, 0);
mt.waitForID(0);
g.drawImage(intro, 0, 0, this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
对不起,我是新来的。任何帮助,将不胜感激。我真的需要帮助。感谢。