Paint方法未运行

时间:2011-12-02 18:17:26

标签: java swing

我最近一直试图制作我的第一个Java游戏,但是当我的Pong类没有画画时,它已经卡住了。可能是因为它是从Menu.class运行的,但我不确定如何解决这个问题。如果你没有全屏运行,它也不会成为一个新窗口? 不管怎样,谢谢。

public class Menu extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
public static int fs = 0;
Image pong = new ImageIcon ("C:\\Users\\me\\Pictures\\pong.jpg").getImage();
JPanel pane = new JPanel();
JButton start = new JButton("Start!");
JButton exit = new JButton("Exit!");
Menu()

{
    super("Pong"); setBounds(100,100,400,284);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(pane); pane.add(start); pane.add(exit);
    start.addActionListener(this);
    start.requestFocus();
    exit.addActionListener(this);
    exit.requestFocus();
    setVisible(true);

}

public void paint(Graphics g) {
    g.drawImage(pong, 0, 13, null);
}

public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();
    if (source == start)
    {
        int n = JOptionPane.showConfirmDialog(null ,"Would you like to play fullscreen?","Option - Fullscreen", JOptionPane.YES_NO_OPTION);
        setVisible(true);
        if(n == JOptionPane.YES_OPTION){
            fs = 1;
            Pong.StartCall(fs);
        }else if (n == JOptionPane.NO_OPTION) {
            fs = 2;
            Pong.StartCall(fs);
        }
    }else if(source == exit){
        System.exit(0);
    }
}

public static void main(String args[]) {
    new Menu();
}


}


public class Pong extends Applet implements Runnable {

private static final long serialVersionUID = 1L;
static boolean fs;
static Pong p = new Pong();
JFrame w = new JFrame("Pong");

public static void StartCall(int i){
        if(i == 1){
            fs = true;
            p.run();
        }else if(i == 1){
            fs = false;
            p.run();
        }
    }

public void run() {
    if(fs = true){
    w.setSize(getMaximumSize());
    System.out.println("max");
    }else if(fs = false){
    w.setSize(400,400);
    System.out.println("min");
    }
    w.setVisible(true);
    System.out.println("running");

}

public void paint(Graphics g){
    System.out.println("painting");
    g.drawRect(111, 111, w.getWidth(), w.getHeight());
    g.setColor(Color.BLUE);
}

public void draw(Graphics g,Image i,int x,int y) {
    g.drawImage(i,x,y,null);
}

}

0 个答案:

没有答案