Draw Rectangles in java
In this example we are going to draw Rectangle.
The Rectangle can be drawn with drawRect() and
fillEect(). We are using two type of package. One is java.awt.*
and second is java.applet.* .The java.awt.* is used for
GUI interface. AWT classes are contain in this package. The java.apple.*
package is used to create an applet.
In this we are using Graphics class and
Applet class.
Here we are using three methods which are following:
void drawRect ( int top, int left, int
width, int height ); This method is used to draw Rectangle.
void fillRect ( int top, int left,
int width, int height); This method is used to draw and fill Rectangle.
paint (Graphics g);
This method is used to paint the
applet.
Here drawRect(); and filli Rect(); use to
create Rectangle. The upper-left corner of Rectangles are denoted by
top, left
and the other dimensions are denoted by height and width.
Here first we are importing two pacakages.Then after we
have write applet tag into comments. In this tag we have pass three arguments
code ,height and width. The code attribute is used to call the applet
class. And Height , width is used for Sizing the window.
Then we have create a class and extends this class with
Applet Class to accurse the property of applet. We had override paint method of
applet class. Then we pass the valu of upper left cornor to set initial
point ( int left, int top) and then pass the other dimension
height and width (int height, int width).
//Draw Rectangle
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles" width=300 height=200>
</applet>
*/
public class Rectangles extends Applet{
public void paint(Graphics g){
g.drawRect(0,0,40,40);
g.fillRect(50,50,100,100);
}
}
|
Download Source Code
Output Of Example
