Skip to content

Commit 5e4d89a

Browse files
committed
fixes for initialization events
1 parent f6b55a4 commit 5e4d89a

5 files changed

Lines changed: 46 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.awt.Graphics;
44

5+
import javax.swing.JComponent;
6+
57

68
public class Canvas extends Panel {
79

@@ -30,7 +32,7 @@ public void update(Graphics g) {
3032
/**
3133
* @j2sNative
3234
*
33-
* this.paintComponent && this.paintComponent(g);
35+
* this.paintComponent$java_awt_Graphics && this.paintComponent$java_awt_Graphics(g);
3436
*
3537
*/
3638
{}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ public void updateUI() {
202202
public boolean isBackgroundPainted;
203203

204204

205+
/**
206+
* This method is added to ensure that if a jpanel or other object's background
207+
* is painted to, that it becomes transparent -- since the actual painting is
208+
* not to this canvas but instead to the JRootPane canvas.
209+
*
210+
* @param jsg
211+
*/
205212
public void checkBackgroundPainted(JSGraphics2D jsg) {
206213
if (jsg == null) {
207214
isBackgroundPainted = false;

sources/net.sf.j2s.java.core/src/javajs/util/DF.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
* $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $
44
* $Revision: 7502 $
55
*
6-
* Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7-
* for use in SwingJS via transpilation into JavaScript using Java2Script.
8-
*
96
* Copyright (C) 2005 The Jmol Development Team
107
*
118
* Contact: jmol-developers@lists.sf.net
@@ -73,7 +70,7 @@ public static String formatDecimal(float value, int decimalDigits) {
7370
if (decimalDigits > formattingStrings.length)
7471
decimalDigits = formattingStrings.length;
7572
if (value == 0)
76-
return formattingStrings[decimalDigits] + "E+0";
73+
return formattingStrings[decimalDigits - 1] + "E+0";
7774
//scientific notation
7875
n = 0;
7976
double d;
@@ -87,9 +84,19 @@ public static String formatDecimal(float value, int decimalDigits) {
8784
String s = ("" + d).toUpperCase();
8885
int i = s.indexOf("E");
8986
n = PT.parseInt(s.substring(i + 1)) + n;
90-
return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring(
91-
0, i)), decimalDigits - 1)
92-
+ "E" + (n >= 0 ? "+" : "") + n);
87+
String sf;
88+
if (i < 0) {
89+
sf = "" + value;
90+
} else {
91+
float f = PT.parseFloat(s.substring(0, i));
92+
if (f == 10 || f == -10) {
93+
//d = 9.99999997465; n = -6 --> 10.00000E-5
94+
f /= 10;
95+
n += (n < 0 ? 1 : -1);
96+
}
97+
sf = formatDecimal(f, decimalDigits - 1);
98+
}
99+
return sf + "E" + (n >= 0 ? "+" : "") + n;
93100
}
94101

95102
if (decimalDigits >= formattingStrings.length)

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ protected void paintComponent(Graphics g) {
629629
// note that this update will fill the component's background, but
630630
// that is not what we are worried about. What we are worried about
631631
// is if this method is overridden and is written to.
632-
// BH TODO CHECK THIS
633632
isBackgroundPainted = false;
634633
ui.update(scratchGraphics, this);
635634
// BH TODO CHECK THIS
@@ -4880,6 +4879,8 @@ void setPaintingChild(Component paintingChild) {
48804879

48814880
void _paintImmediately(int x, int y, int w, int h) {
48824881

4882+
// this method is called on JPanel if the developer uses jpanel.repaint()
4883+
48834884
Graphics g;
48844885
Container c;
48854886
// Rectangle b;
@@ -5041,7 +5042,14 @@ void _paintImmediately(int x, int y, int w, int h) {
50415042
// SwingJS not clipping for better performance
50425043
// g.setClip(paintImmediatelyClip.x,paintImmediatelyClip.y,
50435044
// paintImmediatelyClip.width,paintImmediatelyClip.height);
5044-
paintingComponent.paint(g);
5045+
5046+
// this sequence assures that if the developer called
5047+
// jpanel.repaint() and then draws on the background,
5048+
// the JPanel's background is made transparent
5049+
// (so that the underlying JRootPane canvas can show).
5050+
checkBackgroundPainted(null);
5051+
paintingComponent.paint(g);
5052+
checkBackgroundPainted((JSGraphics2D) (Object) g);
50455053
}
50465054
} finally {
50475055
g.dispose();

sources/net.sf.j2s.java.core/src/swingjs/JSGraphics2D.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ private boolean drawImageXT(Image img, AffineTransform xform,
442442
return ret;
443443
}
444444

445+
private Object imageData;
446+
private int[] buf8;
447+
private int lastx,lasty,nx,ny;
448+
445449
@SuppressWarnings("unused")
446450
public boolean drawImagePriv(Image img, int x, int y, ImageObserver observer) {
447451
backgroundPainted = true;
@@ -465,8 +469,14 @@ public boolean drawImagePriv(Image img, int x, int y, ImageObserver observer) {
465469
if ((imgNode = getImageNode(img)) != null)
466470
ctx.drawImage(imgNode, x, y, width, height);
467471
} else {
468-
Object imageData = HTML5CanvasContext2D.getImageData(ctx, x, y, width, height);
469-
int[] buf8 = HTML5CanvasContext2D.getBuf8(imageData);
472+
if (buf8 == null || x != lastx || y != lasty || nx != width || ny != height) {
473+
imageData = HTML5CanvasContext2D.getImageData(ctx, x, y, width, height);
474+
buf8 = HTML5CanvasContext2D.getBuf8(imageData);
475+
lastx = x;
476+
lasty = y;
477+
nx = width;
478+
ny = height;
479+
}
470480
for (int pt = 0, i = 0, n = Math.min(buf8.length/4, pixels.length); i < n; i++) {
471481
int argb = pixels[i];
472482
buf8[pt++] = (argb >> 16) & 0xFF;

0 commit comments

Comments
 (0)