If an operation that calculates the bounding box of this Polygon has already been performed, such as getBounds or containsthen this method updates the bounding box.

Parameters: x - the specified X coordinate y - the specified Y coordinate Since: 1. The bounding box is the smallest Rectangle whose sides are parallel to the x and y axes of the coordinate space, and can completely contain the Polygon. Since: 1.

As of JDK version 1. Returns the bounds of this Polygon. Returns: the bounds of this Polygon. Parameters: p - the specified Point to be tested Returns: true if the Polygon contains the Point ; false otherwise. Parameters: x - the specified X coordinate to be tested y - the specified Y coordinate to be tested Returns: true if this Polygon contains the specified coordinates x,y ; false otherwise. Determines whether the specified coordinates are contained in this Polygon.

Note that there is no guarantee that the returned Rectangle2D is the smallest bounding box that encloses the Shapeonly that the Shape lies entirely within the indicated Rectangle2D. The bounding box returned by this method is usually tighter than that returned by the getBounds method and never fails due to overflow problems since the return value can be an instance of the Rectangle2D that uses double precision values to store the dimensions.

Note that the definition of insideness can lead to situations where points on the defining outline of the shape may not be considered contained in the returned bounds object, but only in cases where those points are also not considered contained in the original shape. If a point is inside the shape according to the contains point method, then it must be inside the returned Rectangle2D bounds object according to the contains point method of the bounds. If a point is not inside the shapethen it might still be contained in the bounds object:.

The Shape. This means that for some Shapes this method might return true even though the rectangular area does not intersect the Shape. The Area class performs more accurate computations of geometric intersection than most Shape objects and therefore can be used if a more precise answer is required. This means that for some Shapes this method might return true even though the Rectangle2D does not intersect the Shape.

All coordinates that lie inside the rectangular area must lie within the Shape for the entire rectangular area to be considered contained within the Shape. This means that for some Shapes this method might return false even though the Shape contains the rectangular area. The Area class performs more accurate geometric computations than most Shape objects and therefore can be used if a more precise answer is required.

This means that for some Shapes this method might return false even though the Shape contains the Rectangle2D.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

### Class java.awt.Rectangle

I was wondering what is the consideration I shall take in, while choosing the correct data type? Rectangle uses int coordinates. Rectangle2D is an abstract class that doesn't care if you're using int, double or float coordinates. Rectangle2D is the base class, so if you're writing code that operates on rectangular shapes in an abstract way, go for Rectangle2D, and assign it like so:. You might say that Rectangle should be called Rectangle2D. But that's not quite it either, because e.

## Java-Rectangles

Rectangle is the only one of the three that implements the serializable interface. Learn more. Asked 10 years, 2 months ago. Active 7 years, 2 months ago. Viewed 14k times. Double interchange.

It's a legacy thing. Rectangle2D was added in a later version of Java than Rectangleand the result is Sun's ham-fisted attempt at resolving it.

Active Oldest Votes. If you need the greater precision of double and float, you'll have to go with Rectangle2D. Like skaffman commented, it's a bit of a legacy issue. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. The Overflow How many jobs can be done at home? Featured on Meta. Community and Moderator guidelines for escalating issues via new response….

Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow. Triage needs to be fixed urgently, and users need to be notified upon…. Dark Mode Beta - help us root out low-contrast and un-converted bits.

Linked 4. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.Summary: In this tutorial "Java Draw Rectangles", you learn how much it is easy to draw rectangles in Java.

Java requires width and height of the rectangle and x and y coordinates where rectangle top left corner position is to be drawn. Java does not include a special method, something like drawSquareto draw a square. Square can be drawn with rectangle method only by keeping width and height same. Every closed figure in Java graphics comes with two methods — drawXXX that draws outline not filled with some color, outline figure and fillXXX that draws filled solid figure.

The default drawing color is black with white background which can be changed. Following program draws two right-angled rectangles — outline and solid. Output screen of RectanglesDrawing. We know, the setColor method of Graphics class sets the drawing color. In the above statement, the color is set to blue one of the 13 predefined colors and this color will be effective until changed again. The method draws an outline rectangle in blue color.

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code. Your email address will not be published. Save my name, email, and website in this browser for the next time I comment.

Notify me of follow-up comments by email. Notify me of new posts by email. Leave a Reply Cancel reply Your email address will not be published.Java program to calculate the area of a rectangle. There are maybe so many methods in order to write the java program to find the area of a rectangle. Here we share the two methods, sample method -1, and sample method-2 with online compiler and execution tool.

Below are the online compiler and execution tool check it out area of rectangle. Lol :-p you knew it, but just a couple of lines. The rectangle is a plane figure with four straight sides or just four right angles. Length X Breadth. Or Length X Width. Here is the online execution tool! The above program is just a simple one. If you good at basic skills of java programming, just skip the below code explanation part and move on to the sample method Where you can execute the program there by itself.

Consider it has another sample method Double can store the values up to 8 bytes, and the name os the memory is L. We always make sure to write the java program in a simple way to the users and newbies can able to understand the program better. Lengthy codes are way better too, but those extra couple of code lines are actually not necessary.

Here is another simple method to find out the area of a rectangle, the below program is quite simple. Sample Method — 2. Note: The following program is just for a subject purpose, with your own inbuilt variables directly dumping into the program.

That means a user is not required to submit the input values. Just for a subject purpose. Actually, there is no need to use the scanner function, since there are no user inputs. The inputs values are inbuilt in a program.

How simple it was right? But it is not an actual program.Java program to find the perimeter of a rectangle — In this distinct article, we will discuss the multiple methods used to calculate the perimeter of a rectangle in Java programming. Suitable examples and sample outputs are included in this piece as well for better interpretation.

The compiler has also been added so that you can execute it yourself pretty easily. As we all know, a rectangle is a two-dimensional quadrilateral figure in the field of geometry.

The opposite sides of a rectangle are equal and parallel in nature. Hence, the methods used to calculate the perimeter of a rectangle in Java programming are as follows:.

The ways to find out the perimeter of a rectangle as per the blog are as follows: Using Scanner Class Using Command Line Arguments Using Static Function As we all know, a rectangle is a two-dimensional quadrilateral figure in the field of geometry. All of the angles in a rectangle are 90 degrees. Using Scanner Class. Scanner. Enter the length of the Rectangle : 3. Enter the width of the Rectangle : 4. Using Command Line Arguments.

Using Static Function. Writer - MK. Copyrighted Protected. Enter the length of the Rectangle : 3 Enter the width of the Rectangle : 4 perimeter of Rectangle is : Moving and resizing a rectangle around. Hi everyone, Im trying to create a rectangle which can be moved and resized by the user by dragging it with the mouse. I found this file on the internet which moves rectangles but i dont know how to resize the rectangles.

Ive now adapted the code found at the link i mentioned into my program and it works well. But i dont have any idea on how to resize and rotate the rectangle, any ideas? Campbell Ritchie. I had an assignment whihc required exactly that.

I used a Rectangle2D object for the drawing, encapsulated inside what I called a RectangleShape object to avoid confusion with other classes. Note the Rectangle2D and Ellipse2D both inherit from the java. RectangularShape class, which has methods to set its bounding box from a diagonal or from its corners. Resizing: I added a MouseMotionListener to the panel the shape was drawn on I had to swap Listeners depending on the shape being drawn.

Then the mouseDragged method can be used to move one or other of the corners of the bounding box.

## Subscribe to RSS

Easy Rotation. I had no end of trouble with it. Find the AffineTransform class and look at its methods. You will find some of the methods duplicated in the Graphics2D class, which I actually used myself. Problem 1: Using the Transforms slowed down the rendering without transforms is took about 1.

**Java Beginner Programming Tutorial - 30 - Objects & Classes Rectangle**

This gave rise to a nasty effect where dragging the window or dragging another window over it left multiple borders. I couldn't work out how to sort it out, and in a fit of desperation I added a javax. Timer to the app which calls "repaint ;" repeatedly. Problem 2: After you have put a transform on, you have to take it off again. Otherwise it is still there for any shapes added subsequently. I tried several tricks, and eventually settled for setting the transform at the start of the method and setting the same with the opposite sign at the end of the method.

I didn't think of a better solution until after I had to hand it in: clone the Graphics2D object, and apply the transform to the clone only. I still found that combining rotation and shearing tended to alter the scale of shapes added later.Java requires width and height of the rectangle and x and y coordinates where rectangle is be drawn.

Java does not include a special method, something like drawSquareto draw a square. Square can be drawn with rectangle method only by keeping width and height same. Every closed figure in Java graphics comes with two methods — drawXXX that draws outline not filled with some color figure and fillXXX that draws filled solid figure. The default drawing color is black with white background which can be changed. Java supports 3D rectangles also but the affect of third dimension is not much visible.

As the elevation is very less, the affect is negligible. Architects say, green color gives good affect in elevations. For this reason, the graphics is set to green color in this program. Java designers give the affect of 3D by drawing lighter and darker lines along the rectangle border. The draw3DReect method draws a rectangle using the first four parameters and as the fourth parameter is true, draws the elevation affect by darkening some sides.

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code. Much appreciated! Started wondering what is broken on my system.

I see yours are green, so I tried that. Even white. Yellow is fine. Or perhaps the color difference is too subtle to notice on my laptop screen. Your email address will not be published. Save my name, email, and website in this browser for the next time I comment. Notify me of follow-up comments by email. Notify me of new posts by email. Leave a Reply Cancel reply Your email address will not be published.