Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Help with 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?

    Java Code:
    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
    Thanks

    Albert:rolleyes:

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    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

    Greetings

    Felissa:p

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    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:
    Java 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);
    
        }
    }
    Java 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
    }
    Greetings

    Eric

Similar Threads

  1. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  2. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 05:12 PM
  3. JFrame Question ?
    By Mindhunter74 in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 10:45 PM
  4. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 07:44 AM
  5. JFrame question
    By Try2Live4God in forum New To Java
    Replies: 2
    Last Post: 10-15-2007, 01:52 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
  •