Java Clear Copy Graphics Area

Java Clear Copy Graphics Area


java.awt.Graphics class comes with many methods of drawing and also with some non-drawing methods that will support drawing indirectly. These methods include clearRect() and copyArea() etc.

clearRect() clears a part of graphical area where a drawing may exist with its own background. When cleared, the cleared area shows the background area of the frame (a proof that some part of the figure area is cleared).

copyArea() copies of a part of drawing area to another space on the frame. This helps to duplicate a figure or take some part of the figure.

Description of supporting methods

  1. void clearRect(int x, int y, int width, int height): Clears a rectangular area as per the parameter list.
  2. void copyArea(int x, int y, int width, int height, int dx, int dy): Copies a part of the graphics area to the relative area given by dx and dy.

Parameter Particulars

  • int x & int y: These are the x and y coordinates of the rectangle region from where clearing/copying should take place.
  • int width & int height: The width and height of the area to be cleared/copied.
  • int dx & int dy: These give the relative horizontal and vertical distances from the original x and y coordinates of the figure.

Following program illustrates both the methods.

Java Clear Copy Graphics Area

clearRect() clears a rectangle area of 60, 100 x and y coordinates of width 100 and height 70 pixels within the graphics area drawn earlier by fillRect() method in default black color. The cleared area is seen with white color, the background color of the frame.

copyArea() method copies the graphics area of width 70 and height 100 starting from x and y coordinates 270 and 100. This area is copied to another area represented by x coordinate 430 (270+160) and y coordinate 120 (100+20).

One thought on “Java Clear Copy Graphics Area

  1. باطل

    This works for simple painting area or place by changing the the background colors or fill it with new rectangles, but in more advanced complicated Panels that might contains schema or images you cannot clear the painting area that you are painting on, only in some smart and dirty ways.

Leave a Reply

Your email address will not be published. Required fields are marked *