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
The break statement is utilized for terminating the current While or For loop and after that transferring program control towards statement just after the terminated loop. The foll

What is static in java? Static means one per class, not one for each object no matter how many example of a class might exist. This means that you can use them without making a

How does a try statement determine which catch clause should be used to handle the exception?

How to Implementing Interfaces in java? To actually utilize this interface you create a class that involves a public double calculateTariff() method and declare in which the cl

A mechanism for converting among string-based markup generated by JavaServer Faces UI components and server-side Java objects.

This is a two part project, one in Alice, and another in Java. You can either zip the files together as one and upload the single zip file, OR upload the separate files. If you dec

how much it will cost me

Describe the Class or static Members ? A method or a field in a Java program could be declared static. That means the member belongs to the class rather than to an individual

how can i add script code in anouncement of a channel in a massneger the massnegr suport that but do not published it please help me please:((((((((((((((((((((((((((((((((((((((((

Explain about the Static classes A  class  can  have  static  variables    and    methods.    Static    methods    and    variables    are  associated  with  class  itself  an