How to finding an applets size, JAVA Programming

Finding an Applet's Size

When running inside a web browser the size of an applet is set through the height and width attributes and cannot be changed by the applet. Several applets require knowing their own size. After all you don't want to draw outside the lines.

Retrieving the applet size is straightforward with the getSize() method. java.applet.Applet inherits this method from java.awt.Component. getSize() returns a java.awt.Dimension object. A Dimension object has two public int fields, height and width. Below is a simple applet that prints its own dimensions.

import java.applet.*;
import java.awt.*;

public class SizeApplet extends Applet {

public void paint(Graphics g) {

Dimension appletSize = this.getSize();
int appletHeight = appletSize.height;
int appletWidth = appletSize.width;

g.drawString("This applet is " + appletHeight +
" pixels high by " + appletWidth + " pixels wide.",
15, appletHeight/2);

}

}
Remember how the applet's height is used to decide where to draw the text. You'll frequent needs to use the applet's dimensions to determine how to place objects on the page. The applet's width wasn't used because it made more sense to left justify the text rather than center it. In other programs you'll have occasion to use the applet width too.

Posted Date: 5/16/2013 6:25:15 AM | Location : United States







Related Discussions:- How to finding an applets size, Assignment Help, Ask Question on How to finding an applets size, Get Answer, Expert's Help, How to finding an applets size Discussions

Write discussion on How to finding an applets size
Your posts are moderated
Related Questions
Write a program called Power that displays the positive powers of 2. When the user enters the exponent at a prompt, the program displays 2 to that power. The program halts when the

Session Bean : is a non-persistent object that provides some business logic running on the server. Session beans do not provide system break down. There are two kinds of session b

A loop is a set of commands which executes repeatedly till a denoted condition is met. JavaScript supports two loop statements: for & while. Additionally, you can employ the break

i need java program for account to hold payment of student

Java Programming 1. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? 2. Write a program to explain the Exce

How is method overriding different from method overloading? Overriding involves the creation of two or more concepts with the same name and same signature in different classes

Ask question Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create

Write a java program that computes the scores of 10 students in the following units; 1.Java 2.PHP 3.Python 4.C++ 5.Perl Generate a transcript.

Project Description: I want to create similar site: fifacointrader I want similar site like fifacointrader.com Skills required are HTML, Java, PHP, Website Design, Wordpre

Ask question #MBookshelves A library decides buy bookshelves to store its books and they need to determine the bookshelves’ heights and widths in order to minimize storage costs. T