Skip to content

Commit 6e9bd38

Browse files
hansonrhansonr
authored andcommitted
image inheritance of Component font, background, foreground
1 parent f331ae1 commit 6e9bd38

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,11 @@ Container getContainer() {
892892
*/
893893
@Deprecated
894894
public ComponentPeer getPeer() {
895-
return peer;
895+
// SwingJS: In Java, peers are only available if the
896+
// component is a heavy-weight component AND it is connected to the DOM
897+
// via its ancestors. We shall see how this goes....
898+
899+
return (isDisplayable() ? peer : null);
896900
}
897901

898902
/**
@@ -1053,22 +1057,6 @@ void resetGC() {
10531057
* @since JDK1.0
10541058
*/
10551059
public Toolkit getToolkit() {
1056-
return getToolkitImpl();
1057-
}
1058-
1059-
/*
1060-
* This is called by the native code, so client code can't be called on the
1061-
* toolkit thread.
1062-
*/
1063-
final Toolkit getToolkitImpl() {
1064-
ComponentPeer peer = this.peer;
1065-
if ((peer != null) && !(peer instanceof LightweightPeer)) {
1066-
return peer.getToolkit();
1067-
}
1068-
Container parent = this.parent;
1069-
if (parent != null) {
1070-
return parent.getToolkitImpl();
1071-
}
10721060
return Toolkit.getDefaultToolkit();
10731061
}
10741062

@@ -1114,7 +1102,8 @@ public boolean isValid() {
11141102
* @since 1.2
11151103
*/
11161104
public boolean isDisplayable() {
1117-
// return getPeer() != null;
1105+
// that is, if this component is connected to a top-level ancestor
1106+
// see JSComponent
11181107
return true;
11191108
}
11201109

@@ -3232,7 +3221,7 @@ public Image createImage(ImageProducer producer) {
32323221
// if ((peer != null) && ! (peer instanceof LightweightPeer)) {
32333222
// return peer.createImage(producer);
32343223
// }
3235-
return getToolkit().createImage(producer);
3224+
return (isDisplayable() ? ((JSToolkit) getToolkit()).createImage(this, producer) : null);
32363225
}
32373226

32383227
/**
@@ -3250,7 +3239,7 @@ public Image createImage(ImageProducer producer) {
32503239
* @since JDK1.0
32513240
*/
32523241
public Image createImage(int width, int height) {
3253-
return Toolkit.getDefaultToolkit().createImage(null, width, height);
3242+
return (isDisplayable() ? ((JSToolkit) getToolkit()).createImage(this, width, height) : null);
32543243
// ComponentPeer peer = this.peer;
32553244
// if (peer instanceof LightweightPeer) {
32563245
// if (parent != null) { return parent.createImage(width, height); }
@@ -3276,6 +3265,7 @@ public Image createImage(int width, int height) {
32763265
* @since 1.4
32773266
*/
32783267
public VolatileImage createVolatileImage(int width, int height) {
3268+
// SwingJS TODO
32793269
// ComponentPeer peer = this.peer;
32803270
// if (peer instanceof LightweightPeer) {
32813271
// if (parent != null) {
@@ -3306,7 +3296,7 @@ public VolatileImage createVolatileImage(int width, int height) {
33063296
* @since 1.4
33073297
*/
33083298
public VolatileImage createVolatileImage(int width, int height, ImageCapabilities caps) throws AWTException {
3309-
// REMIND : check caps
3299+
// SwingJS TODO
33103300
return createVolatileImage(width, height);
33113301
}
33123302

@@ -3346,6 +3336,7 @@ public boolean prepareImage(Image image, ImageObserver observer) {
33463336
* @since JDK1.0
33473337
*/
33483338
public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
3339+
// SwingJS TODO
33493340
// ComponentPeer peer = this.peer;
33503341
// if (peer instanceof LightweightPeer) {
33513342
// return (parent != null)

sources/net.sf.j2s.java.core/src/java/awt/image/BufferedImage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
package java.awt.image;
3030

31+
import java.awt.Component;
3132
import java.awt.Graphics;
3233
import java.awt.Graphics2D;
3334
import java.awt.GraphicsEnvironment;
@@ -82,6 +83,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
8283
protected int width, height;
8384
protected boolean _havePix;
8485
protected Object _canvas; // created in setRGB
86+
public Component _component; // for context from component.createImage()
8587
private int[] _pixSaved;
8688
JSGraphics2D _g; // a JSGraphics2D instance
8789
//private static int rangeIndex;
@@ -1780,7 +1782,13 @@ public Graphics2D getImageGraphic() {
17801782
}
17811783
_pix = null;
17821784
}
1783-
return (Graphics2D) (Object)_g;
1785+
Graphics2D g2d = (Graphics2D) (Object)_g;
1786+
if (_component != null) {
1787+
g2d.setFont(_component.getFont());
1788+
g2d.setBackground(_component.getBackground());
1789+
g2d.setColor(_component.getForeground());
1790+
}
1791+
return g2d;
17841792
}
17851793

17861794

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package swingjs;
22

3-
import javajs.util.Base64;
3+
import java.awt.Component;
4+
import java.awt.Image;
45
import java.awt.image.BufferedImage;
6+
7+
import javajs.util.Base64;
58
import swingjs.api.js.DOMNode;
69

710
/**
@@ -65,6 +68,15 @@ public void getDOMImage(byte[] b, String type) {
6568
{}
6669
_imgNode = img;
6770
}
68-
71+
72+
/**
73+
* font will be derived from this component when graphics is created
74+
* @param c
75+
* @return
76+
*/
77+
public Image setComponent(Component c) {
78+
_component = c;
79+
return this;
80+
}
6981

7082
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,5 @@ public static ImageIcon createImageIcon(Component c, Icon icon, String id) {
276276
return new ImageIcon(img, "paintedIcon");
277277
}
278278

279-
280279

281280
}

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -232,24 +232,6 @@ public static boolean isFocused(Window window) {
232232
return false;
233233
}
234234

235-
/**
236-
* generates proper font name for JSGraphics2d Apparently Java sizes are
237-
* pixels, not points. Not sure on this...
238-
*
239-
* @param font
240-
* @return "italic bold 10pt Helvetica"
241-
*/
242-
public static String getCSSFont(Font font) {
243-
String css = "";
244-
if (font.isItalic())
245-
css += "font-style:italic;";
246-
if (font.isBold())
247-
css += "font-weight:bold;";
248-
css += "font-size:" + font.getSize() + "px;";
249-
css += "font-family:" + font.getFamily() + ";";
250-
return css;
251-
}
252-
253235
private static HTML5CanvasContext2D defaultContext;
254236

255237
public static float getStringWidth(HTML5CanvasContext2D context, Font font,
@@ -606,6 +588,11 @@ public Image createImage(ImageProducer producer) {
606588
return kit.getCreatedImage();
607589
}
608590

591+
public Image createImage(Component c, ImageProducer producer) {
592+
return ((JSImage) createImage(producer)).setComponent(c);
593+
}
594+
595+
609596
public static ImageIcon createImageIcon(Component c, Icon icon, String id) {
610597
return JSImagekit.createImageIcon(c, icon, id);
611598
}
@@ -629,6 +616,10 @@ public Image createImage(byte[] data, int imageoffset, int imagelength) {
629616
return getImagekit().createImageFromBytes(data, imageoffset, imagelength, null);
630617
}
631618

619+
public Image createImage(Component c, int width, int height) {
620+
return ((JSImage) createImage((byte[]) null, width, height)).setComponent(c);
621+
}
622+
632623
@Override
633624
public int checkImage(Image image, int width, int height,
634625
ImageObserver observer) {
@@ -830,7 +821,7 @@ public FontMetrics getFontMetrics(Font font) {
830821
* pixels, not points. Not sure on this...
831822
*
832823
* @param font
833-
* @return "italic bold 10pt Helvetica"
824+
* @return "italic bold 10pt Arial"
834825
*/
835826
public static String getCanvasFont(Font font) {
836827
String strStyle = "";
@@ -846,7 +837,9 @@ public static String getCanvasFont(Font font) {
846837

847838
public static String getCSSFontFamilyName(String family) {
848839
family = family.toLowerCase();
849-
if (family.equals("sansserif") || family.equals("dialog")
840+
if (family.equals("sansserif")
841+
|| family.equals("helvetica")
842+
|| family.equals("dialog")
850843
|| family.equals("dialoginput"))
851844
family = "Arial";
852845
else if (family.equals("monospaced"))

0 commit comments

Comments
 (0)