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

Advanced java info1414, This is a working program that shows election resul...

This is a working program that shows election results from the infamous 2000 presidential election involving a lot of Floridians and 'hanging chads'. The program as written reads a

Ps1 emulator for android, PS1 emulator for android There is an open sour...

PS1 emulator for android There is an open source app known as psx4droid i want a developer to solve problems and bugs and doing performance improvement to source code to running

Need to develop chat application for j2me, Need to develop Chat application...

Need to develop Chat application for j2me and I want Image Processing code Project Description: We want a chat application for a community website. Image processing and co

Java program to do the following operations:, Read information of n student...

Read information of n students. The class name should be kkhsou with the member variable name, rollno, center_code and percentage in last examestion.Display the students data whose

Java and Arrays, AskWrite an application that uses an Array to store 10mess...

AskWrite an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be “I love

Using rmi build robust distributed systems-jdbc odbc, This work assesses th...

This work assesses the following Outcomes: • Build robust, secure distributed systems using advanced programming techniques Use RMI to build a distributed application accessing

Write output for the given code of for loop, What is the output of the foll...

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 should be public and private, What should be public? What should be pr...

What should be public? What should be private? As a rule of thumb: Classes are public. Fields are private. Constructors are public. Getter and setter methods

Create modeling tickets for a city subway system, For this assignment you'l...

For this assignment you'll create some classes for modeling tickets for a city subway system. The type of system we are modeling has these main features: Stations are grouped in

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