简体   繁体   English

Graphics2D DrawImage不代表图像大小

[英]Graphics2D DrawImage not representing image size

I am currently working on a project to make Connect 4 and I'm running into some problems with the coordinate system for it. 我目前正在研究制作Connect 4的项目,并且在使用它的坐标系时遇到了一些问题。 I have an 800x600px png image which shows the 'board' and it is made up of an 8x6 grid with each location being a 100x100px square...at least that's how it is in GIMP2 and in the filesystem. 我有一个800x600px png图像,显示了'board',它是由8x6网格组成的,每个位置都是100x100px正方形...至少在GIMP2和文件系统中是这样的。 However when I use Graphics2D's drawImage() method to paint the image onto a 800x600 JFrame it sticks off the bottom. 但是,当我使用Graphics2D的drawImage()方法将图像绘制到800x600 JFrame时,它会粘在底部。 Do I need to account for the JFrame's borders and stuff in this system, or am I missing something else? 我是否需要考虑该系统中JFrame的边框和其他内容,还是我遗漏了其他内容?

Thanks! 谢谢!

PS Here is the JPanel class that does the drawing. PS这是执行绘图的JPanel类。

public class GameBoard extends JPanel { 
public GameBoard() {
}

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    //Write messages
    if (!(GameManager.getInstance().getMessage() == null)) {
        g2d.drawString(GameManager.getInstance().getMessage(), 400, 25);
    }


    //Draw board graphic        
    ImageIcon bg = new ImageIcon("res/board.png");
    g2d.drawImage(bg.getImage(), 0, 0, 800, 600, this);

    //Draw counters


    for (int columns = 0; columns < 8; columns++) {
        for (int rows = 0; rows < 6; rows++) {
            if (!(GameManager.getInstance().getState(columns, rows) == null)) {
                Counter cTemp = GameManager.getInstance().getState(columns, rows);
                g2d.drawImage(cTemp.getImage(), cTemp.getX(), cTemp.getY(), 100, 100, this);
            }                   
        }
    }       
}

} }

  1. Override paintComponent instead of paint and call super.paintComponent(g) 覆盖paintComponent而不是paint并调用super.paintComponent(g)

     @Override protected void paintComponent(Graphics g) { super.paintComponent(g); ... } 
  2. Override getPreferredSize() for your JPanel 为您的JPanel覆盖getPreferredSize()

     @Override public Dimension getPreferredSize() { return new Dimension(800, 600); } 
  3. Don't create your image inside the paintComponent method. 不要paintComponent方法内创建图像。 Do it in the constructor. 在构造函数中执行。

     public class GameBoard extends JPanel { ImageIcon img; public GameBoard() { img = new ImageIcon(...); } 
  4. pack() your frame, don't setSize() , and the preferred size of the JPanel will be respected (by your override getPreferredSize() ). pack()框架, 不要设置 setSize() ,并且将遵守JPanel的首选大小(通过重写getPreferredSize() )。

  5. You can just use getWidth() and getHeight() after override getPreferredSize() 您可以在覆盖getPreferredSize()之后只使用getWidth()getHeight() getPreferredSize()

     drawImage(bg.getImage(), 0, 0, getWidth(), getHeight(), this); 

Work on all these things, if it doesn't help, post a runnable example we can test out. 处理所有这些问题,如果没有帮助,请发布一个可运行的示例,我们可以进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM