Explain drawing rectangles in java, JAVA Programming

Assignment Help:

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.


Related Discussions:- Explain drawing rectangles in java

Upload a file in struts? , In JSP page give the code as given below: en...

In JSP page give the code as given below: enctype="multipart/form-data" name="fileForm" scope="session">     Please select file here that you would like to upload:

Decsion tree, implementation of DT in netbeans

implementation of DT in netbeans

Explain data security management issue, Explain DATA SECURITY Management is...

Explain DATA SECURITY Management issue? All data in the system is extremely critical to its operation, the security of the customer's personal data is most important. Most of t

Java expert with tomcat tuning experience required, Java expert with tomcat...

Java expert with tomcat tuning experience needed Project Description: Want a Java expert to fix memory issues with tomcat. I need School java project - Threads Programm

Java error handling mechanism, Errors: When a dynamic linking failure or ...

Errors: When a dynamic linking failure or some other "hard" failure in the virtual machine happens, the virtual machine gives an Error. Typical Java programs could not find Error

State about the garbage collection, Explain garbage collection. Java us...

Explain garbage collection. Java uses garbage collection to free the memory. By cleaning those objects which is no longer reference by any of the program. Step involve in clean

Web-based client program, Why are JSP pages the preferred API for creating ...

Why are JSP pages the preferred API for creating a web-based client program? Because no plug-ins or security policy files are required on the client systems (applet does). Also,

Program for users to play the game of brick, Task Your task is  to  w...

Task Your task is  to  write  a  Java  program  that  allows  users  to  play  the  game  of  Brickles. (note:  it  is  up  to  you  whether  to  use the  skeleton).  The prog

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd