Explain drawing rectangles in java, JAVA Programming

Explain Drawing Rectangles in java ?

Drawing rectangles is easy. Begin with a Graphics object g and call its drawRect() method:
public void drawRect(int x, int y, int width, int height)
As the variable names suggest, the first int is the left hand side of the rectangle, the second is the top of the rectangle, the third is the width and the fourth is the height. This is in contrast to a few APIs whereas the four sides of the rectangle are given.
This uses drawRect() to draw a rectangle around the sides of an applet.

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

public class RectangleApplet extends Applet {

public void paint(Graphics g) {

g.drawRect(0, 0, this.getSize().width - 1, this.getSize().height - 1);

}

}
Notice that getSize().width is the width of the applet and getSize().height is its height.

Why was the rectangle drawn only to getSize().height-1 and getSize().width-1?

Remember which the upper left hand corner of the applet starts at (0, 0), not at (1, 1). This means in which a 100 by 200 pixel applet includes the points along with x coordinates among 0 and 99, not between 0 and 100. As same the y coordinates are among 0 and 199 inclusive, not 0 and 200.

There is no separate drawSquare() method. A square is just a rectangle along with equal length sides, so to draw a square call drawRect() and pass the same number for both the height and width arguments.

Posted Date: 5/16/2013 7:29:25 AM | Location : United States







Related Discussions:- Explain drawing rectangles in java, Assignment Help, Ask Question on Explain drawing rectangles in java, Get Answer, Expert's Help, Explain drawing rectangles in java Discussions

Write discussion on Explain drawing rectangles in java
Your posts are moderated
Related Questions
Ask questionWrite a program BruteForceSearch that uses the brute-force approach given above and compare its running time on your computer with that of Binary Search for largeW.txt

Java is increasingly used for middleware applications to communicate among Server and clients. Java has features like multithreading, portability and networking capabilities. Chang

Develop a code for fibonacci series

Im not in the line of computer science.But i would like to have some knowledge about the computer language "JAVA".I aldredy learns the computer languages "C,C++".So kindly help me

I have a very simple app that I have developed and is in the AppStore. I need the app to be developed for Android as it is presently in Objective C only. All plist html/ lists s

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

What is the output of the following code: for (i=0; i { document.write("The number is " + i); document.write(" "); } Output The number is 0 The number is 1 The number i

What are Trojan Horses? Trojan Horses are unlike viruses usually they are stand-alone programs. The look such as what they are not. They appear to be something harmless and int

An aspect is the cross-cutting functionality that you are executing. It is the aspect of your application you are modularizing. An example of an aspect is logging. Logging is somet

Do you provides a Complete source Codes for this application: " a. Write an application for a video store. Place the names of 10 of your favorite movies in a combo box. Let the use