#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Rectangle and Jframe


    Below is the code to generate the a random color,size and position of rectangle in a JFrame. my question is how can i make sure that the rectangle drawn is entirely inside the JFrame? Asume the frame is 500x500


    class DrawPanel extends JPanel {

    public void paintComponent(Graphics g){

    Graphics2D g2 = (Graphics2D) g;

    int r = (int) (Math.random() * 250);
    int gr = (int) (Math.random() * 250);
    int b = (int) (Math.random() * 250);

    g.setColor(new Color(r,gr,b));

    int ht = (int) ((Math.random() * 120) + 10);
    int width = (int) ((Math.random() * 120) + 10);
    int x = (int) ((Math.random() * 40) + 10);
    int y = (int) ((Math.random() * 40) + 10);

    g.fillRect(x,y,ht,width);
    }
    Last edited by DevoraJava; November 20th, 2012 at 06:31 AM. Reason: Rectangle and Jframe

IMN logo majestic logo threadwatch logo seochat tools logo