Skip to content

Commit 47b569f

Browse files
hansonrhansonr
authored andcommitted
Refactored handling of background now based on uses of getGraphics and
overriding of Paint, PaintComponent, PaintContainer
1 parent f83f014 commit 47b569f

File tree

23 files changed

+407
-309
lines changed

23 files changed

+407
-309
lines changed
-2.82 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190613215044
1+
20190614172837
-2.82 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190613215044
1+
20190614172837
-2.82 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/applet/JSApplet.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
import javax.swing.JRootPane;
4444
import javax.swing.RepaintManager;
4545

46-
import swingjs.plaf.JSComponentUI;
47-
4846
/**
4947
* SwingJS note: This class is the original java.applet.Applet class.
5048
* It is subclassed by JApplet. The replacement java.applet.Applet class
@@ -289,8 +287,8 @@ public void resizeHTML(int width, int height) {
289287
// Added 2/23/2019 to force layout prior to Canvas painting in mpFrakta.Applets.Geomet
290288
JRootPane root = ((JApplet) this).getRootPane();
291289
root.invalidate();
292-
((JSComponentUI)root.getUI()).setPainted(null);
293-
root.秘isBackgroundPainted = false;
290+
// ((JSComponentUI)root.getUI()).setPainted(null);
291+
// root.秘isBackgroundPainted = false;
294292
RepaintManager.currentManager(this).addInvalidComponent(root);
295293
}
296294
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,9 @@ protected final void invalidateIfValid() {
26972697

26982698
/**
26992699
*
2700-
* For SwingJS, we have the graphics without needing to get it from a peer.
2700+
* Unused in SwingJS. For SwingJS, we have the graphics without needing to get it from a peer.
2701+
* @see JSComponent.getGraphics
2702+
*
27012703
* Creates a graphics context for this component. This method will return
27022704
* <code>null</code> if this component is currently not displayable.
27032705
*
@@ -8308,4 +8310,5 @@ static boolean doesImplement(Object obj, String interfaceName) {
83088310
return doesClassImplement(obj.getClass(), interfaceName);
83098311
}
83108312

8313+
83118314
}

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

Lines changed: 125 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import javax.swing.RootPaneContainer;
3838
import javax.swing.UIDefaults;
3939
import javax.swing.UIManager;
40+
import javax.swing.border.AbstractBorder;
41+
import javax.swing.border.Border;
4042
import javax.swing.plaf.ComponentUI;
4143
import javax.swing.plaf.FontUIResource;
4244
import javax.swing.plaf.UIResource;
@@ -93,7 +95,46 @@ public interface A2SWrappedComponent {
9395
public Component 秘getWrap();
9496

9597
}
96-
/**
98+
99+
protected boolean 秘isAppletFrame;
100+
public boolean 秘isFramedApplet;
101+
102+
public String 秘htmlName;
103+
104+
protected int 秘num;
105+
private static int 秘incr;
106+
private Insets 秘tempInsets;
107+
public JSGraphics2D 秘gtemp; // indicates that we are painting, so that g.setBackground() should also be set
108+
109+
public boolean 秘isRootPane, 秘isContentPane;
110+
public JSAppletViewer 秘appletViewer = ((JSAppletThread) Thread.currentThread()).秘appletViewer;
111+
private JSFrameViewer 秘frameViewer, 秘topFrameViewer;
112+
public HTML5Canvas 秘canvas;
113+
public ComponentUI ui; // from JComponent
114+
115+
private String 秘uiClassID;
116+
117+
Boolean 秘peerVis;
118+
119+
protected Border 秘border; // from private JComponent field
120+
121+
public final static int PAINTS_SELF_NO = -1;
122+
public final static int PAINTS_SELF_YES = 1;
123+
public final static int PAINTS_SELF_UNKNOWN = 0;
124+
125+
/**
126+
* will be set to 1 if paint(graphics) is found to be overridden, signally that
127+
* we can't depend upon this component to be drawn by itself; JLabel will
128+
* also set this to 1 if there there is an icon
129+
*
130+
*/
131+
private int 秘iPaintMyself = PAINTS_SELF_UNKNOWN;
132+
133+
// trying to replace these two:
134+
// public boolean 秘isBackgroundPainted;
135+
// protected boolean 秘alwaysPaint; // in AWT canvas
136+
137+
/**
97138
* overridden in Applet
98139
*
99140
* SwingJS Applet repurposes resize(width, height) to call
@@ -105,9 +146,7 @@ public interface A2SWrappedComponent {
105146
* @param width
106147
* @param height
107148
*/
108-
@SuppressWarnings("deprecation")
109149
public void 秘resizeOriginal(int width, int height) {
110-
// o
111150
resize(width, height);
112151
}
113152

@@ -145,45 +184,6 @@ public interface A2SWrappedComponent {
145184
*
146185
*/
147186

148-
protected boolean 秘isAppletFrame;
149-
public boolean 秘isFramedApplet;
150-
151-
public String 秘htmlName;
152-
153-
protected int 秘num;
154-
private static int 秘incr;
155-
public boolean 秘isBackgroundPainted;
156-
protected boolean 秘alwaysPaint;
157-
private Insets 秘tempInsets;
158-
public JSGraphics2D 秘gtemp; // indicates that we are painting, so that g.setBackground() should also be set
159-
160-
161-
public boolean 秘isRootPane, 秘isContentPane;
162-
public JSAppletViewer 秘appletViewer = ((JSAppletThread) Thread.currentThread()).秘appletViewer;
163-
private JSFrameViewer 秘frameViewer, 秘topFrameViewer;
164-
public HTML5Canvas 秘canvas;
165-
public ComponentUI ui; // from JComponent
166-
167-
private String 秘uiClassID;
168-
169-
Boolean 秘peerVis;
170-
171-
/**
172-
* not totally successful; triggered for images, background, and fillBox
173-
*
174-
*/
175-
176-
public boolean selfOrChildIsPainted() {
177-
if (秘alwaysPaint || 秘isBackgroundPainted)
178-
return true;
179-
Component[] a = JSComponent.秘getChildArray((Container) this);
180-
for (int i = ((Container) this).getComponentCount(); --i >= 0;)
181-
if (((JSComponent) a[i]).selfOrChildIsPainted())
182-
return true;
183-
return false;
184-
}
185-
186-
187187
public JSComponent() {
188188
super();
189189
秘num = ++秘incr;
@@ -222,6 +222,13 @@ public Graphics getGraphics() {
222222
// if (g instanceof ConstrainableGraphics) {
223223
// ((ConstrainableGraphics) g).constrain(x, y, width, height);
224224
// } else {
225+
// Check to see if the subclass is getting this graphics
226+
// object directly, without using paint(Graphics).
227+
if (!JComponent.isComponentObtainingGraphicsFrom(null) && !秘paintsSelf()) {
228+
秘setPaintsSelf();
229+
((JSComponentUI) ui).clearPaintPath();
230+
}
231+
225232
g.translate(x, (秘isContentPane ? 0 : y));
226233
g.clipRect(0, 0, width, height); // BH changed 2018.12.05 was setClip
227234
// }
@@ -325,41 +332,21 @@ public void updateUI() {
325332
return (/** @j2sNative g.mark$ ? g : */ null);
326333
}
327334

328-
/**
329-
* This method is added to ensure that if a jpanel or other object's
330-
* background is painted to, that it becomes transparent -- since the actual
331-
* painting is not to this canvas but instead to the JRootPane canvas.
332-
*
333-
* @param jsg
334-
*/
335-
public void 秘checkBackgroundPainted(JSGraphics2D jsg, boolean init) {
336-
if (jsg == null || init) {
337-
秘isBackgroundPainted = false;
338-
秘gtemp = jsg;
339-
return;
340-
}
341-
秘gtemp = null;
342-
秘isBackgroundPainted = 秘alwaysPaint || jsg.isBackgroundPainted();
343-
if (秘isBackgroundPainted) {
344-
((JSComponentUI) ui).setPainted(jsg);
345-
// It's all one canvas, and it is behind the root pane (bad design?)
346-
// so if it is painted, we should make the root pane transparent
347-
((JSComponentUI) ((JComponent) this).getRootPane().getUI()).setPainted(jsg);
348-
}
349-
}
350-
335+
@SuppressWarnings("unused")
351336
@Override
352337
public boolean isBackgroundSet() {
353338
return (background == null ? false
354339
: /** @j2sNative this.isAWT$ || */false ? !(background instanceof UIResource) : true);
355340
}
356341

342+
@SuppressWarnings("unused")
357343
@Override
358344
public boolean isForegroundSet() {
359345
return (foreground == null ? false
360346
: /** @j2sNative this.isAWT$ || */false ? !(foreground instanceof UIResource) : true);
361347
}
362348

349+
@SuppressWarnings("unused")
363350
@Override
364351
public boolean isFontSet() {
365352
return (font == null ? null : /** @j2sNative this.isAWT$ || */false ? !(font instanceof FontUIResource) : true);
@@ -464,6 +451,13 @@ public void validate() {
464451
秘checkBackgroundPainted(jcg, false);
465452
}
466453

454+
public void 秘paintContainerBackgroundCheck(Graphics g) {
455+
JSGraphics2D jcg = 秘getJSGraphic2D(g);
456+
秘checkBackgroundPainted(jcg, true);
457+
((Container) this).paintContainer(g);
458+
秘checkBackgroundPainted(jcg, false);
459+
}
460+
467461
@Override
468462
public void addKeyListener(KeyListener l) {
469463
super.addKeyListener(l);
@@ -525,38 +519,82 @@ public void removeKeyListener(KeyListener l) {
525519
return 秘isFocusableSet && isFocusable();
526520
}
527521

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;
522+
/**
523+
* This method is added to ensure that if a jpanel or other object's
524+
* background is painted to, that it becomes transparent -- since the actual
525+
* painting is not to this canvas but instead to the JRootPane canvas.
526+
*
527+
* @param jsg
528+
*/
529+
public void 秘checkBackgroundPainted(JSGraphics2D jsg, boolean init) {
530+
if (jsg == null || init) {
531+
// 秘isBackgroundPainted = false;
532+
秘gtemp = jsg;
533+
((JSComponentUI) ui).paintBackground(jsg);
534+
return;
535+
}
536+
秘gtemp = null;
537+
// 秘isBackgroundPainted = 秘alwaysPaint || jsg.isBackgroundPainted();
538+
// if (秘isBackgroundPainted) {
539+
// ((JSComponentUI) ui).setPainted(jsg);
540+
// // It's all one canvas, and it is behind the root pane (bad design?)
541+
// // so if it is painted, we should make the root pane transparent
542+
// ((JSComponentUI) ((JComponent) this).getRootPane().getUI()).setPainted(jsg);
543+
// }
544+
}
535545

546+
public int 秘setPaintsSelf() {
547+
return 秘iPaintMyself = PAINTS_SELF_YES;
548+
}
549+
536550
/**
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)
551+
* Used by:
552+
*
553+
* JComponent to checked to see if a component in SwingJS can paint immediately
554+
* because it is opaque or DOES NOT paint itself;
555+
*
556+
* JScrollPane to check if it needs to fire a repaint()
557+
* on the scrolled component; and
558+
*
559+
* JSComponentUI to check if it can use a CSS background
540560
*
541561
* @return
542562
*/
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-
}
563+
public boolean 秘paintsSelf() {
564+
if (秘iPaintMyself == PAINTS_SELF_UNKNOWN) {
565+
// don't allow if not opaque and has components
566+
// don't allow if JComponent.paint(Graphics) has been overridden
567+
// don't allow if AbstractBorder.paintBorder(...) has been overridden
568+
// unchecked here is if a class calls getGraphics outside of this context
569+
秘iPaintMyself = (JSUtil.isOverridden(this, "paint$java_awt_Graphics", JComponent.class)
570+
|| JSUtil.isOverridden(this, "paintComponent$java_awt_Graphics", JComponent.class)
571+
|| JSUtil.isOverridden(this, "paintContainer$java_awt_Graphics", Container.class)
572+
|| JSUtil.isOverridden(秘border, "paintBorder$java_awt_Component$java_awt_Graphics$I$I$I$I",
573+
AbstractBorder.class)
574+
? 秘setPaintsSelf() : PAINTS_SELF_NO);
553575
}
554576
// TODO -- still need to set RepaintManager so that
555577
// objects with the same paintable root can be grouped together.
556-
557-
return (秘paintOverridden == -1);
578+
579+
return (秘iPaintMyself == PAINTS_SELF_YES);
558580
}
559581

582+
/**
583+
* JScrollBar needs to know if we need to paint this component when it is scrolled
584+
*
585+
*/
560586

587+
public boolean 秘selfOrChildIsPainted() {
588+
return 秘paintsSelf();
589+
}
561590

591+
// private boolean childPaintsItself() {
592+
// Component[] a = JSComponent.秘getChildArray((Container) this);
593+
// for (int i = ((Container) this).getComponentCount(); --i >= 0;)
594+
// if (((JSComponent) a[i]).秘selfOrChildIsPainted())
595+
// return true;
596+
// return false;
597+
// }
598+
599+
562600
}

0 commit comments

Comments
 (0)