Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Newsletters Archive: 2

JavaFAQ Home » Java Newsletters Go to all tips in Java Newsletters


Bookmark and Share

Hello dear subsribers!

We continue to send you our daily Java tips!
As you could notice we added one new book: "Swing".
You can read it online or download on your computer. This huge (more than 800 pages) book also has a lot of working examples!

And here our tips!
…………………………………………………………
Tip1.
Question: How can I round a number to specified precision?
I have a double field that I would like to round to 2 places of precision, however, it
seems like the documentation on the round function only rounds to
closest integers. So that I would not be able say .3658585859 = .37, as
I would like.


Answer: can you scale the number up and then down again when you are finished?
e.g. 0.3658585859 * 100 = 36.58585859
round(36.58585859) = 37
37 / 100 = 0.37
…………………………………………………………


Tip2.
Q: How can you tell if an integer is odd or even?
A: I know an even number is divisible by 2 but I'm thinking then how can I detect
if a resulting number after dividing 2 ints has a remainder?

Basically the method is simple, if a variable contains an odd number
I want to return the value 'zero', and on the other hand if the
variable contains an even number I want to return the value 'one'.

Check out the % (modulus) operator, it computes the remainder.
public int modMethod(int div){
if(div%2==0){
return 1;
} else{
return 0;
}
}
…………………………………………………………
Tip3.
Q: How do I view the error output from my Java applets in IE?
A: The file windowsJavaJavalog.txt contains info about the last Applet loaded in IE.
All the System.out messages and exception information is stored here when Java Logging
is enabled in IE. To enable Java Logging start IE and select View/Options/Advanced.
Select "Enable Java Logging" check box click OK. Restart IE.
In NT4 the file in C:WINNTJava

…………………………………………………………
Tip4.
Is JavaScript same as Java?


A: NO! An Amazingly large number of people, including many web designers, don't understand the difference between Java and JavaScript. Briefly it can be summed up as follows:

Java was developed by Sun Microsystems. Java is a full-fledged object-oriented programming language. It can be used to create standalone applications and applet. Applets are downloaded as separate files to your browser alongside an HTML document, and provide an infinite variety of added functionality to the Web site you are visiting. The displayed results of applets can appear to be embedded in an HTML page (e.g., the scrolling banner message that is so common on Java-enhanced sites), but the Java code arrives as a separate file.

JavaScript on the other hand was developed by Netscape, is a smaller and simpler scripting language that does not create applets or standalone applications. In its most common form today, JavaScript resides inside HTML documents, and can provide levels of interactivity far beyond typically flat HTML pages -- without the need for server-based CGI (Common Gateway Interface) programs.

Some server software, such as Netscape's SuiteSpot, let's web application developers write CGI programs in a server-side version of JavaScript. Both client-side and server-side JavaScript share the same core JavaScript language, but each side deals with different kinds of objects. Client-side objects are predominantly the components of an HTML web page (e.g., forms, text boxes, buttons). Server-side objects are those that facilitate the handling of requests that come from clients, as well as connectivity to databases.
…………………………………………………………
Tip5.
Q: How can I minimise "Flicker" in animation? Solution 1:
A: Solution 1:

Override update(): Flickering in animation occurs because default update() method clears the screen of any existing contents and then calls paint(). To reduce flickering, therefore, override update(). Here is how just add the following code to your applet:

public void update(Graphics g) {
paint(g);
}


What the update is now doing is just calling paint() and not clearing it, a further refinement to the above will be to override update() method and painting only the region where the changes are taking place. Here is how:

public void update(Graphics g) {
g.clipRect(x, y, w, h);
paint(g);
}


Solution 2 will be described tomorrow

…………………………………………………………
Tip6.
How can I minimise "Flicker" in animation? Solution 2:
Solution 1 was described in our tip yesterday
Solution 2:

Use double-buffering: double buffering is the process of doing all your drawing to an offscreen and then displaying the entire screen at once. It is called double buffering because there are two drawing buffers and you switch between them. Use double buffering only if the above solution alone does not work. The following code snippet describes how to do it.

Image offscreenImage;
Graphics offscreenGraphics;
offscreenImage = createImage(size().width, size().height);
offscreenGraphics = offscreenImage.getGraphics();
offscreenGraphics.drawImage(img, 10, 10, this);
g.drawImage(offscreenImage, 0, 0, this);
…………………………………………………………
Tip7.
How do we exchange data between Java and JavaScript and vice-versa?
A: Public variable and methods of Java Applet are visible to a HTML document. So using JavaScript you can access the public variables and public functions.

The syntax is:

var some_var = document.appletname.variable_name

With this you will get the value of the variable variable_name in your JavaScript variable some_var.


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy