Skip to content

Commit 8574afe

Browse files
committed
allows for applet.resize(width,height) in JavaScript or Java
1 parent 6d9e8fa commit 8574afe

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed
-166 Bytes
Binary file not shown.

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import java.awt.Panel;
3737
import java.util.Locale;
3838

39+
import swingjs.JSToolkit;
40+
import swingjs.JSUtil;
41+
import swingjs.api.js.HTML5Applet;
42+
3943
/**
4044
* An applet is a small program that is intended not to be run on
4145
* its own, but rather to be embedded inside another application.
@@ -51,6 +55,19 @@
5155
*/
5256
public class Applet extends Panel {
5357

58+
static {
59+
60+
/**
61+
* SwingJS providing unqualified resize(w,h) as a convenience only
62+
*
63+
* @j2sNative
64+
*
65+
* C$.prototype.resize = C$.prototype.resize$I$I;
66+
*
67+
*/
68+
69+
}
70+
5471
/**
5572
* Constructs a new Applet.
5673
* <p>
@@ -200,19 +217,28 @@ public void setVisible(boolean b) {
200217
* @param width the new requested width for the applet.
201218
* @param height the new requested height for the applet.
202219
*/
203-
@SuppressWarnings("deprecation")
204220
@Override
205-
public void resize(int width, int height) {
221+
public void resize(int width, int height) {
222+
if (appletViewer == null) {
223+
resizeOriginal(width, height);
224+
} else {
225+
// use J2S.Applet._resizeApplet.
226+
appletViewer.html5Applet._resizeApplet(new int[] {width, height});
227+
}
228+
}
229+
230+
@SuppressWarnings("deprecation")
231+
public void resizeOriginal(int width, int height) {
206232
Dimension d = size();
207233
if ((d.width != width) || (d.height != height)) {
208234
super.resize(width, height);
209235
if (stub != null) {
210236
stub.appletResize(width, height);
211237
}
212238
}
213-
}
239+
}
214240

215-
/**
241+
/**
216242
* Requests that this applet be resized.
217243
*
218244
* @param d an object giving the new width and height.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767

6868
import sun.awt.AppContext;
6969
import sun.awt.SunToolkit;
70-
import swingjs.JSImagekit;
7170
import swingjs.JSToolkit;
7271

7372
/**

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
package java.awt;
2929

3030
import java.awt.peer.ComponentPeer;
31-
import java.beans.PropertyChangeEvent;
3231
import java.beans.PropertyChangeListener;
3332

3433
import javax.swing.JComponent;
@@ -50,6 +49,22 @@
5049
*/
5150
public abstract class JSComponent extends Component {
5251

52+
/**
53+
* overridden in Applet
54+
*
55+
* SwingJS Applet repurposes resize(width, height) to call
56+
* J2S.Applet.prototype._resizeApplet in order to take care
57+
* of all the HTML5 business associated with this applet.
58+
*
59+
*
60+
* @param width
61+
* @param height
62+
*/
63+
@SuppressWarnings("deprecation")
64+
public void resizeOriginal(int width, int height) {
65+
resize(width, height);
66+
}
67+
5368
/**
5469
* For modal dialogs, make sure the parent component, if there is one, is a
5570
* PropertyChangeListener for this component (JOptionPane, JFileChooser, or

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.awt.Insets;
1414
import java.awt.Toolkit;
1515
import java.awt.Window;
16-
import java.io.IOException;
1716
import java.net.MalformedURLException;
1817
import java.net.URL;
1918
import java.net.URLStreamHandlerFactory;
@@ -411,7 +410,7 @@ public int run1(int mode) {
411410
}
412411
System.out.println("JSAppletViewer init");
413412
japplet.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
414-
japplet.resize(defaultAppletSize);
413+
japplet.resizeOriginal(defaultAppletSize.width, defaultAppletSize.height);
415414
japplet.init();
416415
// Need the default(fallback) font to be created in this
417416
// AppContext
@@ -430,7 +429,7 @@ public int run1(int mode) {
430429
japplet.getRootPane().addNotify();
431430
// force peer creation now
432431
System.out.println("JSAppletViewer start" + currentAppletSize);
433-
japplet.resize(currentAppletSize);
432+
japplet.resizeOriginal(currentAppletSize.width, currentAppletSize.height);
434433
japplet.start();
435434
// japplet.repaint();
436435
status = APPLET_START;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void setScreenDimension(int width, int height) {
141141
setGraphics((Graphics) (Object)(jsgraphics = null), width, height);
142142
//resize(width, height);
143143
if (top != null)
144-
top.resize(width, height);
144+
top.resizeOriginal(width, height);
145145
}
146146

147147
/**

sources/net.sf.j2s.java.core/src/swingjs/plaf/Resizer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.awt.Dimension;
99
import java.awt.JSComponent;
1010
import java.awt.Rectangle;
11+
12+
import javax.swing.JApplet;
1113
import javax.swing.JFrame;
1214
import javax.swing.JRootPane;
1315
import javax.swing.RootPaneContainer;
@@ -146,8 +148,7 @@ protected void fHandleDOMResize(Object event, int dw, int dh) {
146148
if (jframe == null) {
147149
rootPane.getGraphics().setColor(Color.WHITE);
148150
rootPane.getGraphics().fillRect(0, 0, r.width, r.height);
149-
rootPane.appletViewer.html5Applet._resizeApplet(new int[] { r.width,
150-
r.height });
151+
((JApplet) rootPane.getParent()).resize(r.width, r.height);
151152
} else {
152153
jframe.setPreferredSize(new Dimension(r.width, r.height));
153154
jframe.invalidate();

0 commit comments

Comments
 (0)