tags:

views:

152

answers:

3
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class MyDrawPanel extends JPanel{
    public void paintComponents(Graphics g){
     g.setColor(Color.orange);
     g.fillRect(20,50,100,100);
    }
    public static void main(String[] args){

     JFrame frame = new JFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.getContentPane().add(paintComponents(g));
     frame.setVisible(true);
    }

}

I think I should add something arguments in frame.getContentPane().add(paintComponents(g));

I looked up Graphics Class but still struggling with it. What should be the parameter of it. Thanks in Advance.

+3  A: 

try this

public class MyDrawPanel extends JPanel{
    MyDrawPanel()
         {
         setOpaque(true);
         }


    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.orange);
        g.fillRect(20,50,100,100);
    }
    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(new MyDrawPanel());
        frame.setBounds(10,10,500, 500);
        frame.setVisible(true);
    }

}
Pierre
Fantastic. it works thanks.
DK
That's great, but do you know WHY it works?
Jorn
sorry it's late here. No time to comment this code.
Pierre
You are accessing Swing components from outside the event thread. See the Java Tutorial as to why that is a bad idea.
Kathy Van Stone
@Kathy Van Stone - Where? The main() method? - not necessarily a problem - http://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/
Nate
A: 

I'm no awt expert, but what I think you want to do is add a Canvas object to your content pane from the JFrame, then paint a Graphics object on it.

Jorn
A: 

Okay, this is what I came up with:

public class MyDrawPanel extends JPanel
{
  private static void createAndShowGUI()
  {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    MyDrawPanel panel = new MyDrawPanel();
    panel.setOpaque(true);
    frame.getContentPane().add(panel);
    frame.setVisible(true);
  }

  @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.setColor(Color.orange);
    g.fillRect(20,50,100,100);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run()
      {
        createAndShowGUI();
      }
    });

  }

}

Notes:

  1. There are several problems with your line frame.getContentPane().add(paintComponents(g));. What you said is "add to the content pane the result of calling paintComponents on g. Where did g come from? You can not used a variable until declared. The result of calling paintComponents is void which means the result cannot be used as an argument to a method. Presumably you had compiler errors.

  2. I changed paintComponents to paintComponent. The former is used to control painting of subcomponents and in general should not be overridden.

  3. Swing objects should not be created on the main thread. The details are complicated for a beginner (and described here in detail). Mostly you can just memorize the SwingUtilities.invokeLater pattern used above.

Kathy Van Stone
Also the @Override is used in Java 5 or later. If you are using an earlier version, you should ignore them.
Kathy Van Stone