PaintComponent 不在 JPanel 上绘制 BufferedImage 网格
我为 TicTacToe 游戏创建了一个 600 x 600 网格,但每次我尝试获取图像(使用 getImg()
方法)时,它都会显示白屏。 JPanel的背景是暗灰色的,它既不绘制网格,也不显示JPanel背景。
这是代码,我没有尝试过任何东西
PanelManager.java
package TicTacToe.display.panel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class PanelManager extends JPanel implements Runnable{
Thread t;
BufferedImage grid;
public PanelManager() {
this.setPreferredSize(new Dimension(600,600));
this.setBackground(Color.darkGray);
this.setDoubleBuffered(true);
this.startThread();
}
public void getImg() {
try {
grid = ImageIO.read(getClass().getResourceAsStream("/TicTacToe/grid/grid.png"));
}
catch (IOException e) {
e.printStackTrace();
}
}
public void startThread() {
t = new Thread(this);
t.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(grid,300,300,600,600,null);
g2.dispose();
}
public void update() {
}
public void run() {
while(t != null) {
update();
try {
Thread.sleep(10);
}
catch (Exception e) {}
repaint();
}
}
}
FrameManager.java
package TicTacToe.display;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import TicTacToe.display.panel.PanelManager;
public class FrameManager {
public static void main(String[] args) {
JFrame f = new JFrame();
PanelManager pm = new PanelManager();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setTitle("TicTacToe Windowed Edition");
f.setResizable(false);
f.setVisible(true);
f.setSize(600,600);
f.setLocationRelativeTo(null);
f.add(pm);
}
}
I created a 600 x 600 grid for a TicTacToe game, but every time I try to get the image(Using the getImg()
method), it shows a white screen. The JPanel's background is darkGray, it neither draws the grid, nor does it show the JPanel background.
This is the code, I haven't tried anything
PanelManager.java
package TicTacToe.display.panel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class PanelManager extends JPanel implements Runnable{
Thread t;
BufferedImage grid;
public PanelManager() {
this.setPreferredSize(new Dimension(600,600));
this.setBackground(Color.darkGray);
this.setDoubleBuffered(true);
this.startThread();
}
public void getImg() {
try {
grid = ImageIO.read(getClass().getResourceAsStream("/TicTacToe/grid/grid.png"));
}
catch (IOException e) {
e.printStackTrace();
}
}
public void startThread() {
t = new Thread(this);
t.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(grid,300,300,600,600,null);
g2.dispose();
}
public void update() {
}
public void run() {
while(t != null) {
update();
try {
Thread.sleep(10);
}
catch (Exception e) {}
repaint();
}
}
}
FrameManager.java
package TicTacToe.display;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import TicTacToe.display.panel.PanelManager;
public class FrameManager {
public static void main(String[] args) {
JFrame f = new JFrame();
PanelManager pm = new PanelManager();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setTitle("TicTacToe Windowed Edition");
f.setResizable(false);
f.setVisible(true);
f.setSize(600,600);
f.setLocationRelativeTo(null);
f.add(pm);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读了一下代码后,我意识到我没有使用
getImg();
方法,我所要做的就是在PanelManager()
中添加一行代码代码>构造函数如下所示:-After a bit of reading the code, I realized I didn't use the
getImg();
method, all I had to do was add one line of code in thePanelManager()
constructor as shown below :-