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



嗨,我试图在游戏循环中每次调用paint方法。现在屏幕弹出了一个标签和一个按钮,一旦按钮被按下,标签和按钮就会转到我想要的位置,但我无法启动绘制方法;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);
    }
}

SGame中的paintComponent()方法从未被调用,因为您没有实例化任何SGame对象。并且window1尚未添加到框架中。

像这样的东西应该更好:

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

最新更新