在按钮开始时从游戏循环调用绘制方法

时间:2012-09-02 16:41:43

标签: java swing

嗨我每次都试图在游戏循环中调用paint方法。一旦按下按钮,屏幕弹出一个标签和一个按钮,标签和按钮就会转到我想要但我可以得到绘画方法开始我试过j.repaint()& j.validate()都没有访问paint方法。任何帮助都将不胜感激。

package sgame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SGame extends JPanel
{

    public static void main(String[] args)
    {

        final JFrame window = new JFrame("hello");
        final JPanel window1 = new JPanel();
        Timer loop = new Timer(1000, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                GameLoop(window1);

            }
        });
        Window(window, loop);

    }

    public static void Window(final JFrame window, final Timer loop)
    {
        final JButton b1 = new JButton("GO!");
        b1.setLocation(210, 300);
        b1.setSize(70, 50);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(500, 500);
        window.setLocation(420, 170);
        window.setBackground(Color.BLACK);
        final JLabel Title = new JLabel("Snake", JLabel.CENTER);
        Title.setFont(new Font("Times New Roman", Font.ITALIC, 60));
        Title.setVerticalAlignment(JLabel.CENTER);
        Title.setForeground(Color.WHITE);
        window.add(b1);
        window.add(Title);
        b1.addActionListener(new ActionListener() // runs when buttons pressed
            {

                @Override
                public void actionPerformed(ActionEvent e) // clears header,button and starts timer
                {
                    b1.invalidate();
                    b1.setVisible(false);
                    Title.setVisible(false);
                    Title.invalidate();
                    loop.start();
                }

            });

    }

    public static void GameLoop(JPanel j)
    {
        // Call paint method
    }

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.yellow);
        g.drawRect(30, 30, 30, 30);
    }
}

1 个答案:

答案 0 :(得分:1)

paintComponent()中的SGame方法永远不会被调用,因为您没有实例化任何Game对象。并且窗口1尚未添加到框架中。

这样的事情会更好:

public class SGame extends JPanel {

    private Timer loop;

    public SGame(){
        loop = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameLoop();
            }
        });
    }

    public static void main(String[] args) {
        final JFrame window = new JFrame("hello");
        final JButton b1 = new JButton("GO!");
        b1.setLocation(210, 300);
        b1.setSize(70, 50);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(500, 500);
        window.setLocation(420, 170);
        window.setBackground(Color.BLACK);
        final JLabel Title = new JLabel("Snake", JLabel.CENTER);
        Title.setFont(new Font("Times New Roman", Font.ITALIC, 60));
        Title.setVerticalAlignment(JLabel.CENTER);
        Title.setForeground(Color.WHITE);
        window.add(b1);
        window.add(Title);
        b1.addActionListener(new ActionListener() { // runs when buttons pressed
            @Override
            public void actionPerformed(ActionEvent e) // clears header,button
            {
                b1.invalidate();
                b1.setVisible(false);
                Title.setVisible(false);
                Title.invalidate();
                SGame sg = new SGame();
                window.add(sg);
                sg.start();
            }
        });;
    }

    public void start(){
        loop.start();
    }

    public void gameLoop() {
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.yellow);
        g.drawRect(30, 30, 30, 30);
    }
}