Andrew
Andrew

Reputation: 1784

Nothing drawn when drawing in a JPanel

I'm trying to draw a rectangle in the JPanel however it wont show up, It works in the frame however.

if you uncomment //frame.getContentPane().add(rect); //Will draw if its in the frame and comment frame.getContentPane().add(panel); // but not in the panel the rectangle will draw.

Thanks for the help.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawingRect{

    public static void main(String[] args) {
        DrawingRect d = new DrawingRect();
    }

  public DrawingRect(){

     JFrame frame = new JFrame("Drawing a rect");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setPreferredSize(new Dimension (500,500));

     Rect rect = new Rect();

     JPanel panel = new JPanel();
     panel.setPreferredSize(new Dimension (500,500));
     panel.setVisible(true);

     panel.add(rect);

    //frame.getContentPane().add(rect); //Will draw if its in the frame
    frame.getContentPane().add(panel); // but not in the panel


     frame.pack();
     frame.setVisible(true);  



     panel.repaint();
     frame.repaint();



  }

  public class Rect extends JComponent{

    private static final long serialVersionUID = 1L;

        public void paint(Graphics g)
        {
            g.setColor(Color.black);
            g.drawRoundRect(10, 10, 100, 100, 20, 20);  
        }
    }

}

Upvotes: 2

Views: 1303

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your Rect is drawing fine, but it's very very small! To see what I mean, show it's size after it has been rendered:

  Rect rect = new Rect();
  rect.setBorder(BorderFactory.createTitledBorder("rect"));

  JPanel panel = new JPanel();
  panel.setPreferredSize(new Dimension(500, 500));
  panel.setVisible(true);

  panel.add(rect);

  // frame.getContentPane().add(rect); //Will draw if its in the frame
  frame.getContentPane().add(panel); // but not in the panel

  frame.pack();
  frame.setVisible(true);

  System.out.println(rect.getSize());

You'll get something like:

java.awt.Dimension[width=1,height=1]

Meaning it is just 1 pixel by 1 pixel -- too small to see.

The solution is to use proper layouts and give it a preferred size so that it will show. e.g.,

   public class Rect extends JComponent {

      private static final long serialVersionUID = 1L;
      private static final int PREF_W = 150;
      private static final int PREF_H = 150;

      @Override
      // public void paint(Graphics g) {
      protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.setColor(Color.black);
         g.drawRoundRect(10, 10, 100, 100, 20, 20);
      }

      @Override
      public Dimension getPreferredSize() {
         return new Dimension(PREF_W, PREF_H);
      }
   }

Also, draw with the JComponent's paintComponent method, not its paint method.

Upvotes: 4

Related Questions