Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    drawing in a JFrame

    In the Paint method of a JFrame I can call a method like drawRect( ) and it will work. But when I try to call a class method for drawing, it won't work. It creates the window but doesn't draw anything in it. Here is a simple example. It uses a class, Design, that just uses drawRect and drawOval. An applet can use the Design class and its methods just fine. Why can't the JFrame do the same?

    import java.awt.*;
    import javax.swing.*;

    public class DrawTest extends JFrame {

    Design d;

    public DrawTest( ) {
    super( "Drawing Test" );

    setSize(100, 100 );
    setVisible( true );
    }

    public void init( ) {
    d = new Design( );
    }

    public void paint( Graphics g ) {
    super.paint( g );
    d.draw( g );
    }

    public static void main( String args[] ) {
    DrawTest application = new DrawTest( );
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    } // end of main

    } // end of DrawTest

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more information

    I forgot to say that this gives a NullPointerException at the DrawTest.paint at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source). I find it puzzling that it mentions RepaintManager since it won't even draw for the first time. It also mentions EventDispatchThread. (Sorry, I don't think there is a way to copy and paste all the error messages from the Command Prompt window.)

  • #3
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    I am not sure if you could draw straight into a JFrame instance....though, I might be wrong.

    I think most Java programmers use an instance of the Canvas class to draw on frames.

    here is an example

    http://www.bluej.org/resources/demos...ll/Canvas.java

    Cheers,
    Ess

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    258
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ess is on track there. What you want to paint on, is not the JFrame, but on its contentPane.

    This is more or less the way I always do it, by using JPanels:

    Code:
    import javax.swing.JFrame;
    
    class{
        main{
             //create JFrame
             JFrame frame = new JFrame ("A JFrame");
             frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            
             //create instance of drawing class
             //i guess you could do it on init()..
             Design d = new Design();
            
             //add contentPane to frame
             //add drawings to contentPane
             frame.getContentPane().add(d);
             frame.pack();
             frame.setVisible(true);
    
        }
    }
    Code:
    import javax.swing.JPanel;
    
    Design class extends JPanel{
        public Design(){ //constructor
             //add initial values
             ...
             //set panel color and size
             setBackground (Color.gray);
             setPreferredSize (new Dimension(600,600));
        }    
    
        ...stuff
        public void paint(Graphics g) {
            //drawings go here
            super.paint(g);
            ....stuff
        }
        ...stuff
    }
    Last edited by daniel_g; 07-03-2007 at 10:51 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
    •