Simple Java Rectangle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niharkd
    New Member
    • Sep 2007
    • 1

    Simple Java Rectangle

    Hi,
    I am trying to run the program below. IT compiles fine and also runs. However, once the jframe loads and the container loads, I dont see any rectangle being drawn in there. Can anyone please help?
    I am using jGrasp development environment with JDK 5.
    I am running this on a Windows XP w/ SP2 OS
    Thanks
    Nihar

    /*************** *************** *************** *************** ***********
    Sample Program: Draw a rectangle on a frame window's content pane.
    *************** *************** *************** *************** ***********/

    import javax.swing.*; // for JFrame
    import java.awt.*; // for Graphics and Container
    import java.applet.*;

    class Rectangle extends Applet {

    public static void main(String [] args ) {

    JFrame win;
    Container contentPane;
    Graphics g;

    win = new JFrame("My First Rectangle");
    win.setSize(300 , 300);
    win.setLocation (100,100);
    win.setVisible( true);

    contentPane = win.getContentP ane();
    g = contentPane.get Graphics();
    g.drawRect(50,5 0,100,30);
    }
    }
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by niharkd
    Hi,
    I am trying to run the program below. IT compiles fine and also runs. However, once the jframe loads and the container loads, I dont see any rectangle being drawn in there. Can anyone please help?
    I am using jGrasp development environment with JDK 5.
    I am running this on a Windows XP w/ SP2 OS
    Thanks
    Nihar

    /*************** *************** *************** *************** ***********
    Sample Program: Draw a rectangle on a frame window's content pane.
    *************** *************** *************** *************** ***********/

    import javax.swing.*; // for JFrame
    import java.awt.*; // for Graphics and Container
    import java.applet.*;

    class Rectangle extends Applet {

    public static void main(String [] args ) {

    JFrame win;
    Container contentPane;
    Graphics g;

    win = new JFrame("My First Rectangle");
    win.setSize(300 , 300);
    win.setLocation (100,100);
    win.setVisible( true);

    contentPane = win.getContentP ane();
    g = contentPane.get Graphics();
    g.drawRect(50,5 0,100,30);
    }
    }
    Hi,
    For drawing shapes u should have awt's
    Code:
     
    public void paint(Graphics g)
    method in ur class. Hope this vil help u.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by niharkd
      Hi,
      I am trying to run the program below. IT compiles fine and also runs. However, once the jframe loads and the container loads, I dont see any rectangle being drawn in there. Can anyone please help?
      I am using jGrasp development environment with JDK 5.
      I am running this on a Windows XP w/ SP2 OS
      Thanks
      Nihar

      /*************** *************** *************** *************** ***********
      Sample Program: Draw a rectangle on a frame window's content pane.
      *************** *************** *************** *************** ***********/

      import javax.swing.*; // for JFrame
      import java.awt.*; // for Graphics and Container
      import java.applet.*;

      class Rectangle extends Applet {

      public static void main(String [] args ) {

      JFrame win;
      Container contentPane;
      Graphics g;

      win = new JFrame("My First Rectangle");
      win.setSize(300 , 300);
      win.setLocation (100,100);
      win.setVisible( true);

      contentPane = win.getContentP ane();
      g = contentPane.get Graphics();
      g.drawRect(50,5 0,100,30);
      }
      }
      Read Sun's painting tutorial and try again.

      Comment

      Working...