简体   繁体   English

扩展JLabel的Draw类

[英]Draw Class which extends JLabel

I'm trying to draw my "Zone" class which extends JLabel. 我正在尝试绘制扩展JLabel的“ Zone”类。 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);
}
  1. Don't override paint , you've broken the paint chain which will affect the ability of the panel to paint it's children. 不要覆盖paint ,您已经破坏了绘画链,这将影响面板绘画其子代的能力。 Override paintComponent AND call super.paintComponent before performing any custom painting 覆盖paintComponent super.paintComponent在执行任何自定义绘制之前调用super.paintComponent
  2. Don't dispose of a Graphics context that you didn't create, this could prevent other components from been painted. 不要处理未创建的Graphics上下文,这可能会导致其他组件无法绘制。
  3. Don't add components within the paint methods, these methods can be called within quick succession and randomly. 不要在paint方法中添加组件,可以快速连续地随机调用这些方法。

Instead, add the components within the constructor 而是在构造函数中添加组件

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

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