Results 1 to 6 of 6

Thread: What is the use of Rectangle class in java?

  1. #1
    Join Date
    Jan 2010
    Posts
    99

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    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);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    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)

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    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();
      }
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,394

    Re: What is the use of Rectangle class in java?

    Please go through following various methods of Rectangle class of java:
    1. translate(int ak, int bk)
    2. setSize(int wd, int ht)
    3. setRect(double ak, double bk, double wd, double ht)
    4. setLocation(int ak, int bk)
    5. resize(int wd, int ht

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. How to use Event class of java?
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 07:15 AM
  3. SwingUtilities class of java
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 07:23 AM
  4. What is wrapper class in java?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 07:40 AM
  5. Java Programming Language Basics: Reflection Basics and Class Class
    By mayuri_gunjan in forum Guides & Tutorials
    Replies: 6
    Last Post: 29-08-2005, 12:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •