为什么背景色不会用repaint(e)填充?
Why won't the background color be filled with repaint(e)?
所以我不确定重绘方法有什么问题。它不会填充新的背景颜色。每当我尝试调试它时,它似乎都没有在该方法中打印出文本。我有 3 个 classes:GameInterface、Renderer 和 RepaintConfiguration。 RepaintConfiguration 扩展自 GameInterface class 并实现 ActionListener。
public class GameInterface {
public static GameInterface gameInterface;
public static JFrame jframe;
private String title;
private Container container;
public GameInterface() {
gameInterface = this;
jframe = new JFrame();
jframe.setSize(1500, 800);
jframe.setResizable(false);
jframe.setTitle("Jetpack");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
public class Renderer extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
RepaintConfiguration.repaintConfiguration.repaint(g);
}
}
public class RepaintConfiguration extends GameInterface implements ActionListener
{
public static RepaintConfiguration repaintConfiguration;
public static Renderer renderer;
public static GameInterface gameInterface;
public RepaintConfiguration() {
super();
repaintConfiguration = this;
renderer = new Renderer();
super.jframe.add(renderer);
}
@Override
public void actionPerformed(ActionEvent e) {
renderer.repaint();
}
public void repaint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, 1500, 800);
}
}
我设法编译了你的代码 运行。 Public 类 必须是内部 类.
我添加了一个main
方法。我添加了对 SwingUtilities
invokeLater
方法的调用。此方法确保在 Event Dispatch Thread.
上创建和执行 Swing 组件
我重新排序了 JFrame
方法调用。 JFrame
方法必须按特定顺序调用。这是我用于所有 Swing 应用程序的顺序。
您没有调整 JFrame
的大小。您调整绘图的尺寸 JPanel
。 JFrame
包含占用一些 space 的装饰。
我留下了你费解的绘图代码。您将很难扩展此代码以实际创建游戏。
这是完整的运行可用代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GameInterface {
private JFrame jframe;
private Renderer renderer;
private RepaintConfiguration repaintConfiguration;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameInterface();
}
});
}
public GameInterface() {
this.repaintConfiguration = new RepaintConfiguration(this);
jframe = new JFrame();
jframe.setTitle("Jetpack");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
this.renderer = new Renderer(repaintConfiguration);
jframe.add(renderer, BorderLayout.CENTER);
jframe.pack();
jframe.setLocationByPlatform(true);
jframe.setVisible(true);
}
public Renderer getRenderer() {
return renderer;
}
public class Renderer extends JPanel {
private static final long serialVersionUID = 1L;
private RepaintConfiguration repaintConfiguration;
public Renderer(RepaintConfiguration repaintConfiguration) {
this.repaintConfiguration = repaintConfiguration;
this.setPreferredSize(new Dimension(1500, 800));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaintConfiguration.repaint(g);
}
}
public class RepaintConfiguration implements ActionListener {
private GameInterface gameInterface;
public RepaintConfiguration(GameInterface gameInterface) {
this.gameInterface = gameInterface;
}
@Override
public void actionPerformed(ActionEvent event) {
gameInterface.getRenderer().repaint();
}
public void repaint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, 1500, 800);
}
}
}
所以我不确定重绘方法有什么问题。它不会填充新的背景颜色。每当我尝试调试它时,它似乎都没有在该方法中打印出文本。我有 3 个 classes:GameInterface、Renderer 和 RepaintConfiguration。 RepaintConfiguration 扩展自 GameInterface class 并实现 ActionListener。
public class GameInterface {
public static GameInterface gameInterface;
public static JFrame jframe;
private String title;
private Container container;
public GameInterface() {
gameInterface = this;
jframe = new JFrame();
jframe.setSize(1500, 800);
jframe.setResizable(false);
jframe.setTitle("Jetpack");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
public class Renderer extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
RepaintConfiguration.repaintConfiguration.repaint(g);
}
}
public class RepaintConfiguration extends GameInterface implements ActionListener
{
public static RepaintConfiguration repaintConfiguration;
public static Renderer renderer;
public static GameInterface gameInterface;
public RepaintConfiguration() {
super();
repaintConfiguration = this;
renderer = new Renderer();
super.jframe.add(renderer);
}
@Override
public void actionPerformed(ActionEvent e) {
renderer.repaint();
}
public void repaint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, 1500, 800);
}
}
我设法编译了你的代码 运行。 Public 类 必须是内部 类.
我添加了一个main
方法。我添加了对 SwingUtilities
invokeLater
方法的调用。此方法确保在 Event Dispatch Thread.
我重新排序了 JFrame
方法调用。 JFrame
方法必须按特定顺序调用。这是我用于所有 Swing 应用程序的顺序。
您没有调整 JFrame
的大小。您调整绘图的尺寸 JPanel
。 JFrame
包含占用一些 space 的装饰。
我留下了你费解的绘图代码。您将很难扩展此代码以实际创建游戏。
这是完整的运行可用代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GameInterface {
private JFrame jframe;
private Renderer renderer;
private RepaintConfiguration repaintConfiguration;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameInterface();
}
});
}
public GameInterface() {
this.repaintConfiguration = new RepaintConfiguration(this);
jframe = new JFrame();
jframe.setTitle("Jetpack");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
this.renderer = new Renderer(repaintConfiguration);
jframe.add(renderer, BorderLayout.CENTER);
jframe.pack();
jframe.setLocationByPlatform(true);
jframe.setVisible(true);
}
public Renderer getRenderer() {
return renderer;
}
public class Renderer extends JPanel {
private static final long serialVersionUID = 1L;
private RepaintConfiguration repaintConfiguration;
public Renderer(RepaintConfiguration repaintConfiguration) {
this.repaintConfiguration = repaintConfiguration;
this.setPreferredSize(new Dimension(1500, 800));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaintConfiguration.repaint(g);
}
}
public class RepaintConfiguration implements ActionListener {
private GameInterface gameInterface;
public RepaintConfiguration(GameInterface gameInterface) {
this.gameInterface = gameInterface;
}
@Override
public void actionPerformed(ActionEvent event) {
gameInterface.getRenderer().repaint();
}
public void repaint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, 1500, 800);
}
}
}