JavaFX - Performance Optimization


There was a question related to Best Practices for speed optimization in Oracle-Mix forum. There are many small tips & tricks, just thought of listing out what ever came to my mind..

Use Layout: For positioning node always use Layout. There are plenty of layout available such as HBox, VBox, Tile, Flow and Stack. If we need any custom layout you can use Container or Panel. Position node using layoutX and layoutY attributes. Use translateX and translateY for Animation.

Consolidate Theme Attributes : We can reuse many theme related objects such as Font, Color, Paint, Image. Eg: If we use same Font for many Label/Text, we can use the same Font instance instead of creating one each for each instance of Node. This consolidation also allow us to customize the UI. You may refer to Theme used in Calendar sample. Also share Image instances such as default-image, place-holder image etc across multiple objects.

Bind with Caution: I have stressed on this in one of my previous post. Repeating the same again. Use this mostly for wiring model to view. Minimize use for other purpose such as layout of Nodes. Also better to avoid chaining of binds, its very problematic and difficult to debug.

JavaFX for View: There is a tendency to write entire application using pure JavaFX. This may be fine for small widget like application or samples. But when you write real business application we need to push out the business logic, IO operations and non-ui related code to Java layer. Use JavaFX Async APIs + Callback methods so as to execute these code outside Event-Dispatch-Thread. HttpRequest API demonstrates use of this approach.

Reduce use of Effects: In many cases we may need to have static effects in UI, such as nice gradient for background, font effects etc. If these effects are static, its always better to use Image instead of effect API.

Stroke Vs Fill: Instead of using stroke attribute for shape, we may use nested shapes with fill attribute. Example: For creating a stroke for Rectangle, we can create two instances of Rectangle overlapping, with the inner Rectangle smaller than outer Rectangle. This will have better performance compared to use of stroke attribute.

Avoid Class-Path in Manifest: When we add various jars as library to Netbeans, these jars are directly referenced in Class-Path of Manifest file of the application jar. When deployed, this will force to download all jars before start of application. Concept of lazy download will not work. Also there will be an attempt to make an unnecessary network connection, even though resources such as plugin.jar, javaws.jar etc are not hosted. This will increase initial applet/javaws startup time.

Use Image.width & Image.height: If we need to show a thumbnail of large image, its better to specify its width and height Image object itself rather than resizing the ImageView using fitWidth and fitHeight attribute. This will reduce the memory used to hold the image.

Provide Startup Feedback: Many a times people get bored due to lack of proper feedback during startup. This creates a notion of increased startup time. Except for small use-cases such as advertisements, RIA's do have some startup time. It will be nice to provide a Custom Splash Screen and even better a Progress Listener UI to give useful feedback.

Tips for Mobile: : Joshua Marinacci has listed some useful tips for mobile.

I'm sure there are more... 'll keep updating this list...


<script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="/rakeshmenonp/resource/JavaFXPerfOpt/zoneit.js"></script>

Comments:

Hi Rakesh,

Small question about JavaFX for View: JavaFX Async APIs + Callback methods

In Switzerland, available online PLC Saia-Siemens. This demo was intended to try SDK .NET application..
URL("....://cgi-demo.saia-sps.dyndns.org/cgi-bin/readVal.exe?PDP,,MW100,d").
Loads a file (text / html) from the specified URL and the received reply contents of the file to Response (String)with MW100.

The code VB.NET is:
' Loads a file (text / html) from the specified URL and the received reply
' Contents of the file to Response (String)

1. Function URLLoad(ByVal URL As String, ByRef Response As String) As Integer
2.' Defining Object for Web Access
3. Dim wReq As WebRequest
4. Dim wResp As WebResponse

5. Try
6.' Loads a file (text / html) from the specified URL and the received reply
7. wReq = WebRequest.Create("....://cgi-demo.saia-sps.dyndns.org/cgi-bin/readVal.exe?PDP,,MW100,d")
8.' Default Proxy-Server
9. wReq.Proxy = WebProxy.GetDefaultProxy
10. wReq.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials ' Read response (Stream)
11. wResp = wReq.GetResponse()
12. Catch ex As Exception
13. pdNetCom_ErrorMessage = ex.Message
14. pdNetCom_ErrorString = ex.ToString
15. Return -1
16. End Try

17. Dim sr As New StreamReader(wResp.GetResponseStream(), Encoding.ASCII)
18. Dim strSource As New StringBuilder("")
19. Dim strTemp As String = ""
20.' Contents of the file to Response (String)

21. strTemp = sr.ReadLine()
22. While Not (strTemp Is Nothing)
23. strSource.Append((strTemp + ControlChars.Cr + ControlChars.Lf))
24. strTemp = sr.ReadLine()
End While
25. Response = strSource.ToString

26. wResp.Close()
27. sr.Close()
28. Return 0

29. End Function

Is it possible to do with JavaFX + Async Callback APIs?

Thanks in advance!

Posted by Alex on March 30, 2010 at 04:07 PM IST #

@Alex You can use HttpRequest class. Specify location attribute as "http://cgi-demo.saia-sps.dyndns.org/cgi-b
in/readVal.exe?PDP,,MW100,d"

You can read the response from InputStream provided by onInput function. Please refer to below docs for more information..

http://java.sun.com/javafx/1.2/docs/api/javafx.io.http/javafx.io.http.HttpRequest.html

http://blogs.sun.com/rakeshmenonp/entry/javafx_webservices

Posted by Rakesh Menon on March 31, 2010 at 02:27 AM IST #

Hi Rakesh,
I've done that with java class in javaFX. That works. I have not found suitable JavaFX parser (XML or JSON or other). onInput: response is few bytes (contents of register = example: 34).

Do you know what parser of JavaFX in this case can use?
That's code.

var httpRequest = HttpRequest {

location: "http://cgi-demo.saia-sps.dyndns.org/cgi-bin/readVal.exe?PDP,,MW{merk},d"
headers: [ httpHeader ]

onInput: function(is: InputStream) {
try {
var result: StringBuffer = new StringBuffer();
var reader: BufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
var temp: String;
while ((temp = reader.readLine()) != null) {
result.append(temp);
}
respons = result.toString();
} finally {
println("{is}");
is.close();
}
}
onException: function(ex: java.lang.Exception) {
onException("{ex.getMessage()}");
println("WARNING: {ex}");
}
};
httpRequest.start();

Thanks in advance!

Posted by Alex on April 01, 2010 at 10:55 AM IST #

You can use "read" instead of "readLine" (that will wait for new-line character). Since all the APIs used there is pure java, you can move all those InputStream related code to Java class.

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
char[] cbuf = new char[10];
int numChar = reader.read(cbuf, 0, 10);
String responseStr = new String(cbuf, 0, numChar);
reader.close();

Posted by Rakesh Menon on April 01, 2010 at 11:51 AM IST #

Hi Rakesh,
Another little question.
"Http://cgi-demo.saia-sps.dyndns.org/cgi-bin/readVal.exe?PDP,, MW100, d '
This code, if you place in the HTML page, and click it with mouse, that provides direct content of MV100 in new HTML page.
In NetBeans project can carry 4 options:
1. Standard detail - always works.
2. Web Start application - only first time. Then reinstall through control panel applet and then start project again, then applet works first time.
3. Run in browser - not working! with message: access denied (java.net. demo.saia Socket Permission cgi-sps.dyndns.org: 80 connect, resolve)
4. Run in mobile

Why in browser (3) does not work and what I can do?

Thanks in advance!

Posted by Alex on April 13, 2010 at 11:12 PM IST #

@Alex Since you are connecting to another website, you need to sign the application. You can do this from project properties in NetBeans.

Posted by Rakesh Menon on April 15, 2010 at 06:42 AM IST #

Post a Comment:
Comments are closed for this entry.
About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today