gpt4 book ai didi

java - 在按钮开始时从游戏循环调用 paint 方法

转载 作者:搜寻专家 更新时间:2023-11-01 03:41:58 24 4
gpt4 key购买 nike

您好,我试图在游戏循环中每次都调用 paint 方法。此刻,一旦按下按钮,屏幕就会弹出一个标签和一个按钮,标签和按钮就可以了,但我可以无法启动绘画方法我尝试了 j.repaint() 和 j.validate() 都没有访问绘画方法。我们将不胜感激。

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

关于java - 在按钮开始时从游戏循环调用 paint 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12237834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com