我一直在研究一个将自定义JComponents绘制到JLayeredPane上的程序,但是对组件上的repaint()的所有调用似乎什么也没做,但是当重新调整窗口时,会自动调用paintComponent方法。
我一直在遵循这里给出的一些建议: Why is paint()/paintComponent() never called?
但是没有解决方案似乎解决了我的问题,在EDT上更新swing组件,在调用repaint()之前手动设置组件大小,在重写的paintComponent()中调用super.paintComponent(g)并调用revalidate()on添加新组件后的框架(虽然在这种情况下显然不是问题)
任何可能阻止通话的想法?在此先感谢:)
以下是View和SVGElementContainer的代码,view.setFile()是在需要显示新文档时调用的入口点。
public class View extends JLayeredPane implements SVGViewport {
private SVGDocument document;
//Array list of the SVGElementContainer components
private ArrayList<SVGElementContainer> elemContainers;
private SVGFrame frame;
private int elemCount;
private Border viewBorder;
private int borderWidth = 1;
//panels displayed on the JLayeredPane
private JPanel backgroundPanel;
/** Creates a new view */
public View(SVGFrame frame) {
super();
this.frame = frame;
elemCount = 0;
elemContainers = new ArrayList<SVGElementContainer>();
viewBorder = BorderFactory.createLineBorder(Color.BLACK, borderWidth);
}
public float getViewportWidth() {
return getWidth();
}
public float getViewportHeight() {
return getHeight();
}
// paints all elements and adds them to the JLayeredPane
public void paintAllElements(){
System.out.println("Painting all elements");
// Paint document
for (SVGElement elem : document) {
//only paint stylable (rect, line, circle) elements
if (elem instanceof SVGStylable){
//create a new SVGElementContainer
SVGElementContainer newElemCont = new SVGElementContainer();
//add component to JLayeredPane
elemCount++;
this.add(newElemCont, new Integer(elemCount + 1));
//set the current element within its container and calls repaint() on the component
System.out.println("Painting element #" + elemCount);
newElemCont.setElement(elem);
newElemCont.repaint();
}
else {
System.out.println("Skip painting group element!");
}
}
}
/** Gets the document currently being displayed by the view. */
public SVGDocument getDocument() {
return document;
}
/** Sets the document that the view should display.
*
* @param document the document to set
*/
public void setDocument(SVGDocument document) {
this.document = document;
//paintBackground();
paintAllElements();
revalidate();
}
public void revalidate(){
//calls validate() on the frame in order to display newly added components
frame.getContentPane().validate();
}
}
public class SVGElementContainer extends JPanel{
private SVGElement elem;
public SVGElementContainer(){
super();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("PAINT METHOD CALLED!");
paint2D((Graphics2D) g);
}
//paint the element onto this JComponent
public void paint2D(Graphics2D g){
if (!(elem instanceof SVGStylable)){
System.out.println("Skipping non-stylable element!");
return;
}
setOpaque(false);
Shape shape = elem.createShape();
// get fill stroke and width properties
SVGStylable style = (SVGStylable) elem;
SVGPaint fillPaint = style.getFill();
SVGPaint strokePaint = style.getStroke();
SVGLength strokeWidth = style.getStrokeWidth();
// Fill the interior of the shape
if (fillPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
g.setPaint(fillPaint.getRGBColor());
g.fill(shape);
}
// Stroke the outline of the shape
if (strokePaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
Stroke stroke = new BasicStroke(strokeWidth.getValue());
g.setStroke(stroke);
g.setColor(strokePaint.getRGBColor());
g.draw(shape);
}
}
public void setElement(SVGElement elem){
this.elem = elem;
setComponentSize();
}
private void setComponentSize(){
//this.setPreferredSize(new Dimension(
// (int)elem.getDocument().getWidth().getValue(),
// (int)elem.getDocument().getHeight().getValue()));
this.setSize(new Dimension(
(int)elem.getDocument().getWidth().getValue(),
(int)elem.getDocument().getHeight().getValue()));
}
}
答案 0 :(得分:2)
我看到你正在调用setOpaque(false)。来自setOpaque javadoc,强调我的:
如果为true,则组件绘制其边界内的每个像素。否则,组件可能无法绘制部分或全部像素,从而允许底层像素显示。
“可能”是在repaint()调用期间第一次没有调用paintComponent()的原因。 Swing可以决定组件没有“改变”,因此不需要重新绘制。
答案 1 :(得分:2)
在调用repaint()之前手动设置组件大小,在重写的paintComponent()中调用super.paintComponent(g)并在添加新组件后在框架上调用revalidate()
您的代码在这些概念上是错误的。
a)从不调用setSize()方法。这是布局管理器的工作。您应该通过覆盖getPreferredSize()等方法来为布局管理器提供提示,以返回组件的首选大小
b)不要覆盖revalidate()方法。该技巧的要点是使用如下代码:
panel.add( .... );
panel.revalidate();
panel.repaint();
但我真的不知道你的所有代码应该做什么,所以我无法确定你的代码是否有意义。我也觉得你扩展JLayeredPane很奇怪。
答案 2 :(得分:1)
我可以看到扩展JPanel
以获得缓冲和UI委托,但不透明度依赖于L&amp; F.相反,您应该从JComponent
开始,并为您的(假设的)SVGEvent
实施EventListenerList
管道。