gpt4 book ai didi

java - 为什么在此程序中的任何时候都没有调用 Paint() 方法?

转载 作者:行者123 更新时间:2023-11-30 03:35:47 24 4
gpt4 key购买 nike

这是我正在开始制作的益智游戏的代码。我试图让它在屏幕中间显示“已暂停”,但无法使其正常工作。事实证明,即使使用各种 repaints(),也没有发生任何事情。为什么?是因为类本身实际上并没有扩展 JPanel 或 JFrame 吗?

package main;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PuzzleSquares implements ActionListener, KeyListener, Runnable {
boolean running = false;
boolean inGame = false;
boolean paused = false;

JFrame frame = new JFrame();
JPanel menu = new JPanel();
JPanel game = new JPanel();

Thread thread;

public PuzzleSquares() {
frame.requestFocus();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Just another puzzle game");

addMenu();

frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}

public void addMenu() {
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
menu.setFocusable(true);
menu.setBackground(Color.GRAY);

createButton("START", menu);
createButton("HIGH SCORES", menu);
createButton("EXIT", menu);

frame.revalidate();
frame.add(menu);
}

public void addGame() {
game.setBackground(Color.GREEN);
game.addKeyListener(this);
game.setFocusable(true);
game.setPreferredSize(frame.getMaximumSize());

frame.add(game);
frame.revalidate();
game.requestFocus();
}

public void createButton(String title, Container c) {
JButton button = new JButton(title) {
{
setSize(200, 30);
setMaximumSize(getSize());
}
};
button.setAlignmentX(JPanel.CENTER_ALIGNMENT);
button.addActionListener(this);
button.setActionCommand(title);

c.add(button);
}

public synchronized void start() {
if (running) { return; }

running = true;

thread = new Thread(this);
thread.start();
}

public synchronized void stop() {
if (!running) { return; }

running = false;

try { thread.join(); } catch (Exception e) {}
}

@Override
public void run() {
while (running) {

}
}

public void paint(Graphics g) {
System.out.println("Hi");
if (paused) {
g.setColor(Color.BLACK);
for (int i=0;i<100;i++) {
g.drawLine(i, i+1, i, i+1);
}
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("START")) {
System.out.println("Start");
frame.remove(menu);
addGame();
}
if (e.getActionCommand().equals("HIGH SCORES")) {
System.out.println("High Scores");
}
if (e.getActionCommand().equals("EXIT")) {
System.out.println("Exit");
}
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (!paused) {
System.out.println("Pause");
paused = true;
} else {
paused = false;
}
game.repaint();
}

}

@Override
public void keyReleased(KeyEvent e) {

}

public static void main(String[] args) {
PuzzleSquares ps = new PuzzleSquares();
ps.start();
}
}

最佳答案

基本上,您的类是不可绘制的,它不会扩展框架可以绘制的任何内容(例如 JComponentJPanel)

如果您尝试做类似的事情...

@Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("Hi");
if (paused) {
g.setColor(Color.BLACK);
for (int i=0;i<100;i++) {
g.drawLine(i, i+1, i, i+1);
}
}
}

它无法编译,因为该方法不会重写其父类或接口(interface)中的任何方法。

参见Painting in AWT and SwingPerforming Custom Painting了解更多详情

我也不鼓励使用 KeyListener 并建议使用键绑定(bind) API,请参阅 How to Use Key Bindings 。它将帮助解决您的注意力问题

关于java - 为什么在此程序中的任何时候都没有调用 Paint() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27935742/

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