PaintComponent 不在 JPanel 上绘制 BufferedImage 网格

发布于 2025-01-16 23:48:14 字数 2365 浏览 5 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2025-01-23 23:48:14

读了一下代码后,我意识到我没有使用 getImg(); 方法,我所要做的就是在 PanelManager() 中添加一行代码代码>构造函数如下所示:-

public PanelManager() {
        
        this.setPreferredSize(new Dimension(600,600));
        this.setBackground(Color.darkGray);
        this.setDoubleBuffered(true);
        
        this.getImg();
        this.startThread();
}

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 the PanelManager() constructor as shown below :-

public PanelManager() {
        
        this.setPreferredSize(new Dimension(600,600));
        this.setBackground(Color.darkGray);
        this.setDoubleBuffered(true);
        
        this.getImg();
        this.startThread();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文