What is the use of Rectangle class in java?
Hi friends,
I just completed core java programming and started advanced java programming. For me the advanced java seems very tough than core java. In which I am not getting the use of Rectangle class. If you posses good knowledge about the Rectangle class of java programming, then please let me know that. I would greatly appreciate you help over Rectangle class.
Re: What is the use of Rectangle class in java?
A Rectangle class specifies an area in a coordinate space that is enclosed by the Rectangle object's top-left point (x, y) in the coordinate space, its width, and its height.A Rectangle object's width and height are public fields. The constructors that create a Rectangle, and the methods that can modify one, do not prevent setting a negative value for width or height. A Rectangle whose width or height is negative is considered empty.
Re: What is the use of Rectangle class in java?
Following java code will depict you about the use of Rectangle class:
Code:
java.awt.Rectangle;
import javax.swing.JComponent;
class RectDemo
{
public void paint(Graphics g)
{
g.drawRect (20, 20, 200, 200);
}
}
public class DrawRect
{
public static void main(String[] agh)
{
JFrame window1 = new JFrame();
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window1.setBounds(40, 40, 300, 300);
window1.getContentPane().add(new RectDemo());
window1.setVisible(true);
}
}
Re: What is the use of Rectangle class in java?
Hello friend,
Before using Rectangle class, you should be aware about below it's constructors:
- Rectangle(Rectangle rct)
- Rectangle(Point pt, Dimension dm)
- Rectangle(Point pt)
- Rectangle(int xn, int yn, int width1, int height1)
- Rectangle(int width1, int height1)
Re: What is the use of Rectangle class in java?
Hi,
I suggest you to study following example of Rectangle class:
Code:
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class RectangleTP extends JApplet {
final static BasicStroke stroke1 = new BasicStroke(2.0f);
public void init() {
setBackground(Color.blue);
setForeground(Color.blue);
}
public void paint(Graphics g) {
Graphics2D g02 = (Graphics2D) g;
g02.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g02.setPaint(Color.gray);
int x = 5;
int y = 7;
g02.setStroke(stroke);
g02.draw(new Rectangle2D.Double(x, y, 200, 200));
g02.drawString("Rectangle2D", x, 250);
}
public static void main(String sko[]) {
JFrame f1 = new JFrame("");
f1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} }
JApplet applet = new RectangleDemo2D();
f1.getContentPane().add("Center", applet);
f1.setSize(new Dimension(400, 300));
f1.show();
}
}
Re: What is the use of Rectangle class in java?
Please go through following various methods of Rectangle class of java:
- translate(int ak, int bk)
- setSize(int wd, int ht)
- setRect(double ak, double bk, double wd, double ht)
- setLocation(int ak, int bk)
- resize(int wd, int ht