Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Overlapping windows? in a Jframe?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Overlapping windows? in a Jframe?

    public class Ch5SampleGraphics_UF  {
     
        public static void main(String[] args) {
     
            JFrame win;
            Container contentPane;
            Graphics gra;
     
            win = new JFrame("My First Rectangle");
            win.setSize(300, 200);
            win.setLocation(100, 100);
            win.setVisible(true);
     
            contentPane = win.getContentPane();
            contentPane.setBackground(Color.ORANGE);
     
            gra = contentPane.getGraphics();
            gra.setColor(Color.blue);
            gra.drawRect(50, 50, 100, 30);
        }
    }


    in my screen the frame and the content pane are the only thing that appears...

    but the CONTENTS (the geometric shape that is supposed to be in the content pane) doesnt appear.


    how can i solved this one?

    anyway , what i did (for me to see it somehow) is i'm re-executing it again and again... and in some of the execution of the program, sometimes the shapes appear.. sometimes it doesnt
    Last edited by chronoz13; November 20th, 2009 at 09:14 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Overlapping windows? in a Jframe?

    Your code now draws to the graphics object of the content pane, but this doesn't ensure that it will be drawn. The standard way to paint to a JComponent (in your case the conentPane) is to override its paintComponent method. In your case you can create a new JPanel, over-ride its paintComponent method, then add that to the content pane

    JPanel panel = new JPanel(){
        @Override public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.drawRect(50,50,100,30);
        }
    };
    getConentPane().add(panel);

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (November 20th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Overlapping windows? in a Jframe?

    wait a second, i dont know where should i put that code in my program...

    can you please add it so i can examine it carefully...

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Overlapping windows? in a Jframe?

    is it supposed to be like this?

    public class Ch5SampleGraphics_UF  {
     
        public static void main(String[] args) {
     
            JFrame win;
            Container contentPane;
            Graphics gra;
     
            win = new JFrame("My First Rectangle");
            win.setSize(300, 200);
            win.setLocation(100, 100);
            win.setVisible(true);
     
            JPanel panel = new JPanel(){
     
                @Override public void paintComponent(Graphics g){
     
                    super.paintComponent(g);
                    g.setColor(Color.BLUE);
                    g.drawRect(50,50,100,30);
                }
            };
     
            panel.setBackground(Color.red);
            contentPane = win.getContentPane();
            contentPane.add(panel);
     
            gra = contentPane.getGraphics();
            gra.setColor(Color.blue);
            gra.drawRect(50, 50, 100, 30);
        }
    }

    ??

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Overlapping windows? in a Jframe?

    now the big question is??


    why should i add a panel ... so the graphics can be drawn?

    is the content pane not enough to make the drawing visible?

    so when it comes in making a drawing in frame... i should declare a panel for it?

  7. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Overlapping windows? in a Jframe?

    Like this
    public class Ch5SampleGraphics_UF  {
     
        public static void main(String[] args) {
     
            JFrame win;
            Container contentPane;
            Graphics gra;
     
            win = new JFrame("My First Rectangle");
            win.setSize(300, 200);
            win.setLocation(100, 100);
            win.setVisible(true);//this line should be called AFTER things are added to the JFrame, otherwise you must valudate the JFrame, also remember its good practice to call pack()
     
            JPanel panel = new JPanel(){
     
                @Override public void paintComponent(Graphics g){
     
                    super.paintComponent(g);
                    g.setColor(Color.BLUE);
                    g.drawRect(50,50,100,30);
                }
            };
     
            panel.setBackground(Color.red);
            contentPane = win.getContentPane();
            contentPane.add(panel);
            /**These lines do nothing   
            gra = contentPane.getGraphics();
            gra.setColor(Color.blue);
            gra.drawRect(50, 50, 100, 30);
            **/
     
        }
    }

    Quote Originally Posted by chronoz13 View Post
    now the big question is??


    why should i add a panel ... so the graphics can be drawn?

    is the content pane not enough to make the drawing visible?

    so when it comes in making a drawing in frame... i should declare a panel for it?
    The answer is a bit complex and has to do with how things are drawn in Swing. JComponents are drawn using paintComponent, so whenever a window is resized, scrolled, or altered in a way to necessitate redrawing, the paintComponent method is called to redraw the window. Just getting a reference to the Graphics object associated with a JComponent and drawing to it doesn't ensure that it will be drawn (or in more probable terms, erased by the parent paintComponent method next time it is redrawn)

  8. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (November 21st, 2009)

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Overlapping windows? in a Jframe?

    ahhh so i was asking for a complex answer, hehhee, anyway.. you game me a little clarification of what i want...

    tnx for that... sir!

Similar Threads

  1. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM
  2. Replies: 1
    Last Post: March 11th, 2009, 04:41 PM
  3. Replies: 3
    Last Post: February 26th, 2009, 04:21 PM
  4. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 06:56 AM