我有問題測試我重寫的繪畫組件。junit測試重寫繪畫組件
我已刪除了大量的代碼來簡化東西
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.RED);
int y = //some come;
int height = //some code
//for loop
g.clearRect(1, y, getWidth(), height);
g.drawRect(1, y, getWidth(), height);
}
}
super.paintComponent(g);
}
我的塗料成份所創建多個矩形。我需要詳細瞭解繪製的矩形數量和高度。我不知道如何做一個單元測試來做到這一點。
我試過使用TextAreas'getComponents()
方法,但它返回null。我認爲調用repaint()會觸發paint組件執行。
感謝所有幫助
上面的一個很好。感謝那。我試圖在yourComponent.paintComponent(g2)之後調用getComponents()方法; 它說沒有組件,即使我做g.drawRect(1,y,getWidth(),height); – Decrypter
'getComponents()'方法返回* child *組件。所以,如果你有一個'JPanel p'並且用'p.add(l)'添加'JLabel l',那麼'p.getComponents()'將返回'l'。但是,重寫'paintComponent'不會添加任何子組件。 –