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
what is thread?

Objectives 1. To help you become comfortable with using inheritance and interfaces to construct type hierarchies. 2. To familiarize you with programming from specifications. A

What does WAN stand for? Wide Area Network - A network in that computers are separated through great distances classically across cities or even continents.

Question: (a) Explain the importance of abstract classes in Java with an appropriate example. (b) Explain what is meant by an interface in Java? Use a simple example to il

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

JavaServer Faces (JSF) framework : JavaServer Faces is a new framework for creating Web applications using Java. JSF gives you with the following main features: Basic us

I want cyclos mobile banking USSD and IVR module Project Description: We already have a Cyclos mobile banking project deployed on our server with SMS banking module. The s

To integrate your Struts application with Spring, we have two options: ? Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependenc

The Action Class is type of the Model and is a wrapper around the business logic. The purpose of Action Class is to change the HttpServletRequest to the business logic. To use the

Write a program in java that will analyze stock data and purchase stock with the user''s specific risk and investment. User will select risk from a GUI, high, medium or low and inp