Questions on java (basic) graphics program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KiranJyothi
    New Member
    • Apr 2007
    • 45

    #1

    Questions on java (basic) graphics program

    Hello everybody,
    While I am doing the program( pasted below ), I am able to see the frame but not the rectangles. Can anyone please help me to get it done.

    Thanks in advance.
    KiranJyothi

    import java.awt.Graphi cs;
    import java.awt.Graphi cs2D;
    import java.awt.Rectan gle;
    import javax.swing.JPa nel;
    import javax.swing.JCo mponent;

    public class Rectangle1 extends JComponent

    {

    public void painComponent( Graphics g )

    {

    Graphics2D g2 = ( Graphics2D ) g;

    Rectangle box = new Rectangle( 5, 10, 20, 30 );
    g2.draw( box );

    box.translate( 15, 25 );

    g2.draw( box );

    }

    }

    /* Driver Class */

    import javax.swing.JFr ame;

    public class RectangleViewer

    {

    public static void main( String args[] )

    {

    JFrame frame = new JFrame();

    final int FRAME_WIDTH = 300;
    final int FRAME_HEIGHT = 700;

    frame.setSize( FRAME_WIDTH,FRA ME_HEIGHT );
    frame.setTitle( "MyRectangl es. ");
    frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );

    Rectangle1 component = new Rectangle1();
    frame.add( component );

    frame.setVisibl e( true );

    }

    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Don't you need a JPanel inside a JFrame to display anything?

    Not sure of my answer, I only started learning about AWT and Swing...about a week ago!? But in all our examples, we had a class that extended JFrame, and we added a class that extended JPanel, and we added our own little objects to the Panel class, not the Frame class.

    Comment

    • KiranJyothi
      New Member
      • Apr 2007
      • 45

      #3
      [QUOTE=Ganon11]Don't you need a JPanel inside a JFrame to display anything?

      Not sure of my answer, I only started learning about AWT and Swing...about a week ago!? But in all our examples, we had a class that extended JFrame, and we added a class that extended JPanel, and we added our own little objects to the Panel class, not the Frame class.[/QUOT

      Actually, this the example given in my text book. I was just trying to work out all the examples and typed as it was given in the book.
      I was also thinking about JPanel. As far as I remember, we have to add the shapes on the panel and add the panel to the frame.
      Thank You for the response.

      KiranJyothi

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        1.)Please remeber to wrap code with code tags next time you post code.
        2.)You made a typo in overriding the paintComponent method

        Code:
        public void paintComponent
        you wrote public void painComponent instead.

        Comment

        Working...