pred
pred

Reputation: 185

Draw Class which extends JLabel

I'm trying to draw my "Zone" class which extends JLabel. I don't understand why this is not working, I searched on the website, but I didn't see what is going wrong

Here's my code :

My Board class

public class Board extends JPanel {

private List<Zone> zones = new ArrayList<Zone>();

public Board() {
    zones.add(new Zone(1, false, true, dalle1C, null, "/zone1D1C.jpg", 0, 0, this));
    zones.add(new Zone(2, false, false, dalle1C, null, "/zone2D1C.jpg", 150, 0, this));
    zones.add(new Zone(3, false, false, dalle1C, null, "/zone3D1C.jpg", 300, 0, this));
    zones.add(new Zone(4, true, false, dalle1C, null, "/zone4D1C.jpg", 0, 150, this));
    zones.add(new Zone(5, false, false, dalle1C, null, "/zone5D1C.jpg", 300, 150, this));
    zones.add(new Zone(6, true, false, dalle1C, null, "/zone6D1C.jpg", 0, 300, this));
    zones.add(new Zone(7, true, false, dalle1C, null, "/zone7D1C.jpg", 150, 300, this));
    zones.add(new Zone(8, false, false, dalle1C, null, "/zone8D1C.jpg", 300, 300, this));
    zones.get(1).addConnexion(connexion);
    connexion = new PassageGD(zones.get(1), zones.get(2), false, false);
    zones.get(1).addConnexion(connexion);
    connexion = new PassageHB(zones.get(2), zones.get(4), false, false);
    zones.get(4).addConnexion(connexion);
    connexion = new PassageGD(zones.get(3), zones.get(4), false, false);
    zones.get(4).addConnexion(connexion);
    connexion = new PassageHB(zones.get(4), zones.get(7), false, false);
    zones.get(4).addConnexion(connexion);
    connexion = new PassageHB(zones.get(3), zones.get(6), false, false);
    zones.get(6).addConnexion(connexion);
    connexion = new PassageGD(zones.get(5), zones.get(6), false, false);
    zones.get(6).addConnexion(connexion);
    connexion = new PassageHB(zones.get(3), zones.get(5), false, false);
    zones.get(5).addConnexion(connexion);
    }
}

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    setBackground(Color.BLACK);
    for (Zone zone : zones) {
        this.add(zone);
    }
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

And my Zone class :

public class Zone extends JLabel implements ActionListener {
...
...
public Zone(Integer id, boolean piece, boolean egout, Dalle[] dalles, List<Connexion> connexions,    String image_name, Integer x, Integer y, Board board) {
    zone = this;
    addMouseListener(new TAdapter());
    this.board = board;
    if(connexions != null) {
        this.connexions = connexions;
        for(Connexion connexion : connexions) {
            connexion.getOtherZone(this).addConnexion(connexion);
        }
    }
    ImageIcon ii = new ImageIcon(this.getClass().getResource(image_name));
    this.x = x;
    this.y = y;
    this.x_end = x + image.getWidth(null);
    this.y_end = y + image.getHeight(null);
    this.setBorder(null);
    this.setIcon(ii);
    this.setText(null);
    this.setVisible(true);
}

Upvotes: 0

Views: 84

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

  1. Don't override paint, you've broken the paint chain which will affect the ability of the panel to paint it's children. Override paintComponent AND call super.paintComponent before performing any custom painting
  2. Don't dispose of a Graphics context that you didn't create, this could prevent other components from been painted.
  3. Don't add components within the paint methods, these methods can be called within quick succession and randomly.

Instead, add the components within the constructor

Upvotes: 3

Related Questions