7 Replies - 792 Views - Last Post: 12 May 2012 - 08:33 PM Rate Topic: -----

#1 TheCodeNoob   User is offline

  • New D.I.C Head

Reputation: -12
  • View blog
  • Posts: 36
  • Joined: 21-November 11

Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 05:21 PM

So I've been trying to experiment with the Java graphics object with what information my Java teacher has taught me so far. To do this, I created a 300 x 350 frame and instructed it to draw a 100 x 100 square at (100, 100), yet for whatever reason, it wouldn't show up until I set up a JOptionPane right before the Graphics object was declared.

This is the specific code that is apparently needed to make it work:

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


public class ImageDrawing {
    
    public static void main(String[] args) {
       JFrame window = new JFrame();
       window.setVisible(true);
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       window.setSize(300, 350);
       
       Container contentPane = window.getContentPane();
       
       JOptionPane.showMessageDialog(window, "Apparently this needs to show up before anything is drawn");
       
       Graphics g = contentPane.getGraphics();
       g.drawRect(100, 100, 100, 100);
    }
    
}


Is there any particular reason I have to call a JOptionPane before Graphics like this and, more importantly, are there any EASY workarounds that don't involve me making the driver class an extension of a JFrame?

This post has been edited by TheCodeNoob: 12 May 2012 - 05:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why do I need a message pane to draw a rectangle on a JFrame?

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 05:40 PM

You don't need that JOptionPane. Replace it with

try {
     Thread.sleep(1000);
     } 
     catch (InterruptedException ex) {
        ex.printStackTrace();
     }



and watch what happens. Now minimize the window and then make it visible again. Your square is gone. My guess is that since you're doing all of this drawing in the main thread, which you should never do, the EDT is sending a repaint() request as soon as the main thread ends, painting over your square. I guess the sleep() and the JOptionPane being shown interfere with that repaint() somehow.

I think it's doing:

setsize and show window
draw rect
repaint.

with the joptionpane or thread.sleep:
setsize and show window
sleep main thread
repaint
draw rect.

as the repaint() requests are happening in a different thread.

Best I can come up with.

This post has been edited by farrell2k: 12 May 2012 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 TheCodeNoob   User is offline

  • New D.I.C Head

Reputation: -12
  • View blog
  • Posts: 36
  • Joined: 21-November 11

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 06:15 PM

Quote

My guess is that since you're doing all of this drawing in the main thread, which you should never do, the EDT is sending a repaint() request as soon as the main thread ends, painting over your square.


So how do I avoid this? Because the only other option I can think of is making the driver class an extension of a JFrame and have everything drawn in a paint class. However, when I do that, what seems to happen is that the content pane seems to go black with a small fraction of a white square in the top left corner which disappears after I minimize the window. And that's when I actually tell it to draw something. Otherwise, the content pane is just flat out not there.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 06:23 PM

You subclass JPanel, and override its paintComponent() to do all of your painting, then you add that JPanel to the JFrame's content pane. Decent tuts here.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 06:29 PM

Handle the drawing before you setVisible() on the JFrame. You won't need a JOptionPane or Thread.sleep() call. When you setVisible() before drawing, a repaint() call is needed to update the JFrame's display.

farrell2k's approach is really the cleaner and more OO approach though. Using getGraphics() is not good practice.
Was This Post Helpful? 0
  • +
  • -

#6 TheCodeNoob   User is offline

  • New D.I.C Head

Reputation: -12
  • View blog
  • Posts: 36
  • Joined: 21-November 11

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 06:32 PM

Ok, so let me see if I'm understanding you right.

So what you're trying to tell me is to create a completely new class outside the driver class which implements a JPanel and override this paintComponent() method with instructions for what I want to draw, then in the driver class create an instance of this object which I then pass off as a JFrame's content pane?

If I got some part of that logic wrong, please tell me. I'm still pretty new to Java logic.

Quote

farrell2k's approach is really the cleaner and more OO approach though. Using getGraphics() is not good practice.


I'm sure it's not, but that's what my teacher taught me at the level of programming the class is at.

This post has been edited by TheCodeNoob: 12 May 2012 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 06:35 PM

That is the logic, correct.
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Why do I need a message pane to draw a rectangle on a JFrame?

Posted 12 May 2012 - 08:33 PM

View PostTheCodeNoob, on 13 May 2012 - 01:32 AM, said:

Ok, so let me see if I'm understanding you right.

So what you're trying to tell me is to create a completely new class outside the driver class which implements a JPanel and override this paintComponent() method with instructions for what I want to draw, then in the driver class create an instance of this object which I then pass off as a JFrame's content pane?

If I got some part of that logic wrong, please tell me. I'm still pretty new to Java logic.

Quote

farrell2k's approach is really the cleaner and more OO approach though. Using getGraphics() is not good practice.


I'm sure it's not, but that's what my teacher taught me at the level of programming the class is at.


If your teacher is teaching you to do any drawing or updating of swing components in your application's main thread, he or she is an idiot. If you paid for this class, you should get your money back.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1