0

I started a Chess Project reusing some older code to paint a map everything basically has been copy pasted. The problem is the squares dont show up? I tried fixing it for a while now and got to no solution. Here are probably the three most important methods and a zip with the whole project. Some of it is in German.

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();
    }

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    `setBounds()` is always a problem in Swing code. Don't use it, use a layout manager. – markspace Dec 06 '21 at 16:04
  • No need for custom painting. Just set the background when you create the component. Check out: https://stackoverflow.com/questions/6811247/drawing-in-jlayeredpane-over-exising-jpanels/6811800#6811800 for a basic example showing this approach as well has simple logic for dragging components around the board. – camickr Dec 06 '21 at 17:56

1 Answers1

1

Do not call repaint() in paintComponent. Otherwise you will never exit the EDT and lock up your code. And the first statement in paintComponent(Graphics g) should be super.paintComponent(g);

Updated to include an example

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();
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • px and py dont change and i dont use them to paint the squares i use the boundaries of the JComponent which are set in createMap. And i was also weirded out by using repaint in the paintComponent method but my mate said thats how it should be and in our old project it worked fine. – warriorloewe Dec 06 '21 at 16:10
  • Using `setBounds()` do do this is probably not a good way to go. But your main problem was calling `repaint()` in the `paintComponent` method. Also, I didn't see where `px` and `py` were initialized. – WJS Dec 06 '21 at 16:12
  • If calling repaint() in your old project worked then you were lucky. Perhaps it was called within a conditional. But otherwise, it will keep requesting painting operations in the EDT which will be analogous to an infinite loop. Trust me when I say don't do it. It is okay to call repaint from the EDT (like a listener) but not from paintComponent(). – WJS Dec 06 '21 at 16:20
  • the paintComponent method only draws one square not all 64 so it doesnt need and arraylist of points, the square basically draws itslef within its boundaries it has two attributes position y and postition x also ive tried not doing repaint in paintComponent but that also didnt work – warriorloewe Dec 06 '21 at 16:24
  • I just used that as an example in case that is what you were doing. Perhaps if you could post a [mre] I could provide more helpful information. Otherwise I will be guessing (and probably incorrectly). – WJS Dec 06 '21 at 16:26
  • its the zip it has the whole project just drop it into eclipse – warriorloewe Dec 06 '21 at 16:30
  • 1
    This site frowns on posting links to code and I rarely download it. I modified my answer (as a [mre]) to provide some assistance. What I said earlier still stands. But if you created your own component then you should let the main program invoke repaint.() This will trickle down to other components in your frame/panels. Also, remember that the location is from the parents perspective. But the component paints from its own perspective. – WJS Dec 06 '21 at 17:01
  • Alright will try that thanks anyways – warriorloewe Dec 07 '21 at 18:19