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
Assume all the obstacles are of height 4 units and the blocks are of heights 1 (bottom), 2 (top), 1 and 2 (top) units respectively.  Complete the control() method of the RobotContr

What is Event Handler works? An event handler is a command which is used to call a function when an event happens, such as the user clicking a button or mouse.

What is Constraints explain with example? One of the purposes to use constructors and setter techniques rather than directly accessing fields is to enforce constraints. For exa

Struts Framework gives the functionality to validate the form data. It can be use to authenticate the data on the users browser as well as on the server side. Struts Framework emit

Aggregation Composition Aggregation :  An   association   in  which  one  class refers to collection or a another class. This is a part of  a  who

AskWrite an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be “I love

This assignment is a good project for working with 2 dimensional arrays. In this program, you will implement an ascii version of the classic game "Connect 4". Connect 4 is a 2 p

1. Inception : In the mean while the inception phase, you work out the business part for the project. You also will be creating a rough cost estimate and return on investment.

Write a program that reads both the girl and boy files into memory using arrays. Then, allow the user to input a name. The program should search through both arrays. If there is