Skip to content

Commit 269ff25

Browse files
hansonrhansonr
authored andcommitted
allow Label/JLabel and other components to paint independently
as long as they do not override paint(Graphics) in JComponent. Otherwise any change to a label triggers a full repaint of their containing frame.
1 parent 4126f6a commit 269ff25

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

sources/net.sf.j2s.java.core/src/java/awt/JSComponent.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import swingjs.JSAppletViewer;
4646
import swingjs.JSFrameViewer;
4747
import swingjs.JSGraphics2D;
48+
import swingjs.JSUtil;
4849
import swingjs.api.js.HTML5Canvas;
4950
import swingjs.plaf.JSComponentUI;
5051

@@ -524,6 +525,38 @@ public void removeKeyListener(KeyListener l) {
524525
return 秘isFocusableSet && isFocusable();
525526
}
526527

528+
/**
529+
* will be set to 1 if paint(graphics) is found to be overridden, signally that
530+
* we can't depend upon this component to be drawn by itself; JLabel will
531+
* also set this to 1 if there there is an icon
532+
*
533+
*/
534+
protected int 秘paintOverridden = 0;
535+
536+
/**
537+
* A component in SwingJS can paint immediately if it is opaque (as in Java) or
538+
* if its paint(Graphics) method is not overridden. Note that AWT Applet and Panel
539+
* do override paint(graphics)
540+
*
541+
* @return
542+
*/
543+
public boolean canPaintImmediately() {
544+
if (isOpaque())
545+
return true;
546+
if (秘paintOverridden == 0) {
547+
if (((Container) this).getComponentCount() != 0) {
548+
秘paintOverridden = -1;
549+
} else {
550+
Object f = JSUtil.getJ2SAlias(this, "paint$java_awt_Graphics");
551+
秘paintOverridden = (JSUtil.isOverridden(f, JComponent.class) ? 1 : -1);
552+
}
553+
}
554+
// TODO -- still need to set RepaintManager so that
555+
// objects with the same paintable root can be grouped together.
556+
557+
return (秘paintOverridden == -1);
558+
}
559+
527560

528561

529562
}

sources/net.sf.j2s.java.core/src/javax/swing/JComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4347,7 +4347,7 @@ public void paintImmediately(int x, int y, int w, int h) {
43474347
return;
43484348
}
43494349

4350-
while (!((JComponent) c).isOpaque()) {
4350+
while (!((JSComponent)c).canPaintImmediately()) {
43514351
parent = c.getParent();
43524352
if (parent != null) {
43534353
x += c.getX();

sources/net.sf.j2s.java.core/src/javax/swing/JLabel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ public Icon getIcon() {
330330
public void setIcon(Icon icon) {
331331
Icon oldValue = defaultIcon;
332332
defaultIcon = icon;
333+
if (defaultIcon != null)
334+
秘paintOverridden = 1;
333335

334336
/* If the default icon has really changed and we had
335337
* generated the disabled icon for this component

0 commit comments

Comments
 (0)