【问题标题】:Why arent the squares being painted?为什么不画正方形?
【发布时间】:2022-01-11 19:46:46
【问题描述】:

我开始了一个国际象棋项目,重用一些旧代码来绘制地图,基本上所有内容都已复制粘贴。问题是方块不显示?我现在尝试修复它一段时间,但没有解决方案。这可能是三个最重要的方法和整个项目的 zip。其中一些是德语。

https://drive.google.com/file/d/1nnZHLB0Ycy04eMyYbEmduMwbGhVLZ2VB/view?usp=sharing

public SchachFrame() {
        super();
        contentPane = new JPanel();
        setContentPane(contentPane);
        setBounds(0, 0, window.width, window.height);
        contentPane.setBackground(Color.darkGray);
        contentPane.setVisible(true);
        
        ge = new GameEnvironment(this);
        ge.setBounds(window.width/2 - 500, window.height/2 - 500, 1000, 1000);
        ge.setVisible(true);
        contentPane.add(ge);
        
        Thread gameEnvironment = new Thread(ge);
        gameEnvironment.start();
        
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    } 


public void createMap(int width, int length, GameEnvironment ge) {
        map = new Spielfeld[length][width];
        Spielfeld.width = 1000/width;
        Spielfeld.height = 1000/length;
        for(int i = 0; i < length; i++) {
            for(int j = 0; j < width; j++) {
                this.map[i][j] = new Spielfeld(j, i, null);
                this.map[i][j].setBounds(ge.getX() + j * Spielfeld.width, ge.getY() + i * Spielfeld.height, Spielfeld.width, Spielfeld.height);
                this.map[i][j].setVisible(true);
                ge.add(this.map[i][j]);
            }
        }
    }


public void paintComponent(Graphics g) {
        if((this.px + this.py) % 2 == 0) {
            g.setColor(Color.blue);
        } else {
            g.setColor(Color.cyan);
        }
        g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
        repaint();
    }

【问题讨论】:

  • setBounds() 在 Swing 代码中始终是一个问题。不要使用它,使用布局管理器。
  • 无需自定义绘画。只需在创建组件时设置背景。查看:stackoverflow.com/questions/6811247/… 的基本示例,展示了这种方法以及在板上拖动组件的简单逻辑。

标签: java swing jframe chess jcomponent


【解决方案1】:

不要在paintComponent 中调用repaint()。否则,您将永远不会退出 EDT 并锁定您的代码。而paintComponent(Graphics g)中的第一条语句应该是super.paintComponent(g);

更新以包含一个示例

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawSquareComponent extends JPanel {
    static int WIDTH = 600;
    static int HEIGHT = 600;
    JFrame frame = new JFrame();
    
    public static void main(String[] args) {
        new DrawSquareComponent().start();
    }
    
    public void start() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        setBackground(Color.white);
        MyComponent my =new MyComponent();
        add(my); // add my component to panel
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
            int size = r.nextInt(50)+50;
            int x = r.nextInt(WIDTH-size)+1;
            int y = r.nextInt(HEIGHT-size)+1;
            my.setBounds(x,y,size,size);
            frame.repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            }
        }
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(WIDTH, HEIGHT);
    }
}

class MyComponent extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics gg = g.create();
        super.paintComponent(gg);
        // location relative to parent's.
        // used to check location in parent's coordinate space.
        Point p = getLocation();
        if ((p.x + p.y) % 2 == 0) {
            gg.setColor(Color.blue);
        } else {
            gg.setColor(Color.cyan);
        }
        // paint this component so location is always 0,0.
        // all you're doing is painting this squares background
        gg.fillRect(0,0, this.getWidth(), this.getHeight());
        gg.dispose();
    }
}

【讨论】:

  • px 和 py 不会改变,我不使用它们来绘制正方形,我使用在 createMap 中设置的 JComponent 的边界。我也很奇怪在paintComponent方法中使用repaint,但我的伙伴说应该是这样,在我们的旧项目中它工作得很好。
  • 使用setBounds() 这样做可能不是一个好方法。但是您的主要问题是在paintComponent 方法中调用repaint()。另外,我没有看到 pxpy 的初始化位置。
  • 如果在您的旧项目中调用 repaint() 有效,那么您很幸运。也许它是在条件内调用的。但除此之外,它将继续在 EDT 中请求绘制操作,这类似于无限循环。当我说不要这样做时,请相信我。可以从 EDT(如侦听器)调用 repaint,但不能从 paintComponent() 调用。
  • paintComponent 方法只绘制一个正方形而不是全部 64,因此它不需要点数组列表,正方形基本上在其边界内绘制它的边界它有两个属性位置 y 和位置 x 也我试过不做重绘在paintComponent中,但这也不起作用
  • 这个网站不赞成发布代码链接,我很少下载它。我修改了我的答案(作为minimal reproducible example)以提供一些帮助。我之前说的仍然有效。但是,如果您创建了自己的组件,那么您应该让主程序调用 repaint.() 这将渗透到您的框架/面板中的其他组件。另外,请记住,该位置是从父母的角度来看的。但是组件是从自己的角度进行绘制的。
猜你喜欢
  • 2016-06-21
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2010-10-13
  • 1970-01-01
  • 2019-01-29
相关资源
最近更新 更多