Skip to content

Commit b70b00c

Browse files
hansonrhansonr
authored andcommitted
adds JSComponent.isWindowOrJSApplet(), selfOrChildIsPainted()
1 parent 615e860 commit b70b00c

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public static void ensurePropertyChangeListener(Component c, Component listener)
105105
}
106106
}
107107

108+
/**
109+
* Note that the length of this array may be longer than getComponentCount()
110+
* @param c
111+
* @return
112+
*/
108113
public static Component[] getChildArray(Container c) {
109114
return (c == null ? Container.EMPTY_ARRAY : c.getChildArray());
110115
}
@@ -137,6 +142,18 @@ public static Component[] getChildArray(Container c) {
137142
*
138143
*/
139144
public boolean _isBackgroundPainted;
145+
protected boolean _alwaysPaint;
146+
147+
public boolean selfOrChildIsPainted() {
148+
if (_alwaysPaint || _isBackgroundPainted)
149+
return true;
150+
Component[] a = JSComponent.getChildArray((Container) this);
151+
for (int i = ((Container) this).getComponentCount(); --i >= 0;)
152+
if (((JSComponent) a[i]).selfOrChildIsPainted())
153+
return true;
154+
return false;
155+
}
156+
140157

141158
private Insets tempInsets;
142159
public JSGraphics2D _gtemp; // indicates that we are painting, so that g.setBackground() should also be set
@@ -282,7 +299,7 @@ public void checkBackgroundPainted(JSGraphics2D jsg, boolean init) {
282299
return;
283300
}
284301
_gtemp = null;
285-
_isBackgroundPainted = jsg.isBackgroundPainted();
302+
_isBackgroundPainted = _alwaysPaint || jsg.isBackgroundPainted();
286303
if (_isBackgroundPainted) {
287304
((JSComponentUI) ui).setPainted(jsg);
288305
// It's all one canvas, and it is behind the root pane (bad design?)
@@ -434,9 +451,7 @@ public void removeKeyListener(KeyListener l) {
434451
*/
435452
public static Container getTopInvokableAncestor(Component c, boolean andFocusable) {
436453
for(Component p = c; p != null; p = nextHigher(p)) {
437-
if (p instanceof Window && (!andFocusable || ((Window)p).isFocusableWindow())
438-
|| p instanceof JSApplet
439-
) {
454+
if (p.isWindowOrJSApplet() && (!andFocusable || ((Window)p).isFocusableWindow())) {
440455
return (Container) p;
441456
}
442457
}

sources/net.sf.j2s.java.core/src/swingjs/a2s/Canvas.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,53 @@
22

33
import java.awt.Color;
44
import java.awt.Graphics;
5+
import java.awt.Graphics2D;
56
import java.awt.GraphicsConfiguration;
67

7-
88
public class Canvas extends Panel {
9-
10-
public void isAWT() {}
11-
12-
public Canvas() {
13-
super();
14-
}
15-
9+
1610
public Canvas(GraphicsConfiguration config) {
11+
this();
12+
}
13+
14+
public Canvas() {
1715
super();
16+
_alwaysPaint = true;
1817
}
19-
18+
2019
@Override
2120
public void setBackground(Color c) {
2221
super.setBackground(c);
2322
setOpaque(c != null);
2423
}
2524

26-
2725
@Override
2826
public void paint(Graphics g) {
29-
g.clearRect(0, 0, width, height);
30-
31-
// see http://www.oracle.com/technetwork/java/painting-140037.html#awt_summary
32-
33-
//BH AWT called canvas.update(g), but Swing will call canvas.paint(g) instead.
34-
//BH a2s does allow for that, with paint(g) calling update(g) (Opposite of standard Swing).
35-
36-
//BH So in the code, canvas.paint should be renamed something like canvas.paintMe
37-
38-
27+
((Graphics2D) g).setBackground(getBackground());
28+
g.clearRect(0, 0, width, height);
29+
30+
// see http://www.oracle.com/technetwork/java/painting-140037.html#awt_summary
31+
32+
// BH AWT called canvas.update(g), but Swing will call canvas.paint(g) instead.
33+
// BH a2s does allow for that, with paint(g) calling update(g) (Opposite of
34+
// standard Swing).
35+
36+
// BH So in the code, canvas.paint should be renamed something like
37+
// canvas.paintMe
38+
3939
update(g);
4040
}
4141

42-
private boolean notified;
42+
// private boolean notified;
4343
@SuppressWarnings("unused")
4444
@Override
4545
public void update(Graphics g) {
46-
if (!notified)
47-
System.out.println("neither paint(g) nor update(g) is implemented for " + this);
48-
notified = true;
49-
if (/** @j2sNative this.paintComponent$java_awt_Graphics || */false)
46+
// if (!notified && JSUtil.is)
47+
// System.out.println("neither paint(g) nor update(g) is implemented for " + this);
48+
// notified = true;
49+
if (/** @j2sNative this.paintComponent$java_awt_Graphics || */
50+
false)
5051
paintComponent(g);
5152
}
5253

53-
5454
}

0 commit comments

Comments
 (0)